1#![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")]
17pub 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#[non_exhaustive]
26#[derive(Copy, Clone, Debug, Default)]
27pub enum Format {
28 Raw,
33 Raw0,
35 #[default]
37 Json,
38 Cbor,
40 Toml,
42 Xml,
44 Yaml,
46 Csv,
48 Tsv,
50}
51
52impl Format {
53 pub const ALL: &str = "raw, raw0, json, cbor, yaml, toml, xml, csv, tsv";
55
56 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 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
87type BoxError = Box<dyn std::error::Error + Send + Sync>;
89
90fn 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}