use std::fmt;
use std::sync::Arc;
use super::reference::{WorkRef, words};
pub trait DocumentSchema: Send + Sync + 'static {
fn parse(&self, content: &str) -> ParsedDocument;
}
#[derive(Clone)]
pub struct AnyDocumentSchema(Arc<dyn DocumentSchema>);
impl AnyDocumentSchema {
#[must_use]
pub fn new(schema: impl DocumentSchema) -> Self {
Self(Arc::new(schema))
}
#[must_use]
pub fn parse(&self, content: &str) -> ParsedDocument {
self.0.parse(content)
}
}
impl fmt::Debug for AnyDocumentSchema {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("AnyDocumentSchema").finish_non_exhaustive()
}
}
#[derive(Debug, Clone)]
pub struct DocumentSpec {
file: String,
schema: AnyDocumentSchema,
}
impl DocumentSpec {
#[must_use]
pub fn new(file: impl Into<String>, schema: impl DocumentSchema) -> Self {
Self {
file: file.into(),
schema: AnyDocumentSchema::new(schema),
}
}
#[must_use]
pub fn file(&self) -> &str {
&self.file
}
#[must_use]
pub const fn schema(&self) -> &AnyDocumentSchema {
&self.schema
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DocumentRow {
line: usize,
text: String,
open: bool,
references: Vec<WorkRef>,
blocker: Option<String>,
}
impl DocumentRow {
#[must_use]
pub fn new(line: usize, text: impl Into<String>) -> Self {
Self {
line,
text: text.into(),
open: false,
references: Vec::new(),
blocker: None,
}
}
#[must_use]
pub const fn open(mut self) -> Self {
self.open = true;
self
}
#[must_use]
pub fn naming(mut self, references: impl IntoIterator<Item = WorkRef>) -> Self {
self.references = references.into_iter().collect();
self
}
#[must_use]
pub fn blocked_on(mut self, blocker: impl Into<String>) -> Self {
self.blocker = Some(blocker.into());
self
}
#[must_use]
pub const fn line(&self) -> usize {
self.line
}
#[must_use]
pub fn text(&self) -> &str {
&self.text
}
#[must_use]
pub fn summary(&self) -> &str {
self.text.lines().next().unwrap_or_default()
}
#[must_use]
pub const fn is_open(&self) -> bool {
self.open
}
#[must_use]
pub fn references(&self) -> &[WorkRef] {
&self.references
}
#[must_use]
pub fn blocker(&self) -> Option<&str> {
self.blocker.as_deref()
}
#[must_use]
pub fn names(&self, id: &str) -> bool {
words(&self.text).any(|word| word == id)
}
fn names_reference(&self, reference: &WorkRef) -> bool {
self.references.iter().any(|named| named.matches(reference))
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedDocument {
rows: Vec<DocumentRow>,
roots: Vec<WorkRef>,
problems: Vec<String>,
}
impl ParsedDocument {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push_row(&mut self, row: DocumentRow) {
self.rows.push(row);
}
pub fn push_root(&mut self, root: WorkRef) {
if !self.roots.contains(&root) {
self.roots.push(root);
}
}
pub fn push_problem(&mut self, problem: impl Into<String>) {
self.problems.push(problem.into());
}
#[must_use]
pub fn rows(&self) -> &[DocumentRow] {
&self.rows
}
#[must_use]
pub fn roots(&self) -> &[WorkRef] {
&self.roots
}
#[must_use]
pub fn problems(&self) -> &[String] {
&self.problems
}
pub(crate) fn rows_naming<'a>(
&'a self,
reference: &'a WorkRef,
) -> impl Iterator<Item = &'a DocumentRow> {
self.rows
.iter()
.filter(move |row| row.names_reference(reference))
}
pub(crate) fn rows_naming_id<'a>(
&'a self,
id: &'a str,
) -> impl Iterator<Item = &'a DocumentRow> {
self.rows.iter().filter(move |row| row.names(id))
}
}