Unverified Commit 4c6207a1 authored by mwtian's avatar mwtian Committed by GitHub
Browse files

Add `bytes_len()` to Params (#848)



* add len() to Params

* Rename to `len_bytes()`

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

* Update comment.

Co-authored-by: Niklas Adolfsson's avatarNiklas 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's avatarNiklas Adolfsson <niklasadolfsson1@gmail.com>
parent 8622facb
Pipeline #207728 passed with stages
in 5 minutes and 5 seconds
...@@ -141,6 +141,14 @@ impl<'a> Params<'a> { ...@@ -141,6 +141,14 @@ impl<'a> Params<'a> {
pub fn into_owned(self) -> Params<'static> { pub fn into_owned(self) -> Params<'static> {
Params(self.0.map(|s| Cow::owned(s.into_owned()))) 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`]. /// An `Iterator`-like parser for a sequence of [`Params`].
...@@ -463,8 +471,10 @@ mod test { ...@@ -463,8 +471,10 @@ mod test {
let none = Params::new(None); let none = Params::new(None);
assert!(none.sequence().next::<u64>().is_err()); assert!(none.sequence().next::<u64>().is_err());
assert!(none.parse::<Option<u64>>().is_ok()); assert!(none.parse::<Option<u64>>().is_ok());
assert_eq!(none.len_bytes(), 0);
let array_params = Params::new(Some("[1, 2, 3]")); let array_params = Params::new(Some("[1, 2, 3]"));
assert_eq!(array_params.len_bytes(), 9);
let arr: Result<[u64; 3], _> = array_params.parse(); let arr: Result<[u64; 3], _> = array_params.parse();
assert!(arr.is_ok()); assert!(arr.is_ok());
...@@ -476,10 +486,12 @@ mod test { ...@@ -476,10 +486,12 @@ mod test {
assert!(seq.next::<u64>().is_err()); assert!(seq.next::<u64>().is_err());
let array_one = Params::new(Some("[1]")); let array_one = Params::new(Some("[1]"));
assert_eq!(array_one.len_bytes(), 3);
let one: Result<u64, _> = array_one.one(); let one: Result<u64, _> = array_one.one();
assert!(one.is_ok()); assert!(one.is_ok());
let object_params = Params::new(Some(r#"{"beef":99,"dinner":0}"#)); 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(); let obj: Result<JsonValue, _> = object_params.parse();
assert!(obj.is_ok()); assert!(obj.is_ok());
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment