diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-10-19 14:20:57 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-10-19 14:20:57 -0400 |
commit | 1ae91166016ec70a242c6dbb14c9ca5e77b1313f (patch) | |
tree | 22b417e24053330a5def802aa6ee02b164e2bdb6 /src/rust | |
parent | a5599fb71c51ddec47282164f71cfb06933096cc (diff) | |
parent | b91bc1babc521abe1497fd913e4fe3fb5a909612 (diff) | |
download | tor-1ae91166016ec70a242c6dbb14c9ca5e77b1313f.tar.gz tor-1ae91166016ec70a242c6dbb14c9ca5e77b1313f.zip |
Merge remote-tracking branch 'onionk/rust-protocommas1' into maint-0.3.5
Diffstat (limited to 'src/rust')
-rw-r--r-- | src/rust/protover/protoset.rs | 33 |
1 files changed, 16 insertions, 17 deletions
diff --git a/src/rust/protover/protoset.rs b/src/rust/protover/protoset.rs index 3bfa7815f6..aa8d243bad 100644 --- a/src/rust/protover/protoset.rs +++ b/src/rust/protover/protoset.rs @@ -352,23 +352,25 @@ impl FromStr for ProtoSet { /// assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("3-")); /// assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1-,4")); /// - /// // Things which would get parsed into an _empty_ `ProtoSet` are, - /// // however, legal, and result in an empty `ProtoSet`: + /// // An empty string is, however, legal, and results in an + /// // empty `ProtoSet`: /// assert_eq!(Ok(ProtoSet::default()), ProtoSet::from_str("")); - /// assert_eq!(Ok(ProtoSet::default()), ProtoSet::from_str(",,,")); /// # /// # Ok(protoset) /// # } /// # fn main() { do_test(); } // wrap the test so we can use the ? operator /// ``` fn from_str(version_string: &str) -> Result<Self, Self::Err> { + // If we were passed in an empty string, then return an empty ProtoSet. + if version_string.is_empty() { + return Ok(Self::default()); + } + let mut pairs: Vec<(Version, Version)> = Vec::new(); let pieces: ::std::str::Split<char> = version_string.split(','); for p in pieces { - if p.is_empty() { - continue; - } else if p.contains('-') { + if p.contains('-') { let mut pair = p.splitn(2, '-'); let low = pair.next().ok_or(ProtoverError::Unparseable)?; @@ -377,24 +379,14 @@ impl FromStr for ProtoSet { let lo: Version = low.parse().or(Err(ProtoverError::Unparseable))?; let hi: Version = high.parse().or(Err(ProtoverError::Unparseable))?; - if lo == u32::MAX || hi == u32::MAX { - return Err(ProtoverError::ExceedsMax); - } pairs.push((lo, hi)); } else { let v: u32 = p.parse().or(Err(ProtoverError::Unparseable))?; - if v == u32::MAX { - return Err(ProtoverError::ExceedsMax); - } pairs.push((v, v)); } } - // If we were passed in an empty string, or - // simply a comma, or a pile of commas, then return an empty ProtoSet. - if pairs.len() == 0 { - return Ok(ProtoSet::default()); - } + ProtoSet::from_slice(&pairs[..]) } } @@ -559,6 +551,13 @@ mod test { } #[test] + fn test_versions_from_str_commas() { + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str(",")); + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1,,2")); + assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("1,2,")); + } + + #[test] fn test_versions_from_str_hyphens() { assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("--1")); assert_eq!(Err(ProtoverError::Unparseable), ProtoSet::from_str("-1-2")); |