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].
///
/// Преобразует 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!(

View file

@ -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]

View file

@ -19,31 +19,29 @@ pub enum QSParserError {
#[snafu(display("asd"))]
SerdeQS {
#[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"))]
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<Error<&str>> for QSParserError {
fn from(value: Error<&str>) -> Self {
QSParserError::Parsing { context: format!("{:?}", value)}
impl From<Error> 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<Option<Decimal>, 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<Option<Epoch>, 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<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") {
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);
};