Skip to content

Commit

Permalink
Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikpe committed May 2, 2024
1 parent 1c45e1d commit 07b73db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 18 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<String, String> {
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<String, String> {
let info_ = util::clean(info);
let mut iter = info_
.trim_matches(DELIMITER)
.split(DELIMITER)
.map(|v| v.to_string());
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 07b73db

Please sign in to comment.