diff --git a/src/ingest_protocol/packet_types.rs b/src/ingest_protocol/packet_types.rs index de8e5c8..f3409c6 100644 --- a/src/ingest_protocol/packet_types.rs +++ b/src/ingest_protocol/packet_types.rs @@ -52,8 +52,8 @@ impl Hash for SensorValue { /// Функция-помощник для [MacAsArray], предназначенный для использования с [serde_with]. /// /// Преобразует MAC-адрес. -fn mac_as_array(value: &str) -> Result<[u8; 6], Error<&str>> { - Ok(parse_mac_address(value)?.1) +fn mac_as_array(value: &str) -> Result<[u8; 6], Error> { + parse_mac_address(value.as_bytes()) } serde_with::serde_conv!( diff --git a/src/ingest_protocol/tests/mod.rs b/src/ingest_protocol/tests/mod.rs index 6a213ad..e93b835 100644 --- a/src/ingest_protocol/tests/mod.rs +++ b/src/ingest_protocol/tests/mod.rs @@ -17,8 +17,9 @@ fn test_asd() { #ALT#38 ## "# - .trim(); - let packet = parse_packet(asd).unwrap().1; + .trim() + .as_bytes(); + let packet = parse_packet(asd).unwrap(); assert!(serde_json::to_string_pretty(&packet) .unwrap() .contains(r#""time": 1000000.0"#)); @@ -27,8 +28,8 @@ fn test_asd() { #[test] fn test_mac() { assert_eq!( - parse_mac_address("12-34-AA-12-55-AA").unwrap(), - ("", [18, 52, 170, 18, 85, 170]) + parse_mac_address(b"12-34-Aa-1255-fA").unwrap(), + [18, 52, 170, 18, 85, 250], ); dbg!(Epoch::now().unwrap().to_unix_seconds()); @@ -45,7 +46,7 @@ fn test_packet() { #T2#1.2#3400005345 ##"#; - println!("{:#?}", parse_packet(inp)); + println!("{:#?}", parse_packet(inp.as_bytes())); } #[test] diff --git a/src/web_server/old_device_sensor_api/qs_parser.rs b/src/web_server/old_device_sensor_api/qs_parser.rs index 0b4ca09..dd5f131 100644 --- a/src/web_server/old_device_sensor_api/qs_parser.rs +++ b/src/web_server/old_device_sensor_api/qs_parser.rs @@ -19,31 +19,29 @@ pub enum QSParserError { #[snafu(display("asd"))] SerdeQS { #[snafu(source(from(serde_qs::Error, convert_to_arc::)))] - source: Arc + source: Arc, }, #[snafu(display("asd"))] - Parsing { - context: String - }, + Parsing { context: String }, #[snafu(display("asd"))] FloatP { #[snafu(source)] - source: ParseFloatError + source: ParseFloatError, }, #[snafu(display("failed to parse into decimal"))] - DecimalParse { - source: rust_decimal::Error - }, + DecimalParse { source: rust_decimal::Error }, #[snafu(display("asd"))] NoMAC, } -impl From> for QSParserError { - fn from(value: Error<&str>) -> Self { - QSParserError::Parsing { context: format!("{:?}", value)} +impl From for QSParserError { + fn from(value: Error) -> Self { + QSParserError::Parsing { + context: format!("{:?}", value), + } } } @@ -66,7 +64,7 @@ pub fn qs_rest_to_values( for (key, value) in parsed { hashset.insert(SensorValue { mac: key, - value: Decimal::from_str(value.as_str()).context(DecimalParseSnafu{})?, + value: Decimal::from_str(value.as_str()).context(DecimalParseSnafu {})?, time: None, unit: None, @@ -82,7 +80,9 @@ pub fn parse_decimal_if_exists( key: &str, ) -> Result, QSParserError> { if let Some(unwrapped_value) = parsed.remove(key) { - Ok(Some(Decimal::from_str(unwrapped_value.as_str()).context(DecimalParseSnafu{})?)) + Ok(Some( + Decimal::from_str(unwrapped_value.as_str()).context(DecimalParseSnafu {})?, + )) } else { Ok(None) } @@ -93,17 +93,19 @@ pub fn parse_epoch_if_exists( key: &str, ) -> Result, QSParserError> { if let Some(unwrapped_value) = parsed.remove(key) { - Ok(Some(Epoch::from_unix_seconds(unwrapped_value.parse().context(FloatPSnafu{})?))) + Ok(Some(Epoch::from_unix_seconds( + unwrapped_value.parse().context(FloatPSnafu {})?, + ))) } else { Ok(None) } } pub async fn parse_nm_qs_format(input: &str) -> Result { - let mut parsed: HashMap = serde_qs::from_str(input).context(SerdeQSSnafu{})?; + let mut parsed: HashMap = serde_qs::from_str(input).context(SerdeQSSnafu {})?; - let (_, device_mac) = if let Some(id) = parsed.get("ID") { - parse_mac_address(id)? + let device_mac = if let Some(id) = parsed.get("ID") { + parse_mac_address(id.as_bytes())? } else { return Err(QSParserError::NoMAC); };