use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub(in crate::types) struct KeyPath(String);
impl KeyPath {
pub(in crate::types) fn new(key: &str) -> Self {
Self(key.to_owned())
}
pub(in crate::types) fn item(mut self, index: usize) -> Self {
self.0.push('[');
self.0.push_str(&index.to_string());
self.0.push(']');
self
}
pub(in crate::types) fn key(mut self, key: &str) -> Self {
self.0.push('.');
self.0.push_str(key);
self
}
}
impl From<KeyPath> for String {
fn from(path: KeyPath) -> Self {
path.0
}
}
impl fmt::Display for KeyPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn key_path_new_is_the_key_itself() {
assert_eq!(KeyPath::new("render").to_string(), "render");
}
#[test]
fn key_path_item_appends_an_index() {
assert_eq!(KeyPath::new("exclude").item(2).to_string(), "exclude[2]");
}
#[test]
fn key_path_key_appends_a_child_key() {
assert_eq!(
KeyPath::new("hooks").item(1).key("run").to_string(),
"hooks[1].run"
);
}
#[test]
fn key_path_nests_lists_and_tables() {
let path = KeyPath::new("hooks").item(0).key("agents").item(3);
assert_eq!(String::from(path), "hooks[0].agents[3]");
}
}