fix: update parser api usages after refactoring

This commit is contained in:
DarkCat09 2025-01-10 22:06:23 +04:00
parent 264177e9c9
commit 71e3b90a0b
Signed by: DarkCat09
GPG key ID: BD3CE9B65916CD82
3 changed files with 27 additions and 24 deletions

View file

@ -52,8 +52,8 @@ impl Hash for SensorValue {
/// Функция-помощник для [MacAsArray], предназначенный для использования с [serde_with]. /// Функция-помощник для [MacAsArray], предназначенный для использования с [serde_with].
/// ///
/// Преобразует MAC-адрес. /// Преобразует MAC-адрес.
fn mac_as_array(value: &str) -> Result<[u8; 6], Error<&str>> { fn mac_as_array(value: &str) -> Result<[u8; 6], Error> {
Ok(parse_mac_address(value)?.1) parse_mac_address(value.as_bytes())
} }
serde_with::serde_conv!( serde_with::serde_conv!(

View file

@ -17,8 +17,9 @@ fn test_asd() {
#ALT#38 #ALT#38
## ##
"# "#
.trim(); .trim()
let packet = parse_packet(asd).unwrap().1; .as_bytes();
let packet = parse_packet(asd).unwrap();
assert!(serde_json::to_string_pretty(&packet) assert!(serde_json::to_string_pretty(&packet)
.unwrap() .unwrap()
.contains(r#""time": 1000000.0"#)); .contains(r#""time": 1000000.0"#));
@ -27,8 +28,8 @@ fn test_asd() {
#[test] #[test]
fn test_mac() { fn test_mac() {
assert_eq!( assert_eq!(
parse_mac_address("12-34-AA-12-55-AA").unwrap(), parse_mac_address(b"12-34-Aa-1255-fA").unwrap(),
("", [18, 52, 170, 18, 85, 170]) [18, 52, 170, 18, 85, 250],
); );
dbg!(Epoch::now().unwrap().to_unix_seconds()); dbg!(Epoch::now().unwrap().to_unix_seconds());
@ -45,7 +46,7 @@ fn test_packet() {
#T2#1.2#3400005345 #T2#1.2#3400005345
##"#; ##"#;
println!("{:#?}", parse_packet(inp)); println!("{:#?}", parse_packet(inp.as_bytes()));
} }
#[test] #[test]

View file

@ -19,31 +19,29 @@ pub enum QSParserError {
#[snafu(display("asd"))] #[snafu(display("asd"))]
SerdeQS { SerdeQS {
#[snafu(source(from(serde_qs::Error, convert_to_arc::<serde_qs::Error>)))] #[snafu(source(from(serde_qs::Error, convert_to_arc::<serde_qs::Error>)))]
source: Arc<serde_qs::Error> source: Arc<serde_qs::Error>,
}, },
#[snafu(display("asd"))] #[snafu(display("asd"))]
Parsing { Parsing { context: String },
context: String
},
#[snafu(display("asd"))] #[snafu(display("asd"))]
FloatP { FloatP {
#[snafu(source)] #[snafu(source)]
source: ParseFloatError source: ParseFloatError,
}, },
#[snafu(display("failed to parse into decimal"))] #[snafu(display("failed to parse into decimal"))]
DecimalParse { DecimalParse { source: rust_decimal::Error },
source: rust_decimal::Error
},
#[snafu(display("asd"))] #[snafu(display("asd"))]
NoMAC, NoMAC,
} }
impl From<Error<&str>> for QSParserError { impl From<Error> for QSParserError {
fn from(value: Error<&str>) -> Self { fn from(value: Error) -> Self {
QSParserError::Parsing { context: format!("{:?}", value)} QSParserError::Parsing {
context: format!("{:?}", value),
}
} }
} }
@ -66,7 +64,7 @@ pub fn qs_rest_to_values(
for (key, value) in parsed { for (key, value) in parsed {
hashset.insert(SensorValue { hashset.insert(SensorValue {
mac: key, mac: key,
value: Decimal::from_str(value.as_str()).context(DecimalParseSnafu{})?, value: Decimal::from_str(value.as_str()).context(DecimalParseSnafu {})?,
time: None, time: None,
unit: None, unit: None,
@ -82,7 +80,9 @@ pub fn parse_decimal_if_exists(
key: &str, key: &str,
) -> Result<Option<Decimal>, QSParserError> { ) -> Result<Option<Decimal>, QSParserError> {
if let Some(unwrapped_value) = parsed.remove(key) { 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 { } else {
Ok(None) Ok(None)
} }
@ -93,17 +93,19 @@ pub fn parse_epoch_if_exists(
key: &str, key: &str,
) -> Result<Option<Epoch>, QSParserError> { ) -> Result<Option<Epoch>, QSParserError> {
if let Some(unwrapped_value) = parsed.remove(key) { 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 { } else {
Ok(None) Ok(None)
} }
} }
pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSParserError> { pub async fn parse_nm_qs_format(input: &str) -> Result<NMDeviceDataPacket, QSParserError> {
let mut parsed: HashMap<String, String> = serde_qs::from_str(input).context(SerdeQSSnafu{})?; let mut parsed: HashMap<String, String> = serde_qs::from_str(input).context(SerdeQSSnafu {})?;
let (_, device_mac) = if let Some(id) = parsed.get("ID") { let device_mac = if let Some(id) = parsed.get("ID") {
parse_mac_address(id)? parse_mac_address(id.as_bytes())?
} else { } else {
return Err(QSParserError::NoMAC); return Err(QSParserError::NoMAC);
}; };