Skip to content

Commit

Permalink
use String::from_json and add a test for json
Browse files Browse the repository at this point in the history
  • Loading branch information
radumarias committed Aug 20, 2024
1 parent 590dbcf commit 0912262
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ impl From<VehicleCategoryCode> for Cbor {

impl crate::definitions::traits::FromJson for VehicleCategoryCode {
fn from_json(v: &serde_json::Value) -> Result<Self, FromJsonError> {
v.as_str()
.map(str::to_uppercase)
.map(|s| s.parse::<VehicleCategoryCode>())
.unwrap_or(Err(strum::ParseError::VariantNotFound))
.map_err(|_| FromJsonError::Missing)
String::from_json(v)?
.to_uppercase()
.parse::<VehicleCategoryCode>()
.map_err(|err| FromJsonError::Parsing(anyhow::Error::new(err)))
}
}

Expand Down Expand Up @@ -100,7 +99,7 @@ pub struct Code {

#[cfg(test)]
mod tests {
use crate::definitions::traits::ToCbor;
use crate::definitions::traits::{FromJson, ToCbor};

use super::VehicleCategoryCode;

Expand All @@ -112,5 +111,9 @@ mod tests {

let v = c.to_cbor();
assert_eq!(v, serde_cbor::Value::Text("A".to_string()));

let j = serde_json::json!("A");
let c = VehicleCategoryCode::from_json(&j).unwrap();
assert_eq!(c, VehicleCategoryCode::A);
}
}

0 comments on commit 0912262

Please sign in to comment.