Skip to content

Commit

Permalink
Updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
DariuszDepta committed Nov 22, 2024
1 parent 50af743 commit 776ba67
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! # KIVI format deserializer
mod loader;
mod model;

Expand Down
30 changes: 29 additions & 1 deletion src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! # Loader
//! # Implementation of KIVI deserialization functions
use crate::model::KeyValuePairs;
use std::path::Path;
Expand Down Expand Up @@ -75,6 +75,18 @@ macro_rules! clear_buffer {
}};
}

/// Loads key-value pairs from string in KIVI format.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\nc\nd\n");
/// for key in kvp.values() {
/// assert!(key == "b" || key == "d");
/// }
/// ```
pub fn load_from_string(input: &str) -> KeyValuePairs {
let mut output = KeyValuePairs::new();
let mut state = State::Key;
Expand Down Expand Up @@ -113,6 +125,22 @@ pub fn load_from_string(input: &str) -> KeyValuePairs {
output
}

/// Loads key-value pairs from KIVI file.
///
/// # Examples
///
/// ```
/// use std::io;
/// use kivi::load_from_file;
///
/// fn main() -> io::Result<()> {
/// let kvp = load_from_file("./tests/loading/data/properties.kivi")?;
/// let default_host = "0.0.0.0".to_string();
/// let host = kvp.get("host").unwrap_or(&default_host);
/// println!("host: {}", host);
/// Ok(())
/// }
/// ```
pub fn load_from_file<P: AsRef<Path>>(path: P) -> io::Result<KeyValuePairs> {
Ok(load_from_string(&fs::read_to_string(path)?))
}
67 changes: 66 additions & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,99 @@
//! # Key-value pairs
//! # Data model for key-value pairs
use std::collections::hash_map::{IntoIter, Keys, Values};
use std::collections::HashMap;

/// A struct representing key-value pairs deserialized from KIVI format.
#[derive(Debug, Clone, PartialEq)]
pub struct KeyValuePairs {
pub(crate) kv: HashMap<String, String>,
}

impl KeyValuePairs {
/// Creates an empty set of key-value pairs.
pub(crate) fn new() -> Self {
Self { kv: HashMap::new() }
}

/// Returns the value associated with the specified key.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\n");
/// assert_eq!("b", kvp.get("a").unwrap());
///
/// let kvp = load_from_string("");
/// assert_eq!(None, kvp.get("a"));
/// ```
pub fn get(&self, key: &str) -> Option<&String> {
self.kv.get(key)
}

/// Returns [true] when the set of key-value pairs is empty.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\n");
/// assert_eq!(false, kvp.is_empty());
///
/// let kvp = load_from_string("");
/// assert_eq!(true, kvp.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
self.kv.is_empty()
}

/// Returns the number of key-value pairs.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\n");
/// assert_eq!(1, kvp.len());
///
/// let kvp = load_from_string("");
/// assert_eq!(0, kvp.len());
/// ```
pub fn len(&self) -> usize {
self.kv.len()
}

/// Returns the iterator over the keys.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\nc\nd\n");
/// for key in kvp.keys() {
/// assert!(key == "a" || key == "c");
/// }
/// ```
pub fn keys(&self) -> Keys<'_, String, String> {
self.kv.keys()
}

/// Returns the iterator over the values.
///
/// # Examples
///
/// ```
/// use kivi::load_from_string;
///
/// let kvp = load_from_string("a\nb\nc\nd");
/// for key in kvp.values() {
/// assert!(key == "b" || key == "d");
/// }
/// ```
pub fn values(&self) -> Values<'_, String, String> {
self.kv.values()
}
Expand Down
2 changes: 2 additions & 0 deletions tests/loading/data/properties.kivi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
host
127.0.0.1

0 comments on commit 776ba67

Please sign in to comment.