Accept ending whitespace in commands

Signed-off-by: Egor Savkin <es@m-labs.hk>
This commit is contained in:
Egor Savkin 2022-12-23 15:15:41 +08:00
parent 211e0fa1bd
commit 60118a56c8
4 changed files with 87 additions and 49 deletions

21
Cargo.lock generated
View File

@ -204,6 +204,26 @@ dependencies = [
"void", "void",
] ]
[[package]]
name = "format-bytes"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48942366ef93975da38e175ac9e10068c6fc08ca9e85930d4f098f4d5b14c2fd"
dependencies = [
"format-bytes-macros",
]
[[package]]
name = "format-bytes-macros"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "203aadebefcc73d12038296c228eabf830f99cba991b0032adf20e9fa6ce7e4f"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "generic-array" name = "generic-array"
version = "0.12.3" version = "0.12.3"
@ -558,6 +578,7 @@ dependencies = [
"cortex-m-log", "cortex-m-log",
"cortex-m-rt 0.6.13", "cortex-m-rt 0.6.13",
"eeprom24x", "eeprom24x",
"format-bytes",
"heapless", "heapless",
"log", "log",
"nb 1.0.0", "nb 1.0.0",

View File

@ -38,6 +38,9 @@ heapless = "0.5"
serde-json-core = "0.1" serde-json-core = "0.1"
sfkv = "0.1" sfkv = "0.1"
[dev-dependencies]
format-bytes = "0.3.0"
[features] [features]
semihosting = ["panic-semihosting", "cortex-m-log/semihosting"] semihosting = ["panic-semihosting", "cortex-m-log/semihosting"]

View File

@ -23,7 +23,7 @@ class Client:
return line return line
def _command(self, *command): def _command(self, *command):
self._socket.sendall(((" ".join(command)).strip() + "\n").encode('utf-8')) self._socket.sendall((" ".join(command) + "\n").encode('utf-8'))
line = self._read_line() line = self._read_line()
response = json.loads(line) response = json.loads(line)

View File

@ -585,6 +585,7 @@ fn fan_curve(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
} }
fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> { fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
let (input, result) =
alt((value(Ok(Command::Quit), tag("quit")), alt((value(Ok(Command::Quit), tag("quit")),
load, load,
save, save,
@ -600,7 +601,9 @@ fn command(input: &[u8]) -> IResult<&[u8], Result<Command, Error>> {
fan, fan,
fan_curve, fan_curve,
value(Ok(Command::ShowHWRev), tag("hwrev")), value(Ok(Command::ShowHWRev), tag("hwrev")),
))(input) ))(input)?;
let (input, _) = end(input)?; // accept trailing whitespace(s) if present
Ok((input, result))
} }
impl Command { impl Command {
@ -619,46 +622,57 @@ impl Command {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
extern crate format_bytes;
use format_bytes::format_bytes;
macro_rules! parse_chkwhitespace {
($cmd: literal) => {{
let command = Command::parse($cmd);
let command_w = Command::parse(&format_bytes!(b"{}\r\t \r\n\n\t ", $cmd));
assert_eq!(command, command_w);
command
}};
}
#[test] #[test]
fn parse_quit() { fn parse_quit() {
let command = Command::parse(b"quit"); let command = parse_chkwhitespace!(b"quit");
assert_eq!(command, Ok(Command::Quit)); assert_eq!(command, Ok(Command::Quit));
} }
#[test] #[test]
fn parse_load() { fn parse_load() {
let command = Command::parse(b"load"); let command = parse_chkwhitespace!(b"load");
assert_eq!(command, Ok(Command::Load { channel: None })); assert_eq!(command, Ok(Command::Load { channel: None }));
} }
#[test] #[test]
fn parse_load_channel() { fn parse_load_channel() {
let command = Command::parse(b"load 0"); let command = parse_chkwhitespace!(b"load 0");
assert_eq!(command, Ok(Command::Load { channel: Some(0) })); assert_eq!(command, Ok(Command::Load { channel: Some(0) }));
} }
#[test] #[test]
fn parse_save() { fn parse_save() {
let command = Command::parse(b"save"); let command = parse_chkwhitespace!(b"save");
assert_eq!(command, Ok(Command::Save { channel: None })); assert_eq!(command, Ok(Command::Save { channel: None }));
} }
#[test] #[test]
fn parse_save_channel() { fn parse_save_channel() {
let command = Command::parse(b"save 0"); let command = parse_chkwhitespace!(b"save 0");
assert_eq!(command, Ok(Command::Save { channel: Some(0) })); assert_eq!(command, Ok(Command::Save { channel: Some(0) }));
} }
#[test] #[test]
fn parse_show_ipv4() { fn parse_show_ipv4() {
let command = Command::parse(b"ipv4"); let command = parse_chkwhitespace!(b"ipv4");
assert_eq!(command, Ok(Command::Show(ShowCommand::Ipv4))); assert_eq!(command, Ok(Command::Show(ShowCommand::Ipv4)));
} }
#[test] #[test]
fn parse_ipv4() { fn parse_ipv4() {
let command = Command::parse(b"ipv4 192.168.1.26/24"); let command = parse_chkwhitespace!(b"ipv4 192.168.1.26/24");
assert_eq!(command, Ok(Command::Ipv4(Ipv4Config { assert_eq!(command, Ok(Command::Ipv4(Ipv4Config {
address: [192, 168, 1, 26], address: [192, 168, 1, 26],
mask_len: 24, mask_len: 24,
@ -668,7 +682,7 @@ mod test {
#[test] #[test]
fn parse_ipv4_and_gateway() { fn parse_ipv4_and_gateway() {
let command = Command::parse(b"ipv4 10.42.0.126/8 10.1.0.1"); let command = parse_chkwhitespace!(b"ipv4 10.42.0.126/8 10.1.0.1");
assert_eq!(command, Ok(Command::Ipv4(Ipv4Config { assert_eq!(command, Ok(Command::Ipv4(Ipv4Config {
address: [10, 42, 0, 126], address: [10, 42, 0, 126],
mask_len: 8, mask_len: 8,
@ -678,31 +692,31 @@ mod test {
#[test] #[test]
fn parse_report() { fn parse_report() {
let command = Command::parse(b"report"); let command = parse_chkwhitespace!(b"report");
assert_eq!(command, Ok(Command::Show(ShowCommand::Input))); assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
} }
#[test] #[test]
fn parse_report_mode() { fn parse_report_mode() {
let command = Command::parse(b"report mode"); let command = parse_chkwhitespace!(b"report mode");
assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting))); assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting)));
} }
#[test] #[test]
fn parse_report_mode_on() { fn parse_report_mode_on() {
let command = Command::parse(b"report mode on"); let command = parse_chkwhitespace!(b"report mode on");
assert_eq!(command, Ok(Command::Reporting(true))); assert_eq!(command, Ok(Command::Reporting(true)));
} }
#[test] #[test]
fn parse_report_mode_off() { fn parse_report_mode_off() {
let command = Command::parse(b"report mode off"); let command = parse_chkwhitespace!(b"report mode off");
assert_eq!(command, Ok(Command::Reporting(false))); assert_eq!(command, Ok(Command::Reporting(false)));
} }
#[test] #[test]
fn parse_pwm_i_set() { fn parse_pwm_i_set() {
let command = Command::parse(b"pwm 1 i_set 16383"); let command = parse_chkwhitespace!(b"pwm 1 i_set 16383");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 1, channel: 1,
pin: PwmPin::ISet, pin: PwmPin::ISet,
@ -712,7 +726,7 @@ mod test {
#[test] #[test]
fn parse_pwm_pid() { fn parse_pwm_pid() {
let command = Command::parse(b"pwm 0 pid"); let command = parse_chkwhitespace!(b"pwm 0 pid");
assert_eq!(command, Ok(Command::PwmPid { assert_eq!(command, Ok(Command::PwmPid {
channel: 0, channel: 0,
})); }));
@ -720,7 +734,7 @@ mod test {
#[test] #[test]
fn parse_pwm_max_i_pos() { fn parse_pwm_max_i_pos() {
let command = Command::parse(b"pwm 0 max_i_pos 7"); let command = parse_chkwhitespace!(b"pwm 0 max_i_pos 7");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
pin: PwmPin::MaxIPos, pin: PwmPin::MaxIPos,
@ -730,7 +744,7 @@ mod test {
#[test] #[test]
fn parse_pwm_max_i_neg() { fn parse_pwm_max_i_neg() {
let command = Command::parse(b"pwm 0 max_i_neg 128"); let command = parse_chkwhitespace!(b"pwm 0 max_i_neg 128");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
pin: PwmPin::MaxINeg, pin: PwmPin::MaxINeg,
@ -740,7 +754,7 @@ mod test {
#[test] #[test]
fn parse_pwm_max_v() { fn parse_pwm_max_v() {
let command = Command::parse(b"pwm 0 max_v 32768"); let command = parse_chkwhitespace!(b"pwm 0 max_v 32768");
assert_eq!(command, Ok(Command::Pwm { assert_eq!(command, Ok(Command::Pwm {
channel: 0, channel: 0,
pin: PwmPin::MaxV, pin: PwmPin::MaxV,
@ -750,13 +764,13 @@ mod test {
#[test] #[test]
fn parse_pid() { fn parse_pid() {
let command = Command::parse(b"pid"); let command = parse_chkwhitespace!(b"pid");
assert_eq!(command, Ok(Command::Show(ShowCommand::Pid))); assert_eq!(command, Ok(Command::Show(ShowCommand::Pid)));
} }
#[test] #[test]
fn parse_pid_target() { fn parse_pid_target() {
let command = Command::parse(b"pid 0 target 36.5"); let command = parse_chkwhitespace!(b"pid 0 target 36.5");
assert_eq!(command, Ok(Command::Pid { assert_eq!(command, Ok(Command::Pid {
channel: 0, channel: 0,
parameter: PidParameter::Target, parameter: PidParameter::Target,
@ -766,13 +780,13 @@ mod test {
#[test] #[test]
fn parse_steinhart_hart() { fn parse_steinhart_hart() {
let command = Command::parse(b"s-h"); let command = parse_chkwhitespace!(b"s-h");
assert_eq!(command, Ok(Command::Show(ShowCommand::SteinhartHart))); assert_eq!(command, Ok(Command::Show(ShowCommand::SteinhartHart)));
} }
#[test] #[test]
fn parse_steinhart_hart_set() { fn parse_steinhart_hart_set() {
let command = Command::parse(b"s-h 1 t0 23.05"); let command = parse_chkwhitespace!(b"s-h 1 t0 23.05");
assert_eq!(command, Ok(Command::SteinhartHart { assert_eq!(command, Ok(Command::SteinhartHart {
channel: 1, channel: 1,
parameter: ShParameter::T0, parameter: ShParameter::T0,
@ -782,13 +796,13 @@ mod test {
#[test] #[test]
fn parse_postfilter() { fn parse_postfilter() {
let command = Command::parse(b"postfilter"); let command = parse_chkwhitespace!(b"postfilter");
assert_eq!(command, Ok(Command::Show(ShowCommand::PostFilter))); assert_eq!(command, Ok(Command::Show(ShowCommand::PostFilter)));
} }
#[test] #[test]
fn parse_postfilter_off() { fn parse_postfilter_off() {
let command = Command::parse(b"postfilter 1 off"); let command = parse_chkwhitespace!(b"postfilter 1 off");
assert_eq!(command, Ok(Command::PostFilter { assert_eq!(command, Ok(Command::PostFilter {
channel: 1, channel: 1,
rate: None, rate: None,
@ -797,7 +811,7 @@ mod test {
#[test] #[test]
fn parse_postfilter_rate() { fn parse_postfilter_rate() {
let command = Command::parse(b"postfilter 0 rate 21"); let command = parse_chkwhitespace!(b"postfilter 0 rate 21");
assert_eq!(command, Ok(Command::PostFilter { assert_eq!(command, Ok(Command::PostFilter {
channel: 0, channel: 0,
rate: Some(21.0), rate: Some(21.0),
@ -806,7 +820,7 @@ mod test {
#[test] #[test]
fn parse_center_point() { fn parse_center_point() {
let command = Command::parse(b"center 0 1.5"); let command = parse_chkwhitespace!(b"center 0 1.5");
assert_eq!(command, Ok(Command::CenterPoint { assert_eq!(command, Ok(Command::CenterPoint {
channel: 0, channel: 0,
center: CenterPoint::Override(1.5), center: CenterPoint::Override(1.5),
@ -815,7 +829,7 @@ mod test {
#[test] #[test]
fn parse_center_point_vref() { fn parse_center_point_vref() {
let command = Command::parse(b"center 1 vref"); let command = parse_chkwhitespace!(b"center 1 vref");
assert_eq!(command, Ok(Command::CenterPoint { assert_eq!(command, Ok(Command::CenterPoint {
channel: 1, channel: 1,
center: CenterPoint::Vref, center: CenterPoint::Vref,
@ -824,25 +838,25 @@ mod test {
#[test] #[test]
fn parse_fan_show() { fn parse_fan_show() {
let command = Command::parse(b"fan"); let command = parse_chkwhitespace!(b"fan");
assert_eq!(command, Ok(Command::ShowFan)); assert_eq!(command, Ok(Command::ShowFan));
} }
#[test] #[test]
fn parse_fan_set() { fn parse_fan_set() {
let command = Command::parse(b"fan 42"); let command = parse_chkwhitespace!(b"fan 42");
assert_eq!(command, Ok(Command::FanSet {fan_pwm: 42})); assert_eq!(command, Ok(Command::FanSet {fan_pwm: 42}));
} }
#[test] #[test]
fn parse_fan_auto() { fn parse_fan_auto() {
let command = Command::parse(b"fan auto"); let command = parse_chkwhitespace!(b"fan auto");
assert_eq!(command, Ok(Command::FanAuto)); assert_eq!(command, Ok(Command::FanAuto));
} }
#[test] #[test]
fn parse_fcurve_set() { fn parse_fcurve_set() {
let command = Command::parse(b"fcurve 1.2 3.4 5.6"); let command = parse_chkwhitespace!(b"fcurve 1.2 3.4 5.6");
assert_eq!(command, Ok(Command::FanCurve { assert_eq!(command, Ok(Command::FanCurve {
k_a: 1.2, k_a: 1.2,
k_b: 3.4, k_b: 3.4,
@ -852,13 +866,13 @@ mod test {
#[test] #[test]
fn parse_fcurve_default() { fn parse_fcurve_default() {
let command = Command::parse(b"fcurve default"); let command = parse_chkwhitespace!(b"fcurve default");
assert_eq!(command, Ok(Command::FanCurveDefaults)); assert_eq!(command, Ok(Command::FanCurveDefaults));
} }
#[test] #[test]
fn parse_hwrev() { fn parse_hwrev() {
let command = Command::parse(b"hwrev"); let command = parse_chkwhitespace!(b"hwrev");
assert_eq!(command, Ok(Command::ShowHWRev)); assert_eq!(command, Ok(Command::ShowHWRev));
} }
} }