Skip to main content

jaq_fmts/
lib.rs

1//! Multi-format support for jaq.
2//!
3//! This crate contains functions to load and write various data formats,
4//! such as JSON, CBOR, YAML, TOML, XML.
5#![warn(missing_docs)]
6
7extern crate alloc;
8extern crate std;
9
10pub mod read;
11pub mod write;
12
13#[cfg(feature = "all")]
14use jaq_core::native::{run, Fun};
15
16#[cfg(feature = "all")]
17/// (De-)Serialisation filters, such as `fromyaml`, `toxml`.
18pub fn funs<D: for<'a> jaq_core::DataT<V<'a> = jaq_json::Val>>() -> impl Iterator<Item = Fun<D>> {
19    [read::funs::<D>(), write::funs::<D>()]
20        .into_iter()
21        .flat_map(move |funs| funs.into_vec().into_iter().map(run::<D>))
22}
23
24/// Input/Output format.
25#[non_exhaustive]
26#[derive(Copy, Clone, Debug, Default)]
27pub enum Format {
28    /// Raw text string
29    ///
30    /// When the option `--slurp` is used additionally,
31    /// then the whole input is read into a single string.
32    Raw,
33    /// Zero-terminated text string
34    Raw0,
35    /// JavaScript Object Notation
36    #[default]
37    Json,
38    /// Concise Binary Object Representation
39    Cbor,
40    /// Tom's Obvious, Minimal Language
41    Toml,
42    /// Extensible Markup Language
43    Xml,
44    /// YAML Ain't Markup Languageā„¢
45    Yaml,
46    /// Comma-Separated Values
47    Csv,
48    /// Tab-Separated Values
49    Tsv,
50}
51
52impl Format {
53    /// List of all currently supported formats.
54    pub const ALL: &str = "raw, raw0, json, cbor, yaml, toml, xml, csv, tsv";
55
56    /// Determine a file format from a path.
57    pub fn determine(path: &std::path::Path) -> Option<Self> {
58        match path.extension()?.to_str()? {
59            "cbor" => Some(Format::Cbor),
60            "toml" => Some(Format::Toml),
61            "xml" | "xhtml" => Some(Format::Xml),
62            "yml" | "yaml" => Some(Format::Yaml),
63            "json" => Some(Format::Json),
64            "csv" => Some(Format::Csv),
65            "tsv" => Some(Format::Tsv),
66            _ => None,
67        }
68    }
69
70    /// Parse a format name.
71    pub fn parse(s: &str) -> Option<Self> {
72        match s {
73            "cbor" => Some(Format::Cbor),
74            "raw" => Some(Format::Raw),
75            "raw0" => Some(Format::Raw0),
76            "json" => Some(Format::Json),
77            "toml" => Some(Format::Toml),
78            "xml" => Some(Format::Xml),
79            "yaml" => Some(Format::Yaml),
80            "csv" => Some(Format::Csv),
81            "tsv" => Some(Format::Tsv),
82            _ => None,
83        }
84    }
85}
86
87/// Dynamic & thread-safe [`std::error::Error`].
88type BoxError = Box<dyn std::error::Error + Send + Sync>;
89
90/// Create an invalid data I/O error.
91fn invalid_data(e: impl Into<BoxError>) -> std::io::Error {
92    std::io::Error::new(std::io::ErrorKind::InvalidData, e)
93}
94
95fn map_invalid_data<T>(r: Result<T, impl Into<BoxError>>) -> std::io::Result<T> {
96    r.map_err(invalid_data)
97}