use firestore::FirestoreSqlFieldName;
use std::fmt::{Display, Formatter};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FieldPath(String);
impl FieldPath {
pub fn field(&self, name: &str) -> Self {
let name = FirestoreSqlFieldName::from(name);
if self.0.is_empty() {
Self(name.to_string())
} else {
Self(format!("{}.{name}", self.0))
}
}
pub fn element(&self, index: usize) -> Self {
Self(format!("{}[{index}]", self.0))
}
}
impl Display for FieldPath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_path_quotes_each_name_and_marks_array_elements_by_position() {
let path = FieldPath::default()
.field("order")
.field("unit-price")
.element(2)
.field("it`s");
assert_eq!(path.to_string(), "`order`.`unit-price`[2].`it``s`");
}
}