tabnas-dmog 0.4.0

The dmog library and command (data mogrify): translates a document from any tabnas format into any other, streaming, and runs alchemy programs over JSON documents, composing tabnas-alchemy, tabnas-transduce and tabnas-render.
Documentation
//! The formats a translation reads and writes, as `dmog formats` lists
//! them.

use tabnas_alchemy::translate::{Render as PartRender, Root, Shape};
use tabnas_transduce::Datum;

use crate::translate::{self, Format};

/// One format a translation reads and writes: one entry of `dmog formats`,
/// as its package's translation parts describe it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FormatInfo {
    /// What [`TranslateOptions`](crate::TranslateOptions) names the format
    /// by.
    pub id: String,
    /// What its documents read as, `tree` or `records`, in order of
    /// preference.
    pub reads: Vec<String>,
    /// What its render writes from, `tree` or `records`.
    pub writes: String,
    /// The root its render needs: `object`, `array` or `any`.
    pub root: String,
    /// The tree its events carry, where it is not a plain one.
    pub schema: Option<String>,
    /// Why its documents are read whole, where they are.
    pub whole: Option<String>,
    /// The entry of its lift, from its events to the shape it reads as.
    pub lift: Option<String>,
    /// The entry of its embedding, from a plain tree into its schema.
    pub embed: Option<String>,
    /// The entry of its render, or `json` or `csv` for the renderer it
    /// takes.
    pub render: Option<String>,
    /// The entry of its transducer part, which streams its documents.
    pub transduce: Option<String>,
    /// What a document written in it does not keep, a sentence each.
    pub loss: Vec<String>,
}

impl FormatInfo {
    fn of(format: &Format) -> FormatInfo {
        let part = &format.part;
        let shape = |shape: &Shape| {
            match shape {
                Shape::Tree => "tree",
                Shape::Records => "records",
            }
            .to_string()
        };
        FormatInfo {
            id: part.id.clone(),
            reads: part.reads.iter().map(shape).collect(),
            writes: shape(&part.writes),
            root: match part.root {
                Root::Object => "object",
                Root::Array => "array",
                Root::Any => "any",
            }
            .to_string(),
            schema: part.schema.clone(),
            whole: part.whole.clone(),
            lift: part.lift.as_ref().map(|alc| alc.entry.clone()),
            embed: part.embed.as_ref().map(|alc| alc.entry.clone()),
            render: Some(match &part.render {
                PartRender::Alc(alc) => alc.entry.clone(),
                PartRender::Json => "json".to_string(),
                PartRender::Csv => "csv".to_string(),
            }),
            transduce: format.transducer().map(|alc| alc.entry.clone()),
            loss: part.loss.clone(),
        }
    }

    fn to_datum(&self) -> Datum {
        let text = |text: &str| Datum::String(text.into());
        let maybe = |text: &Option<String>| {
            text.as_deref()
                .map_or(Datum::Null, |t| Datum::String(t.into()))
        };
        let texts = |texts: &[String]| Datum::Array(texts.iter().map(|t| text(t)).collect());
        let fields = [
            ("id", text(&self.id)),
            ("reads", texts(&self.reads)),
            ("writes", text(&self.writes)),
            ("root", text(&self.root)),
            ("schema", maybe(&self.schema)),
            ("whole", maybe(&self.whole)),
            ("lift", maybe(&self.lift)),
            ("embed", maybe(&self.embed)),
            ("render", maybe(&self.render)),
            ("transduce", maybe(&self.transduce)),
            ("loss", texts(&self.loss)),
        ];
        Datum::Object(
            fields
                .into_iter()
                .map(|(key, value)| (key.into(), value))
                .collect(),
        )
    }
}

/// Every format, by id.
pub fn formats() -> Vec<FormatInfo> {
    translate::formats().iter().map(FormatInfo::of).collect()
}

/// [`formats`] as the JSON array `dmog formats` prints, without its final
/// line feed.
pub fn formats_json() -> String {
    Datum::Array(formats().iter().map(FormatInfo::to_datum).collect()).to_string()
}

/// The ids of the formats whose documents are parsed, in the order
/// [`formats`] lists them: the formats a program's
/// [`grammar`](crate::ProgramOptions::grammar) takes, as `dmog run
/// --grammar` does. A format whose documents are read only as the tree its
/// package makes of them, such as expr, is not among them, which
/// [`FormatInfo`] does not say.
pub fn parsed_formats() -> Vec<String> {
    translate::formats()
        .iter()
        .filter(|format| format.parses())
        .map(|format| format.id().to_string())
        .collect()
}