From 192a8259bc45cf88db972a27654cc1355897195f Mon Sep 17 00:00:00 2001 From: "andre.mello" Date: Mon, 18 Mar 2024 09:55:02 +0000 Subject: [PATCH 1/3] fix pop_front --- src/pointer.rs | 74 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 17 deletions(-) diff --git a/src/pointer.rs b/src/pointer.rs index f3eed14..92c23e6 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -169,25 +169,21 @@ impl Pointer { } /// Removes and returns the first `Token` in the `Pointer` if it exists. pub fn pop_front(&mut self) -> Option { - if self.inner.is_empty() { - return None; - } - if self.count > 0 { + if !self.inner.is_empty() && self.count > 0 { self.count -= 1; - } - self.inner[1..] - .split_once('/') - .map_or(Some((&self.inner[1..], "")), Option::Some) - .map(|(f, b)| (f.to_owned(), b.to_owned())) - .map(|(front, back)| { - if !back.is_empty() { - self.inner = String::from("/") + &back; - } else { - self.inner = String::new() - } - front.into() - }) + if let Some((front, back)) = self.inner[1..].split_once('/') { + let front = Token::from_encoded(front); + self.inner = String::from("/") + back; + Some(front) + } else { + let token = Token::from_encoded(&self.inner[1..]); + self.inner.truncate(0); + Some(token) + } + } else { + None + } } /// Returns the number of tokens in the `Pointer`. pub fn count(&self) -> usize { @@ -1379,6 +1375,50 @@ mod tests { assert_eq!(ptr, ""); } + #[test] + fn pop_front_works_with_empty_strings() { + { + let mut ptr = Pointer::new(["bar", "", ""]); + + assert_eq!(ptr.tokens().count(), 3); + let mut token = ptr.pop_front(); + assert_eq!(token, Some(Token::from_encoded("bar"))); + assert_eq!(ptr.tokens().count(), 2); + token = ptr.pop_front(); + assert_eq!(token, Some(Token::from_encoded(""))); + assert_eq!(ptr.tokens().count(), 1); + token = ptr.pop_front(); + assert_eq!(token, Some(Token::from_encoded(""))); + assert_eq!(ptr.tokens().count(), 0); + assert_eq!(ptr, Pointer::root()); + } + { + let mut ptr = Pointer::root(); + assert_eq!(ptr.tokens().count(), 0); + ptr.push_back("".into()); + assert_eq!(ptr.tokens().count(), 1); + ptr.pop_back(); + assert_eq!(ptr.tokens().count(), 0); + } + { + let mut ptr = Pointer::root(); + let input = ["", "", "", "foo", "", "bar", "baz", ""]; + for (idx, s) in input.iter().enumerate() { + assert_eq!(ptr.tokens().count(), idx); + ptr.push_back(s.into()); + } + assert_eq!(ptr.tokens().count(), input.len()); + for (idx, s) in input.iter().enumerate() { + assert_eq!(ptr.tokens().count(), 8 - idx); + assert_eq!(ptr.front().unwrap().as_str(), *s); + assert_eq!(ptr.pop_front().unwrap().as_str(), *s); + } + assert_eq!(ptr.tokens().count(), 0); + assert!(ptr.back().is_none()); + assert!(ptr.pop_front().is_none()); + } + } + #[test] fn test_formatting() { assert_eq!(Pointer::new(["foo", "bar"]), "/foo/bar"); From 0094b72fcdf0422e93c912f659fe68f31f5b7de0 Mon Sep 17 00:00:00 2001 From: "andre.mello" Date: Mon, 18 Mar 2024 10:05:23 +0000 Subject: [PATCH 2/3] update front --- src/pointer.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/pointer.rs b/src/pointer.rs index 92c23e6..d6c5a0e 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -227,8 +227,11 @@ impl Pointer { } self.inner[1..] .split_once('/') - .map_or(Some((&self.inner[1..], "")), Option::Some) - .map(|(front, _)| Token::from_encoded(front)) + .map_or_else( + || Token::from_encoded(&self.inner[1..]), + |(front, _)| Token::from_encoded(front), + ) + .into() } /// Returns the first `Token` in the `Pointer`. /// @@ -1414,7 +1417,7 @@ mod tests { assert_eq!(ptr.pop_front().unwrap().as_str(), *s); } assert_eq!(ptr.tokens().count(), 0); - assert!(ptr.back().is_none()); + assert!(ptr.front().is_none()); assert!(ptr.pop_front().is_none()); } } From dcb50f94dadcc49a897ef9a9f587d8ff24961a98 Mon Sep 17 00:00:00 2001 From: "andre.mello" Date: Mon, 18 Mar 2024 10:43:11 +0000 Subject: [PATCH 3/3] simplification --- src/pointer.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/pointer.rs b/src/pointer.rs index d6c5a0e..e312836 100644 --- a/src/pointer.rs +++ b/src/pointer.rs @@ -169,21 +169,19 @@ impl Pointer { } /// Removes and returns the first `Token` in the `Pointer` if it exists. pub fn pop_front(&mut self) -> Option { - if !self.inner.is_empty() && self.count > 0 { + (!self.inner.is_empty() && self.count > 0).then(|| { self.count -= 1; if let Some((front, back)) = self.inner[1..].split_once('/') { let front = Token::from_encoded(front); self.inner = String::from("/") + back; - Some(front) + front } else { let token = Token::from_encoded(&self.inner[1..]); self.inner.truncate(0); - Some(token) + token } - } else { - None - } + }) } /// Returns the number of tokens in the `Pointer`. pub fn count(&self) -> usize {