pub mod element;
pub mod error;
pub mod locate;
pub mod path;
pub mod read;
pub mod write;
use core::fmt;
use fhir_types::codec::Value;
const fn json_kind(value: &Value) -> &'static str {
match value {
Value::Null => "null",
Value::Bool(_) => "a boolean",
Value::Number(_) => "a number",
Value::String(_) => "a string",
Value::Array(_) => "an array",
Value::Object(_) => "an object",
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Occurrence(Vec<usize>);
impl Occurrence {
#[must_use]
pub fn new(indices: impl Into<Vec<usize>>) -> Self {
Self(indices.into())
}
#[must_use]
pub fn indices(&self) -> &[usize] {
&self.0
}
#[must_use]
pub fn depth(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
fn push(&mut self, index: usize) {
self.0.push(index);
}
}
impl fmt::Display for Occurrence {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut first = true;
for index in &self.0 {
if !first {
f.write_str(".")?;
}
write!(f, "{index}")?;
first = false;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::Occurrence;
#[test]
fn an_occurrence_renders_its_indices_in_path_order() {
assert_eq!(Occurrence::new([0, 2, 1]).to_string(), "0.2.1");
assert_eq!(Occurrence::default().to_string(), "");
assert_eq!(Occurrence::new([3]).depth(), 1);
assert!(Occurrence::default().is_empty());
}
}