yog-data 0.10.2

Yog Data — the one data model, text syntax, binary form and schema system of every Yog format.
Documentation
//! The model: a tree of typed values, like NBT.

use std::fmt;

/// A Yog Data value.
///
/// The text form has no syntax of its own for the typed values (`Ref`,
/// `Tag`, `Res`, `Text`, `Image`, `Color`): they're written as strings
/// (`"@item:minecraft:stick"`), so every file stays JSON, and the schema of
/// a field says which type its string is. The binary form keeps the type.
#[derive(Debug, Clone, PartialEq, Default)]
pub enum Value {
    #[default]
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    Str(String),
    List(Vec<Value>),
    Map(Map),
    /// `@item:minecraft:stick` — content of some kind, by id.
    Ref(Ref),
    /// `@tag:item:minecraft:planks` — a tag of content of some kind.
    Tag(Ref),
    /// `@Strings.title` — a mod resource (`.yres` entry).
    Res(Res),
    /// `@lang:gui.done` — a translation key of the game's or a mod's lang files.
    Lang(String),
    /// Yog text: `{b|bold} and {#8d6acc|color}`.
    Text(String),
    /// An image source: `sprite:button`, `mymod:gui/logo`, `item:minecraft:stick`.
    Image(String),
    /// `0xAARRGGBB`.
    Color(u32),
    /// Bytes as they are — a picture, a sound — written `@bytes:<base64>` as text.
    Bytes(Vec<u8>),
}

/// A reference to content: its kind (`item`, `block`, `entity`, `recipe`,
/// `entry`, `category`, or a mod's own) and id (`minecraft:stick`,
/// `minecraft:furnace[facing=north]`).
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Ref {
    pub kind: String,
    pub id: String,
}

impl Ref {
    pub fn new(kind: impl Into<String>, id: impl Into<String>) -> Self {
        Self {
            kind: kind.into(),
            id: id.into(),
        }
    }

    /// The namespace of the id (`minecraft` for an id without one).
    pub fn namespace(&self) -> &str {
        yog_ids::split(&self.id).0
    }
}

/// A mod resource: `@<set>.<key>` — `@Strings.screen.title` is set
/// `Strings`, key `screen.title`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Res {
    pub set: String,
    pub key: String,
}

impl Res {
    /// `Set.key`: what the resources list it by.
    pub fn name(&self) -> String {
        format!("{}.{}", self.set, self.key)
    }
}

impl std::fmt::Display for Res {
    /// `@Set.key`, as files write it.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}{}.{}", yog_ids::sep::TYPED, self.set, self.key)
    }
}

pub use super::map::Map;

impl Value {
    /// The type's name, for messages: `"a string"`, `"a list"`, ...
    pub fn kind_name(&self) -> &'static str {
        match self {
            Value::Null => "null",
            Value::Bool(_) => "true/false",
            Value::Int(_) => "an integer",
            Value::Float(_) => "a number",
            Value::Str(_) => "a string",
            Value::List(_) => "a list",
            Value::Map(_) => "an object",
            Value::Ref(_) => "a reference",
            Value::Tag(_) => "a tag",
            Value::Res(_) => "a resource",
            Value::Lang(_) => "a translation key",
            Value::Text(_) => "a text",
            Value::Image(_) => "an image",
            Value::Color(_) => "a color",
            Value::Bytes(_) => "bytes",
        }
    }

    pub fn is_null(&self) -> bool {
        matches!(self, Value::Null)
    }

    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Bool(b) => Some(*b),
            _ => None,
        }
    }

    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Value::Int(n) => Some(*n),
            Value::Float(f) if f.fract() == 0.0 && f.abs() < 9.0e15 => Some(*f as i64),
            _ => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Int(n) => Some(*n as f64),
            Value::Float(f) => Some(*f),
            _ => None,
        }
    }

    /// The string of a `Str` (and of the string-like typed values: `Text`, `Image`).
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::Str(s) | Value::Text(s) | Value::Image(s) => Some(s),
            _ => None,
        }
    }

    pub fn as_list(&self) -> Option<&[Value]> {
        match self {
            Value::List(l) => Some(l),
            _ => None,
        }
    }

    pub fn as_map(&self) -> Option<&Map> {
        match self {
            Value::Map(m) => Some(m),
            _ => None,
        }
    }

    /// `self[key]` of a map, `None` for anything else.
    pub fn get(&self, key: &str) -> Option<&Value> {
        self.as_map()?.get(key)
    }

    /// The value at `path`.
    pub fn at(&self, path: &Path) -> Option<&Value> {
        let mut v = self;
        for seg in path.segments() {
            v = match (seg, v) {
                (Seg::Key(k), Value::Map(m)) => m.get(k)?,
                (Seg::Index(i), Value::List(l)) => l.get(*i)?,
                _ => return None,
            };
        }
        Some(v)
    }

    /// The value `segs` lead to, to change.
    pub fn at_mut(&mut self, segs: &[Seg]) -> Option<&mut Value> {
        let mut v = self;
        for seg in segs {
            v = match (seg, v) {
                (Seg::Key(k), Value::Map(m)) => m.get_mut(k)?,
                (Seg::Index(i), Value::List(l)) => l.get_mut(*i)?,
                _ => return None,
            };
        }
        Some(v)
    }
}

impl From<bool> for Value {
    fn from(b: bool) -> Self {
        Value::Bool(b)
    }
}
impl From<i64> for Value {
    fn from(n: i64) -> Self {
        Value::Int(n)
    }
}
impl From<f64> for Value {
    fn from(f: f64) -> Self {
        Value::Float(f)
    }
}
impl From<&str> for Value {
    fn from(s: &str) -> Self {
        Value::Str(s.to_string())
    }
}
impl From<String> for Value {
    fn from(s: String) -> Self {
        Value::Str(s)
    }
}
impl From<Vec<Value>> for Value {
    fn from(l: Vec<Value>) -> Self {
        Value::List(l)
    }
}
impl From<Map> for Value {
    fn from(m: Map) -> Self {
        Value::Map(m)
    }
}

/// One step of a [`Path`].
#[derive(Debug, Clone, PartialEq, Eq, Hash, crate::Schema)]
#[data(tag = "type")]
pub enum Seg {
    Key(String),
    Index(usize),
}

/// Where a value is in a document: `entries[2].pages[0].item`.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct Path(Vec<Seg>);

impl Path {
    pub fn root() -> Self {
        Self(Vec::new())
    }

    pub fn segments(&self) -> &[Seg] {
        &self.0
    }

    pub fn is_root(&self) -> bool {
        self.0.is_empty()
    }

    pub fn push(&mut self, seg: Seg) {
        self.0.push(seg);
    }

    pub fn pop(&mut self) {
        self.0.pop();
    }

    pub fn key(&self, key: &str) -> Path {
        let mut p = self.clone();
        p.0.push(Seg::Key(key.to_string()));
        p
    }

    /// `rest` under this path.
    pub fn then(&self, rest: &Path) -> Path {
        let mut p = self.clone();
        p.0.extend(rest.0.iter().cloned());
        p
    }

    pub fn index(&self, i: usize) -> Path {
        let mut p = self.clone();
        p.0.push(Seg::Index(i));
        p
    }
}

/// A path as its steps.
impl crate::Schema for Path {
    fn shape() -> crate::Shape {
        Vec::<Seg>::shape()
    }
    fn read(v: &crate::ValueRef<'_>, cx: &mut crate::Reader) -> Option<Self> {
        Vec::<Seg>::read(v, cx).map(Path)
    }
    fn write(&self) -> Value {
        self.0.write()
    }
}

impl fmt::Display for Path {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.0.is_empty() {
            return f.write_str("(root)");
        }
        for (i, seg) in self.0.iter().enumerate() {
            match seg {
                Seg::Key(k) if is_plain_key(k) => {
                    if i > 0 {
                        f.write_str(".")?;
                    }
                    f.write_str(k)?;
                }
                Seg::Key(k) => write!(f, "[{k:?}]")?,
                Seg::Index(n) => write!(f, "[{n}]")?,
            }
        }
        Ok(())
    }
}

fn is_plain_key(k: &str) -> bool {
    let mut chars = k.chars();
    chars
        .next()
        .is_some_and(|c| c.is_ascii_alphabetic() || c == '_' || c == '$')
        && chars.all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '$')
}