Skip to content

Commit

Permalink
Add bytes_len() to Params (#848)
Browse files Browse the repository at this point in the history
* add len() to Params

* Rename to `len_bytes()`

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update comment.

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Update types/src/params.rs

* Update types/src/params.rs

* Update types/src/params.rs

* Update types/src/params.rs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
  • Loading branch information
mwtian and niklasad1 authored Aug 12, 2022
1 parent 8622fac commit 4c6207a
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions types/src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ impl<'a> Params<'a> {
pub fn into_owned(self) -> Params<'static> {
Params(self.0.map(|s| Cow::owned(s.into_owned())))
}

/// Return the length of underlying JSON string in number of bytes.
pub fn len_bytes(&self) -> usize {
match self.0 {
Some(ref cow) => cow.len(),
None => 0,
}
}
}

/// An `Iterator`-like parser for a sequence of [`Params`].
Expand Down Expand Up @@ -463,8 +471,10 @@ mod test {
let none = Params::new(None);
assert!(none.sequence().next::<u64>().is_err());
assert!(none.parse::<Option<u64>>().is_ok());
assert_eq!(none.len_bytes(), 0);

let array_params = Params::new(Some("[1, 2, 3]"));
assert_eq!(array_params.len_bytes(), 9);
let arr: Result<[u64; 3], _> = array_params.parse();
assert!(arr.is_ok());

Expand All @@ -476,10 +486,12 @@ mod test {
assert!(seq.next::<u64>().is_err());

let array_one = Params::new(Some("[1]"));
assert_eq!(array_one.len_bytes(), 3);
let one: Result<u64, _> = array_one.one();
assert!(one.is_ok());

let object_params = Params::new(Some(r#"{"beef":99,"dinner":0}"#));
assert_eq!(object_params.len_bytes(), 22);
let obj: Result<JsonValue, _> = object_params.parse();
assert!(obj.is_ok());
}
Expand Down

0 comments on commit 4c6207a

Please sign in to comment.