pub struct Key { /* private fields */ }Expand description
A configuration key path.
Keys represent paths to values in configuration files using a dot-separated notation with support for array indices and attributes.
§Examples
use bare_config::key::Key;
use std::str::FromStr;
let key = Key::from_str(".database.host").unwrap();
assert_eq!(key.segments().len(), 2);Implementations§
Source§impl Key
impl Key
Sourcepub fn from_str(s: &str) -> ConfigResult<Self>
pub fn from_str(s: &str) -> ConfigResult<Self>
Parse a key from a string path.
§Errors
Returns ConfigError::InvalidKey if the key format is invalid.
Sourcepub const fn from_segments(segments: Vec<KeySegment>) -> Self
pub const fn from_segments(segments: Vec<KeySegment>) -> Self
Creates a key from a vector of segments.
Sourcepub fn segments(&self) -> &[KeySegment]
pub fn segments(&self) -> &[KeySegment]
Returns the segments that make up this key.
Sourcepub fn parent(&self) -> Option<Self>
pub fn parent(&self) -> Option<Self>
Returns the parent key (all segments except the last).
Returns None if this key has only one segment.
Sourcepub fn last_segment(&self) -> Option<&KeySegment>
pub fn last_segment(&self) -> Option<&KeySegment>
Returns the last segment of this key.
Sourcepub fn first_segment(&self) -> Option<&KeySegment>
pub fn first_segment(&self) -> Option<&KeySegment>
Returns the first segment of this key.
Sourcepub fn append(self, segment: KeySegment) -> Self
pub fn append(self, segment: KeySegment) -> Self
Appends a segment to the end of this key.
Sourcepub fn to_key_string(&self) -> String
pub fn to_key_string(&self) -> String
Converts this key back to a string representation.
Sourcepub fn traverse<F, T>(&self, f: F) -> ConfigResult<T>
pub fn traverse<F, T>(&self, f: F) -> ConfigResult<T>
Traverse the key segments with a function.
§Errors
Returns ConfigError::InvalidKey if the key is empty.
Sourcepub fn walk<F>(&self, f: F) -> ConfigResult<()>
pub fn walk<F>(&self, f: F) -> ConfigResult<()>
Walk through all segments with a function.
§Errors
Returns ConfigError::InvalidKey if any segment traversal fails.
Sourcepub fn head(&self, n: usize) -> Option<Self>
pub fn head(&self, n: usize) -> Option<Self>
Returns the first n segments of this key.
Returns None if n is 0 or greater than the number of segments.
Sourcepub fn tail(&self, n: usize) -> Option<Self>
pub fn tail(&self, n: usize) -> Option<Self>
Returns the last n segments of this key.
Returns None if n is 0 or greater than the number of segments.
Sourcepub fn child(&self, segment: KeySegment) -> Self
pub fn child(&self, segment: KeySegment) -> Self
Returns a new key with the given segment appended.
Trait Implementations§
Source§impl From<Vec<KeySegment>> for Key
impl From<Vec<KeySegment>> for Key
Source§fn from(segments: Vec<KeySegment>) -> Self
fn from(segments: Vec<KeySegment>) -> Self
impl Eq for Key
impl StructuralPartialEq for Key
Auto Trait Implementations§
impl Freeze for Key
impl RefUnwindSafe for Key
impl Send for Key
impl Sync for Key
impl Unpin for Key
impl UnsafeUnpin for Key
impl UnwindSafe for Key
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.