tabnas_dmog/lib.rs
1//! dmog (data mogrify) as a library: what the `dmog` command does, for a
2//! program to call.
3//!
4//! - **Translation.** [`translate()`] writes a document of any format
5//! [`formats()`] lists in any other, as it reads it: [`Translation`]
6//! compiles the route once, for many documents, and
7//! [`TranslateOptions`] holds what the command's options say.
8//! - **The formats.** [`formats()`] lists them as values, and
9//! [`formats_json`] as `dmog formats` prints them; [`parsed_formats`]
10//! names those whose documents a program can read as their parse.
11//! - **Programs.** [`Program`] compiles an alchemy program and runs it over
12//! a document, as `dmog run` does; [`canon`], [`format()`], [`check`],
13//! [`explain`] and [`explain_json`] answer what the commands of those
14//! names print.
15//!
16//! Input is a text, or a reader the run pulls from as it goes; output is a
17//! text, or a writer the run writes to as it goes. Every failure is a
18//! [`Fail`]: its [`Code`], its message, where it was found, and whether
19//! output had already been written. The defaults are the command's:
20//! [`default_limits`], the member a root is wrapped under, and the writer.
21//!
22//! A run compiles and evaluates on a thread of
23//! [`tabnas_alchemy::STACK_BYTES`] that the library starts, so it may be
24//! called from any thread. An [`Abort`] stops a run that is under way.
25//!
26//! ```
27//! use tabnas_dmog::{translate_str, Program, ProgramOptions, Source, TranslateOptions};
28//!
29//! let json = translate_str("a: 1\nb: [x, y]\n", TranslateOptions::new("yaml", "json"))?;
30//! assert_eq!(json, "{\"a\":1,\"b\":[\"x\",\"y\"]}\n");
31//!
32//! let names = Source::new(
33//! "names.alc",
34//! "def export [input]\n pipe input\n select (path each-index \"name\")\n join \",\"\n",
35//! );
36//! let program = Program::new(names, ProgramOptions::default())?;
37//! assert_eq!(program.run_str(r#"[{"name":"Ada"},{"name":"Alan"}]"#)?, "Ada,Alan");
38//! # Ok::<(), tabnas_dmog::Fail>(())
39//! ```
40//!
41//! The `dmog` command is this library's first user: `src/bin/dmog.rs`
42//! parses the command line, opens the files, and turns a failure into its
43//! exit status.
44
45mod formats;
46mod program;
47mod stack;
48mod translation;
49
50#[doc(hidden)]
51pub mod translate;
52
53pub use formats::{formats, formats_json, parsed_formats, FormatInfo};
54pub use program::{canon, check, explain, explain_json, format, Program, ProgramOptions, Render};
55pub use stack::Abort;
56pub use tabnas_alchemy::shared::{Code, Fail, Limits};
57pub use translation::{translate, translate_str, TranslateOptions, Translation};
58
59/// A program's text, and the name its failures cite it by.
60#[derive(Clone, Debug, PartialEq, Eq)]
61pub struct Source {
62 pub name: String,
63 pub text: String,
64}
65
66impl Source {
67 pub fn new(name: impl Into<String>, text: impl Into<String>) -> Source {
68 Source {
69 name: name.into(),
70 text: text.into(),
71 }
72 }
73}
74
75/// The limits a run has when none are given: the command's.
76pub fn default_limits() -> Limits {
77 Limits::default()
78}
79
80/// The README's examples, which `cargo test --doc` runs.
81#[cfg(doctest)]
82#[doc = include_str!("../README.md")]
83pub struct ReadmeDoctests;