use std::str::FromStr;
use std::fmt;
use std::collections::{HashMap, HashSet};
+use std::ops::Range;
use std::string::String;
/// The first version of Tor that included "proto" entries in its descriptors.
}
/// Translates a vector representation of a protocol list into a HashMap
-fn parse_protocols(
- protocols: &[&str],
+fn parse_protocols<'a, P: Iterator<Item = &'a str>>(
+ protocols: P,
) -> Result<HashMap<Proto, HashSet<u32>>, &'static str> {
let mut parsed = HashMap::new();
fn parse_protocols_from_string<'a>(
protocol_string: &'a str,
) -> Result<HashMap<Proto, HashSet<u32>>, &'static str> {
- let protocols: &[&'a str] =
- &protocol_string.split(" ").collect::<Vec<&'a str>>()[..];
-
- parse_protocols(protocols)
+ parse_protocols(protocol_string.split(" "))
}
/// Translates supported tor versions from a string into a HashMap, which is
/// of the error.
///
fn tor_supported() -> Result<HashMap<Proto, HashSet<u32>>, &'static str> {
- parse_protocols(&SUPPORTED_PROTOCOLS)
+ parse_protocols(SUPPORTED_PROTOCOLS.iter().map(|n| *n))
}
/// Get the unique version numbers supported by a subprotocol.
/// * the specified range is empty
/// * the version range does not contain both a valid lower and upper bound.
///
-fn expand_version_range(range: &str) -> Result<Vec<u32>, &'static str> {
+fn expand_version_range(range: &str) -> Result<Range<u32>, &'static str> {
if range.is_empty() {
return Err("version string empty");
}
))?;
// We can use inclusive range syntax when it becomes stable.
- Ok((lower..higher + 1).collect())
+ Ok(lower..higher + 1)
}
/// Checks to see if there is a continuous range of integers, starting at the
fn parse_protocols_from_string_with_no_validation<'a>(
protocol_string: &'a str,
) -> Result<HashMap<String, HashSet<u32>>, &'static str> {
- let protocols = &protocol_string.split(" ").collect::<Vec<&'a str>>()[..];
-
let mut parsed: HashMap<String, HashSet<u32>> = HashMap::new();
- for subproto in protocols {
+ for subproto in protocol_string.split(" ") {
let mut parts = subproto.splitn(2, "=");
let name = match parts.next() {
use super::expand_version_range;
assert_eq!(Err("version string empty"), expand_version_range(""));
- assert_eq!(Ok(vec![1, 2]), expand_version_range("1-2"));
- assert_eq!(Ok(vec![1, 2, 3, 4]), expand_version_range("1-4"));
+ assert_eq!(Ok(1..3), expand_version_range("1-2"));
+ assert_eq!(Ok(1..5), expand_version_range("1-4"));
assert_eq!(
Err("cannot parse protocol range lower bound"),
expand_version_range("a")