From 8ba37a16620b3e1ec37e2b940d60ff2b352c2349 Mon Sep 17 00:00:00 2001 From: Enrico Marconi Date: Mon, 23 Sep 2024 11:45:56 +0200 Subject: [PATCH] fix tests --- src/builder.rs | 24 ++++++------------------ src/encoder.rs | 29 +++++++++++++---------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 232d28f..a6f6e2c 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -240,10 +240,7 @@ mod test { fn returns_an_error_for_nonexistant_object_paths() { let result = SdJwtBuilder::new(json!({})).unwrap().make_concealable("/email"); - assert_eq!( - result.unwrap_err(), - Error::InvalidPath("email does not exist".to_string()), - ); + assert_eq!(result.unwrap_err(), Error::InvalidPath("/email".to_string()),); } #[test] @@ -252,10 +249,7 @@ mod test { .unwrap() .make_concealable("/nationalities/0"); - assert_eq!( - result.unwrap_err(), - Error::InvalidPath("nationalities/0 does not exist".to_string()), - ); + assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/0".to_string()),); } #[test] @@ -266,10 +260,7 @@ mod test { .unwrap() .make_concealable("/nationalities/2"); - assert_eq!( - result.unwrap_err(), - Error::InvalidPath("nationalities/2 does not exist".to_string()), - ); + assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/2".to_string()),); } } @@ -284,10 +275,7 @@ mod test { .unwrap() .make_concealable("/address/region"); - assert_eq!( - result.unwrap_err(), - Error::InvalidPath("address/region does not exist".to_string()), - ); + assert_eq!(result.unwrap_err(), Error::InvalidPath("/address/region".to_string()),); } #[test] @@ -300,7 +288,7 @@ mod test { assert_eq!( result.unwrap_err(), - Error::InvalidPath("address/contact_person/2 does not exist".to_string()), + Error::InvalidPath("/address/contact_person/2".to_string()), ); } @@ -314,7 +302,7 @@ mod test { assert_eq!( result.unwrap_err(), - Error::InvalidPath("address/contact_person/2 does not exist".to_string()), + Error::InvalidPath("/address/contact_person/2".to_string()), ); } } diff --git a/src/encoder.rs b/src/encoder.rs index 84c1b11..e1318b7 100644 --- a/src/encoder.rs +++ b/src/encoder.rs @@ -70,24 +70,22 @@ impl SdObjectEncoder { let element_pointer = path .parse::>() - .map_err(|err| Error::InvalidPath(format!("{:?}", err)))?; + .map_err(|_| Error::InvalidPath(path.to_string()))?; let mut parent_pointer = element_pointer.clone(); - let element_key = parent_pointer - .pop() - .ok_or(Error::InvalidPath("path does not contain any values".to_string()))?; + let element_key = parent_pointer.pop().ok_or(Error::InvalidPath(path.to_string()))?; let parent = parent_pointer .get(&self.object) - .map_err(|err| Error::InvalidPath(format!("{:?}", err)))?; + .map_err(|_| Error::InvalidPath(path.to_string()))?; match parent { Value::Object(_) => { let parent = parent_pointer .get_mut(&mut self.object) - .map_err(|err| Error::InvalidPath(format!("{:?}", err)))? + .map_err(|_| Error::InvalidPath(path.to_string()))? .as_object_mut() - .ok_or(Error::InvalidPath("path does not contain any values".to_string()))?; + .ok_or(Error::InvalidPath(path.to_string()))?; // Remove the value from the parent and create a disclosure for it. let disclosure = Disclosure::new( @@ -95,7 +93,7 @@ impl SdObjectEncoder { Some(element_key.to_owned()), parent .remove(&element_key) - .ok_or(Error::InvalidPath(format!("{} does not exist", element_key)))?, + .ok_or(Error::InvalidPath(path.to_string()))?, ); // Hash the disclosure. @@ -106,7 +104,9 @@ impl SdObjectEncoder { Ok(disclosure) } Value::Array(_) => { - let element = element_pointer.get_mut(&mut self.object).unwrap(); + let element = element_pointer + .get_mut(&mut self.object) + .map_err(|_| Error::InvalidPath(path.to_string()))?; let disclosure = Disclosure::new(salt, None, element.clone()); let hash = self.hasher.encoded_digest(disclosure.as_str()); let tripledot = json!({ARRAY_DIGEST_KEY: hash}); @@ -144,13 +144,13 @@ impl SdObjectEncoder { } fn add_decoy(&mut self, path: &str) -> Result<()> { - let mut element_pointer = path + let element_pointer = path .parse::>() - .map_err(|err| Error::InvalidPath(format!("{:?}", err)))?; + .map_err(|_| Error::InvalidPath(path.to_string()))?; let value = element_pointer .get_mut(&mut self.object) - .map_err(|err| Error::InvalidPath(format!("{:?}", err)))?; + .map_err(|_| Error::InvalidPath(path.to_string()))?; if let Some(object) = value.as_object_mut() { let (_, hash) = Self::random_digest(&self.hasher, self.salt_size, false); Self::add_digest_to_object(object, hash)?; @@ -161,10 +161,7 @@ impl SdObjectEncoder { array.push(tripledot); Ok(()) } else { - Err(Error::InvalidPath(format!( - "{:?} is neither an object nor an array", - element_pointer.pop() - ))) + Err(Error::InvalidPath(path.to_string())) } }