silcrow 0.1.0

Deterministic CLI for a citable, checkable graph of laws in a git tree.
use std::path::PathBuf;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Location {
    pub path: PathBuf,
    pub line: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Target {
    Stable {
        id: String,
        letter: Option<String>,
    },
    #[allow(dead_code)]
    Display {
        note: String,
        section: u32,
        clause: Option<String>,
        letter: Option<char>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Clause {
    pub stable_id: Option<String>,
    pub letter: Option<String>,
    pub text: String,
    /// The lettered line plus the content attached under it (continuation, table, fence).
    pub body: String,
    /// For a row of a table that letters its rows, the rows of that table before its first
    /// lettered row, through which the row reads. Empty for an item.
    pub header: String,
    /// The last line of the clause and its attached body, 1-based.
    pub end: usize,
    pub struck: bool,
    /// Written `(x)?`, or under a draft law: it owes no marker and no marker may name it.
    pub draft: bool,
    pub loc: Location,
}

impl Clause {
    /// The text a clause pin is bound to: its body, after the header of its table.
    pub fn pin_body(&self) -> String {
        if self.header.is_empty() {
            self.body.clone()
        } else {
            format!("{}\n{}", self.header, self.body)
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Law {
    pub id: String,
    pub name: String,
    /// Content attached under the head before any clause or prose paragraph.
    pub body: String,
    /// The last line of the head and the content attached under it, 1-based.
    pub end: usize,
    /// The lines between the head and its last clause that neither holds: context, which binds
    /// nothing and no pin hashes.
    pub context: String,
    /// Ids this law carried before an `accept`, kept as empty anchors in the head.
    pub aliases: Vec<String>,
    pub loc: Location,
    pub clauses: Vec<Clause>,
    pub struck: bool,
    /// Written `§?`: it and every clause of it owe no marker.
    pub draft: bool,
    /// Written `§!`: a promise of the public documentation, which owes a documentation marker.
    pub public: bool,
}

impl Law {
    /// The text a law-grain pin is bound to.
    pub fn pin_text(&self) -> String {
        if self.body.is_empty() {
            self.name.clone()
        } else {
            format!("{}\n{}", self.name, self.body)
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum PointerKind {
    Cite,
    Implements,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Pointer {
    pub kind: PointerKind,
    /// The project path of a qualified reference, `<projectpath>:<id>`. `None` is the current project.
    pub project: Option<String>,
    pub name: String,
    pub target: Target,
    pub loc: Location,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FormError {
    pub loc: Location,
    pub message: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Lint {
    pub loc: Location,
    pub message: String,
}

/// A paragraph `**§ <projectpath>:<id>[.<letter>]**` that takes another project's law, or one of
/// its letters, as a duty of the current project.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Adoption {
    pub project: String,
    pub id: String,
    pub letter: Option<String>,
    pub loc: Location,
}

#[derive(Debug, Clone, Default)]
pub struct Scan {
    pub laws: Vec<Law>,
    pub pointers: Vec<Pointer>,
    /// `§ <id>[.<letter>]` named in the prose of a documentation file: a cite only `check` reads.
    pub mentions: Vec<Pointer>,
    pub adoptions: Vec<Adoption>,
    pub form_errors: Vec<FormError>,
    pub lints: Vec<Lint>,
    /// The files the `docs` list matches, relative to the project root.
    pub doc_files: Vec<PathBuf>,
}