Skip to main content

tabnas_dmog/
formats.rs

1//! The formats a translation reads and writes, as `dmog formats` lists
2//! them.
3
4use tabnas_alchemy::translate::{Render as PartRender, Root, Shape};
5use tabnas_transduce::Datum;
6
7use crate::translate::{self, Format};
8
9/// One format a translation reads and writes: one entry of `dmog formats`,
10/// as its package's translation parts describe it.
11#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct FormatInfo {
13    /// What [`TranslateOptions`](crate::TranslateOptions) names the format
14    /// by.
15    pub id: String,
16    /// What its documents read as, `tree` or `records`, in order of
17    /// preference.
18    pub reads: Vec<String>,
19    /// What its render writes from, `tree` or `records`.
20    pub writes: String,
21    /// The root its render needs: `object`, `array` or `any`.
22    pub root: String,
23    /// The tree its events carry, where it is not a plain one.
24    pub schema: Option<String>,
25    /// Why its documents are read whole, where they are.
26    pub whole: Option<String>,
27    /// The entry of its lift, from its events to the shape it reads as.
28    pub lift: Option<String>,
29    /// The entry of its embedding, from a plain tree into its schema.
30    pub embed: Option<String>,
31    /// The entry of its render, or `json` or `csv` for the renderer it
32    /// takes.
33    pub render: Option<String>,
34    /// The entry of its transducer part, which streams its documents.
35    pub transduce: Option<String>,
36    /// What a document written in it does not keep, a sentence each.
37    pub loss: Vec<String>,
38}
39
40impl FormatInfo {
41    fn of(format: &Format) -> FormatInfo {
42        let part = &format.part;
43        let shape = |shape: &Shape| {
44            match shape {
45                Shape::Tree => "tree",
46                Shape::Records => "records",
47            }
48            .to_string()
49        };
50        FormatInfo {
51            id: part.id.clone(),
52            reads: part.reads.iter().map(shape).collect(),
53            writes: shape(&part.writes),
54            root: match part.root {
55                Root::Object => "object",
56                Root::Array => "array",
57                Root::Any => "any",
58            }
59            .to_string(),
60            schema: part.schema.clone(),
61            whole: part.whole.clone(),
62            lift: part.lift.as_ref().map(|alc| alc.entry.clone()),
63            embed: part.embed.as_ref().map(|alc| alc.entry.clone()),
64            render: Some(match &part.render {
65                PartRender::Alc(alc) => alc.entry.clone(),
66                PartRender::Json => "json".to_string(),
67                PartRender::Csv => "csv".to_string(),
68            }),
69            transduce: format.transducer().map(|alc| alc.entry.clone()),
70            loss: part.loss.clone(),
71        }
72    }
73
74    fn to_datum(&self) -> Datum {
75        let text = |text: &str| Datum::String(text.into());
76        let maybe = |text: &Option<String>| {
77            text.as_deref()
78                .map_or(Datum::Null, |t| Datum::String(t.into()))
79        };
80        let texts = |texts: &[String]| Datum::Array(texts.iter().map(|t| text(t)).collect());
81        let fields = [
82            ("id", text(&self.id)),
83            ("reads", texts(&self.reads)),
84            ("writes", text(&self.writes)),
85            ("root", text(&self.root)),
86            ("schema", maybe(&self.schema)),
87            ("whole", maybe(&self.whole)),
88            ("lift", maybe(&self.lift)),
89            ("embed", maybe(&self.embed)),
90            ("render", maybe(&self.render)),
91            ("transduce", maybe(&self.transduce)),
92            ("loss", texts(&self.loss)),
93        ];
94        Datum::Object(
95            fields
96                .into_iter()
97                .map(|(key, value)| (key.into(), value))
98                .collect(),
99        )
100    }
101}
102
103/// Every format, by id.
104pub fn formats() -> Vec<FormatInfo> {
105    translate::formats().iter().map(FormatInfo::of).collect()
106}
107
108/// [`formats`] as the JSON array `dmog formats` prints, without its final
109/// line feed.
110pub fn formats_json() -> String {
111    Datum::Array(formats().iter().map(FormatInfo::to_datum).collect()).to_string()
112}
113
114/// The ids of the formats whose documents are parsed, in the order
115/// [`formats`] lists them: the formats a program's
116/// [`grammar`](crate::ProgramOptions::grammar) takes, as `dmog run
117/// --grammar` does. A format whose documents are read only as the tree its
118/// package makes of them, such as expr, is not among them, which
119/// [`FormatInfo`] does not say.
120pub fn parsed_formats() -> Vec<String> {
121    translate::formats()
122        .iter()
123        .filter(|format| format.parses())
124        .map(|format| format.id().to_string())
125        .collect()
126}