char/avril

(wip) why'd you have to go and make things so complicaaaaated?

git clone https://git.t4t.associates/char/avril

Charlotte Somrearrange libs into lib and extensions into extsf4a0f5b

main
2.9 KiB96 linesraw
1#![doc = include_str!("../README.md")]
2
3mod document;
4mod format;
5pub mod syntax;
6
7pub use document::{Document, PathSegment};
8pub use syntax::{Error, ErrorKind, Span};
9
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11#[non_exhaustive]
12pub struct Options {
13    pub max_depth: usize,
14}
15
16impl Options {
17    pub const DEFAULT_MAX_DEPTH: usize = 128;
18
19    pub fn new() -> Self {
20        Self::default()
21    }
22
23    pub fn with_max_depth(mut self, max_depth: usize) -> Self {
24        self.max_depth = max_depth;
25        self
26    }
27}
28
29impl Default for Options {
30    fn default() -> Self {
31        Self {
32            max_depth: Self::DEFAULT_MAX_DEPTH,
33        }
34    }
35}
36
37pub fn parse(source: &str) -> Result<serde_json::Value, Error> {
38    parse_with(source, &Options::default())
39}
40
41pub fn parse_with(source: &str, options: &Options) -> Result<serde_json::Value, Error> {
42    syntax::parse_with(source, options)?.to_value()
43}
44
45pub fn stringify(value: &serde_json::Value) -> Result<String, Error> {
46    stringify_with(value, &Options::default())
47}
48
49pub fn stringify_with(value: &serde_json::Value, options: &Options) -> Result<String, Error> {
50    format::print(value, true, options)
51}
52
53impl syntax::Value {
54    pub fn to_value(&self) -> Result<serde_json::Value, Error> {
55        use serde_json::Value;
56        use syntax::ValueKind;
57
58        Ok(match &self.kind {
59            ValueKind::Null => Value::Null,
60            ValueKind::Bool(value) => Value::Bool(*value),
61            // Rebuilt from primitives so `serde_json/arbitrary_precision`, which the
62            // bundler enables, neither keeps the source spelling nor admits out-of-range numbers.
63            ValueKind::Number(value) => Value::Number(
64                value
65                    .parse()
66                    .ok()
67                    .and_then(|number: serde_json::Number| {
68                        number
69                            .as_u64()
70                            .map(Into::into)
71                            .or_else(|| number.as_i64().map(Into::into))
72                            .or_else(|| serde_json::Number::from_f64(number.as_f64()?))
73                    })
74                    .ok_or_else(|| Error {
75                        kind: ErrorKind::Number,
76                        span: self.span.clone(),
77                        message: "Invalid or out-of-range JSON number".into(),
78                        previous: None,
79                    })?,
80            ),
81            ValueKind::String(value) => Value::String(value.clone()),
82            ValueKind::Array(values) => Value::Array(
83                values
84                    .iter()
85                    .map(Self::to_value)
86                    .collect::<Result<_, _>>()?,
87            ),
88            ValueKind::Object(members) => Value::Object(
89                members
90                    .iter()
91                    .map(|member| Ok((member.key.clone(), member.value.to_value()?)))
92                    .collect::<Result<_, Error>>()?,
93            ),
94        })
95    }
96}