Skip to content

Commit 8ba37a1

Browse files
committed
fix tests
1 parent 0c1364e commit 8ba37a1

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

src/builder.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,7 @@ mod test {
240240
fn returns_an_error_for_nonexistant_object_paths() {
241241
let result = SdJwtBuilder::new(json!({})).unwrap().make_concealable("/email");
242242

243-
assert_eq!(
244-
result.unwrap_err(),
245-
Error::InvalidPath("email does not exist".to_string()),
246-
);
243+
assert_eq!(result.unwrap_err(), Error::InvalidPath("/email".to_string()),);
247244
}
248245

249246
#[test]
@@ -252,10 +249,7 @@ mod test {
252249
.unwrap()
253250
.make_concealable("/nationalities/0");
254251

255-
assert_eq!(
256-
result.unwrap_err(),
257-
Error::InvalidPath("nationalities/0 does not exist".to_string()),
258-
);
252+
assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/0".to_string()),);
259253
}
260254

261255
#[test]
@@ -266,10 +260,7 @@ mod test {
266260
.unwrap()
267261
.make_concealable("/nationalities/2");
268262

269-
assert_eq!(
270-
result.unwrap_err(),
271-
Error::InvalidPath("nationalities/2 does not exist".to_string()),
272-
);
263+
assert_eq!(result.unwrap_err(), Error::InvalidPath("/nationalities/2".to_string()),);
273264
}
274265
}
275266

@@ -284,10 +275,7 @@ mod test {
284275
.unwrap()
285276
.make_concealable("/address/region");
286277

287-
assert_eq!(
288-
result.unwrap_err(),
289-
Error::InvalidPath("address/region does not exist".to_string()),
290-
);
278+
assert_eq!(result.unwrap_err(), Error::InvalidPath("/address/region".to_string()),);
291279
}
292280

293281
#[test]
@@ -300,7 +288,7 @@ mod test {
300288

301289
assert_eq!(
302290
result.unwrap_err(),
303-
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
291+
Error::InvalidPath("/address/contact_person/2".to_string()),
304292
);
305293
}
306294

@@ -314,7 +302,7 @@ mod test {
314302

315303
assert_eq!(
316304
result.unwrap_err(),
317-
Error::InvalidPath("address/contact_person/2 does not exist".to_string()),
305+
Error::InvalidPath("/address/contact_person/2".to_string()),
318306
);
319307
}
320308
}

src/encoder.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,30 @@ impl<H: Hasher> SdObjectEncoder<H> {
7070

7171
let element_pointer = path
7272
.parse::<JsonPointer<_, _>>()
73-
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
73+
.map_err(|_| Error::InvalidPath(path.to_string()))?;
7474

7575
let mut parent_pointer = element_pointer.clone();
76-
let element_key = parent_pointer
77-
.pop()
78-
.ok_or(Error::InvalidPath("path does not contain any values".to_string()))?;
76+
let element_key = parent_pointer.pop().ok_or(Error::InvalidPath(path.to_string()))?;
7977

8078
let parent = parent_pointer
8179
.get(&self.object)
82-
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
80+
.map_err(|_| Error::InvalidPath(path.to_string()))?;
8381

8482
match parent {
8583
Value::Object(_) => {
8684
let parent = parent_pointer
8785
.get_mut(&mut self.object)
88-
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?
86+
.map_err(|_| Error::InvalidPath(path.to_string()))?
8987
.as_object_mut()
90-
.ok_or(Error::InvalidPath("path does not contain any values".to_string()))?;
88+
.ok_or(Error::InvalidPath(path.to_string()))?;
9189

9290
// Remove the value from the parent and create a disclosure for it.
9391
let disclosure = Disclosure::new(
9492
salt,
9593
Some(element_key.to_owned()),
9694
parent
9795
.remove(&element_key)
98-
.ok_or(Error::InvalidPath(format!("{} does not exist", element_key)))?,
96+
.ok_or(Error::InvalidPath(path.to_string()))?,
9997
);
10098

10199
// Hash the disclosure.
@@ -106,7 +104,9 @@ impl<H: Hasher> SdObjectEncoder<H> {
106104
Ok(disclosure)
107105
}
108106
Value::Array(_) => {
109-
let element = element_pointer.get_mut(&mut self.object).unwrap();
107+
let element = element_pointer
108+
.get_mut(&mut self.object)
109+
.map_err(|_| Error::InvalidPath(path.to_string()))?;
110110
let disclosure = Disclosure::new(salt, None, element.clone());
111111
let hash = self.hasher.encoded_digest(disclosure.as_str());
112112
let tripledot = json!({ARRAY_DIGEST_KEY: hash});
@@ -144,13 +144,13 @@ impl<H: Hasher> SdObjectEncoder<H> {
144144
}
145145

146146
fn add_decoy(&mut self, path: &str) -> Result<()> {
147-
let mut element_pointer = path
147+
let element_pointer = path
148148
.parse::<JsonPointer<_, _>>()
149-
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
149+
.map_err(|_| Error::InvalidPath(path.to_string()))?;
150150

151151
let value = element_pointer
152152
.get_mut(&mut self.object)
153-
.map_err(|err| Error::InvalidPath(format!("{:?}", err)))?;
153+
.map_err(|_| Error::InvalidPath(path.to_string()))?;
154154
if let Some(object) = value.as_object_mut() {
155155
let (_, hash) = Self::random_digest(&self.hasher, self.salt_size, false);
156156
Self::add_digest_to_object(object, hash)?;
@@ -161,10 +161,7 @@ impl<H: Hasher> SdObjectEncoder<H> {
161161
array.push(tripledot);
162162
Ok(())
163163
} else {
164-
Err(Error::InvalidPath(format!(
165-
"{:?} is neither an object nor an array",
166-
element_pointer.pop()
167-
)))
164+
Err(Error::InvalidPath(path.to_string()))
168165
}
169166
}
170167

0 commit comments

Comments
 (0)