Implement sentence encoding

This commit is contained in:
Aram 🍐 2021-08-03 20:20:41 -04:00
parent 472395ca14
commit e267b837de

View file

@ -184,6 +184,34 @@ macro_rules! impl_sentences {
None None
} }
/// Encodes the sentence.
pub(crate) fn encode(&self) -> Vec<&str> {
use super::Word::*;
match self {
$(
Self::$name {
$($arg,)*
} => {
#[allow(unused_mut)]
let mut words = vec![
$(
$word.encode(),
)*
];
$(
words[$argidx] = Some($arg);
)*
words
.into_iter()
.flatten()
.collect()
}
)*
}
}
} }
}; };
} }
@ -193,9 +221,27 @@ macro_rules! test_decode {
([$($word:expr$(,)?)+] => $expected:expr) => { ([$($word:expr$(,)?)+] => $expected:expr) => {
assert_eq!( assert_eq!(
Sentences::decode(vec![ Sentences::decode(vec![
$($word.into(),)* $(String::from($word),)*
]), ]),
$expected Some($expected)
);
};
}
#[allow(unused_macros)]
macro_rules! test_encode_decode {
([$($word:expr$(,)?)+] <=> $expected:expr) => {
assert_eq!(
Sentences::decode(vec![
$(String::from($word),)*
]),
Some($expected)
);
assert_eq!(
vec![
$(String::from($word),)*
],
$expected.encode()
); );
}; };
} }
@ -519,149 +565,155 @@ pub mod server_bound {
mod tests { mod tests {
use super::*; use super::*;
#[test] #[test]
fn test_decode() { fn test_encode_decode() {
test_decode!( test_encode_decode!(
["GET", "NUMLOGINS", "nutdev"] => ["GET", "NUMLOGINS", "nutdev"] <=>
Some(Sentences::QueryNumLogins { Sentences::QueryNumLogins {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["GET", "UPSDESC", "nutdev"] => ["GET", "NUMLOGINS", "nutdev"] <=>
Some(Sentences::QueryUpsDesc { Sentences::QueryNumLogins {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["GET", "VAR", "nutdev", "test.var"] => ["GET", "UPSDESC", "nutdev"] <=>
Some(Sentences::QueryVar { Sentences::QueryUpsDesc {
ups_name: "nutdev".into(),
}
);
test_encode_decode!(
["GET", "VAR", "nutdev", "test.var"] <=>
Sentences::QueryVar {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
}) }
); );
test_decode!( test_encode_decode!(
["GET", "TYPE", "nutdev", "test.var"] => ["GET", "TYPE", "nutdev", "test.var"] <=>
Some(Sentences::QueryType { Sentences::QueryType {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
}) }
); );
test_decode!( test_encode_decode!(
["GET", "DESC", "nutdev", "test.var"] => ["GET", "DESC", "nutdev", "test.var"] <=>
Some(Sentences::QueryDesc { Sentences::QueryDesc {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
}) }
); );
test_decode!( test_encode_decode!(
["GET", "CMDDESC", "nutdev", "test.cmd"] => ["GET", "CMDDESC", "nutdev", "test.cmd"] <=>
Some(Sentences::QueryCmdDesc { Sentences::QueryCmdDesc {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
cmd_name: "test.cmd".into(), cmd_name: "test.cmd".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "VAR", "nutdev"] => ["LIST", "VAR", "nutdev"] <=>
Some(Sentences::QueryListVar { Sentences::QueryListVar {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "RW", "nutdev"] => ["LIST", "RW", "nutdev"] <=>
Some(Sentences::QueryListRw { Sentences::QueryListRw {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "CMD", "nutdev"] => ["LIST", "CMD", "nutdev"] <=>
Some(Sentences::QueryListCmd { Sentences::QueryListCmd {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "ENUM", "nutdev", "test.var"] => ["LIST", "ENUM", "nutdev", "test.var"] <=>
Some(Sentences::QueryListEnum { Sentences::QueryListEnum {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "RANGE", "nutdev", "test.var"] => ["LIST", "RANGE", "nutdev", "test.var"] <=>
Some(Sentences::QueryListRange { Sentences::QueryListRange {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LIST", "CLIENT", "nutdev"] => ["LIST", "CLIENT", "nutdev"] <=>
Some(Sentences::QueryListClient { Sentences::QueryListClient {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["SET", "VAR", "nutdev", "test.var", "something"] => ["SET", "VAR", "nutdev", "test.var", "something"] <=>
Some(Sentences::ExecSetVar { Sentences::ExecSetVar {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
var_name: "test.var".into(), var_name: "test.var".into(),
value: "something".into(), value: "something".into(),
}) }
); );
test_decode!( test_encode_decode!(
["INSTCMD", "nutdev", "test.cmd"] => ["INSTCMD", "nutdev", "test.cmd"] <=>
Some(Sentences::ExecInstCmd { Sentences::ExecInstCmd {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
cmd_name: "test.cmd".into(), cmd_name: "test.cmd".into(),
}) }
); );
test_decode!( test_encode_decode!(
["LOGOUT"] => ["LOGOUT"] <=>
Some(Sentences::ExecLogout {}) Sentences::ExecLogout {}
); );
test_decode!( test_encode_decode!(
["LOGIN", "nutdev"] => ["LOGIN", "nutdev"] <=>
Some(Sentences::ExecLogin { Sentences::ExecLogin {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["MASTER", "nutdev"] => ["MASTER", "nutdev"] <=>
Some(Sentences::ExecMaster { Sentences::ExecMaster {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["FSD", "nutdev"] => ["FSD", "nutdev"] <=>
Some(Sentences::ExecForcedShutDown { Sentences::ExecForcedShutDown {
ups_name: "nutdev".into(), ups_name: "nutdev".into(),
}) }
); );
test_decode!( test_encode_decode!(
["PASSWORD", "topsecret"] => ["PASSWORD", "topsecret"] <=>
Some(Sentences::SetPassword { Sentences::SetPassword {
password: "topsecret".into(), password: "topsecret".into(),
}) }
); );
test_decode!( test_encode_decode!(
["USERNAME", "john"] => ["USERNAME", "john"] <=>
Some(Sentences::SetUsername { Sentences::SetUsername {
username: "john".into(), username: "john".into(),
}) }
); );
test_decode!( test_encode_decode!(
["STARTTLS"] => ["STARTTLS"] <=>
Some(Sentences::ExecStartTLS {}) Sentences::ExecStartTLS {}
); );
test_decode!( test_encode_decode!(
["HELP"] => ["HELP"] <=>
Some(Sentences::QueryHelp {}) Sentences::QueryHelp {}
); );
test_decode!( test_encode_decode!(
["VERSION"] => ["VERSION"] <=>
Some(Sentences::QueryVersion {}) Sentences::QueryVersion {}
); );
test_decode!( test_encode_decode!(
["NETVER"] => ["NETVER"] <=>
Some(Sentences::QueryNetworkVersion {}) Sentences::QueryNetworkVersion {}
); );
} }
} }