use std::collections::BTreeMap;
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Document {
sections: BTreeMap<String, Section>,
}
impl Document {
pub fn new() -> Self {
Self {
sections: BTreeMap::new(),
}
}
pub(crate) fn ensure_section_mut(&mut self, name: &str) -> &mut Section {
self.sections
.entry(name.to_string())
.or_insert_with(|| Section::new(name))
}
pub fn section(&self, name: &str) -> Option<&Section> {
self.sections.get(name)
}
pub fn sections(&self) -> impl Iterator<Item = (&String, &Section)> + '_ {
self.sections.iter()
}
}
impl Default for Document {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Section {
pub name: String,
pub entries: Vec<Entry>,
}
impl Section {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
entries: Vec::new(),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct Entry {
pub key: String,
pub value: Value,
pub line: usize,
}
impl Entry {
pub fn new(key: impl Into<String>, value: Value, line: usize) -> Self {
Self {
key: key.into(),
value,
line,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub enum Value {
String(String),
Number(f64),
Bool(bool),
Array(Vec<Value>),
}
impl Value {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
}
}