1use tabnas_alchemy::translate::{Render as PartRender, Root, Shape};
5use tabnas_transduce::Datum;
6
7use crate::translate::{self, Format};
8
9#[derive(Clone, Debug, PartialEq, Eq)]
12pub struct FormatInfo {
13 pub id: String,
16 pub reads: Vec<String>,
19 pub writes: String,
21 pub root: String,
23 pub schema: Option<String>,
25 pub whole: Option<String>,
27 pub lift: Option<String>,
29 pub embed: Option<String>,
31 pub render: Option<String>,
34 pub transduce: Option<String>,
36 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
103pub fn formats() -> Vec<FormatInfo> {
105 translate::formats().iter().map(FormatInfo::of).collect()
106}
107
108pub fn formats_json() -> String {
111 Datum::Array(formats().iter().map(FormatInfo::to_datum).collect()).to_string()
112}
113
114pub 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}