Below is my code. The message is passed by another application. This application may send PeerCommand or PeerEvent. But once I check message is a PeerCommand type then I cannot check if the message is PeerEvent. Im getting mismatched types compile-time error.
serde_json::from_str(&line) is a generic function that returns a value with the type serde_json::Result<T>, where T is the type you're deserializing the line into.
The first match message means that message must be a PeerCommand, therefore serde_json::Result<T> must be a serde_json::Result<PeerCommand>, and the compiler will generate code that deserializes a PeerCommand from the JSON. But inside the second match message, message must be a PeerEvent. Hence an error: serde_json::from_str(&line) can't return both a serde_json::Result<PeerCommand> and serde_json::Result<PeerEvent> at the same time.
If you want to deserialize something that is either a PeerCommand or PeerEvent, you need to make an enum that models that:
You may need to add a serde attribute like #[serde(tag = "something")] to the enum to make it work depending on what the JSON you're working with actually looks like. You can read about such attributes here: Enum representations · Serde
By the way, it's always best if you add the full compiler error that cargo check outputs, it makes it a lot easier for someone reading your post to see what the issue is.