diff --git a/README.md b/README.md index 32a971e..89ecd70 100644 --- a/README.md +++ b/README.md @@ -5,12 +5,12 @@ ## Usage ```rust -let info_str = r#"\maxfps\77\pm_ktjump\1\*version\MVDSV 0.36"#; +let info_str = r#"\maxfps\77\matchtag\kombat"#; let info = quake_infostring::to_hashmap(&info_str); -println!("{:?}", info.maxfps); // Some("77") -println!("{:?}", info.version); // Some("MVDSV 0.36") -println!("{:?}", info.admin); // None +assert_eq!(info.get("maxfps"), Some(&"77".to_string())); +assert_eq!(info.get("matchtag"), Some(&"kombat".to_string())); +assert_eq!(info.get("MISSING_KEY"), None); ``` ## See also diff --git a/src/lib.rs b/src/lib.rs index 7fc7503..ad094b9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,12 +1,25 @@ +//! # quake_infostring +//! Parse QuakeWorld info strings use std::collections::HashMap; mod util; -pub const DELIMITER: char = '\\'; - -pub fn to_hashmap(serverinfo: &str) -> HashMap { - let serverinfo_ = util::clean(serverinfo); - let mut iter = serverinfo_ +const DELIMITER: char = '\\'; + +/// Convert info string to HashMap +/// +/// # Examples +/// ``` +/// let info_str = r#"\maxfps\77\matchtag\kombat"#; +/// let info = quake_infostring::to_hashmap(&info_str); +/// +/// assert_eq!(info.get("maxfps"), Some(&"77".to_string())); +/// assert_eq!(info.get("matchtag"), Some(&"kombat".to_string())); +/// assert_eq!(info.get("MISSING_KEY"), None); +/// ``` +pub fn to_hashmap(info: &str) -> HashMap { + let info_ = util::clean(info); + let mut iter = info_ .trim_matches(DELIMITER) .split(DELIMITER) .map(|v| v.to_string()); @@ -35,15 +48,5 @@ mod tests { assert_eq!(result.get("maxfps"), Some(&"77".to_string())); assert_eq!(result.get("matchtag"), None); } - - // valid string - { - let result = to_hashmap(r#"\maxfps\77\matchtag\kombat\epoch\123456"#); - assert_eq!(result.get("maxfps"), Some(&"77".to_string())); - assert_eq!(result.get("matchtag"), Some(&"kombat".to_string())); - assert_eq!(result.get("epoch"), Some(&"123456".to_string())); - - assert_eq!(result.get("MISSING_KEY"), None); - } } }