wip: Testing json parser
This commit is contained in:
parent
6f8249a683
commit
15167131f9
47
src/types.rs
47
src/types.rs
|
@ -1,6 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Message {
|
||||
pub src: String,
|
||||
pub dest: String,
|
||||
|
@ -9,17 +10,27 @@ pub struct Message {
|
|||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct MessageBody {
|
||||
#[serde(rename = "type")]
|
||||
message_type: String,
|
||||
msg_id: i64,
|
||||
msg_id: Option<i64>,
|
||||
in_reply_to: Option<i64>,
|
||||
|
||||
#[serde(rename = "type")]
|
||||
message_type: String,
|
||||
|
||||
#[serde(flatten)]
|
||||
pub message: MessageEnum,
|
||||
pub message_body: MessageEnum,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum MessageEnum {}
|
||||
#[serde(untagged, deny_unknown_fields)]
|
||||
pub enum MessageEnum {
|
||||
Echo(EchoMessage),
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct EchoMessage {
|
||||
echo: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
@ -34,7 +45,8 @@ mod test {
|
|||
\"body\": {
|
||||
\"type\": \"echo\",
|
||||
\"msg_id\": 1,
|
||||
\"echo\": \"Please echo 35\"
|
||||
\"echo\": \"Please echo 35\",
|
||||
\"extraneous\": \"hi\"
|
||||
}
|
||||
}";
|
||||
|
||||
|
@ -44,8 +56,27 @@ mod test {
|
|||
eprintln!("successfully parsed into Message {:#?}", resp);
|
||||
}
|
||||
|
||||
eprintln!("{:#?}", resp);
|
||||
|
||||
assert!(resp.is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_echo() {
|
||||
let body = "{
|
||||
\"src\": \"c1\",
|
||||
\"dest\": \"n1\",
|
||||
\"body\": {
|
||||
\"type\": \"echo\",
|
||||
\"msg_id\": 1,
|
||||
\"echo\": \"Please echo 35\"
|
||||
}
|
||||
}";
|
||||
|
||||
let resp: Result<Message, serde_json::Error> = serde_json::from_str(body);
|
||||
|
||||
if resp.is_err() {
|
||||
eprintln!("failed to parsed into Echo Message {:#?}", resp);
|
||||
}
|
||||
|
||||
assert!(resp.is_ok());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user