1use ::serde::{Serialize, de::DeserializeOwned};
2
3pub mod config;
4mod config_options;
5pub mod error;
6pub mod object;
7pub mod parser;
8pub(crate) mod path;
9pub mod raw;
10pub mod serde;
11pub mod syntax;
12pub mod transform;
13pub mod value;
14mod merge {
15 pub(crate) mod add_assign;
16 pub(crate) mod array;
17 pub(crate) mod concat;
18 pub(crate) mod delay_replacement;
19 pub(crate) mod memo;
20 pub(crate) mod object;
21 pub(crate) mod path;
22 pub(crate) mod substitution;
23 pub(crate) mod value;
24}
25pub use config::Config;
26pub use config_options::ConfigOptions;
27pub use error::Error;
28pub use value::Value;
29
30pub type Result<T> = std::result::Result<T, Error>;
31
32pub fn to_value<T>(value: T) -> crate::Result<Value>
33where
34 T: Serialize,
35{
36 let value: Value = serde_json::to_value(value)?.into();
37 Ok(value)
38}
39
40pub fn from_value<T>(value: Value) -> crate::Result<T>
41where
42 T: DeserializeOwned,
43{
44 T::deserialize(value)
45}
46
47#[inline]
48pub(crate) fn join<I, V>(
49 mut iter: I,
50 sep: &str,
51 f: &mut std::fmt::Formatter<'_>,
52) -> std::fmt::Result
53where
54 I: Iterator<Item = V>,
55 V: std::fmt::Display,
56{
57 if let Some(v) = iter.next() {
58 write!(f, "{v}")?;
59 for v in iter {
60 write!(f, "{sep}")?;
61 write!(f, "{v}")?;
62 }
63 }
64 Ok(())
65}
66
67#[inline]
68pub(crate) fn join_format<I, V, S, R>(
69 mut iter: I,
70 f: &mut std::fmt::Formatter<'_>,
71 separator_formatter: S,
72 value_formatter: R,
73) -> std::fmt::Result
74where
75 I: Iterator<Item = V>,
76 S: Fn(&mut std::fmt::Formatter) -> std::fmt::Result,
77 R: Fn(&mut std::fmt::Formatter, V) -> std::fmt::Result,
78{
79 if let Some(v) = iter.next() {
80 value_formatter(f, v)?;
81 for v in iter {
82 separator_formatter(f)?;
83 value_formatter(f, v)?;
84 }
85 }
86 Ok(())
87}
88
89#[inline]
90pub(crate) fn join_debug<I, V>(
91 mut iter: I,
92 sep: &str,
93 f: &mut std::fmt::Formatter<'_>,
94) -> std::fmt::Result
95where
96 I: Iterator<Item = V>,
97 V: std::fmt::Debug,
98{
99 if let Some(v) = iter.next() {
100 write!(f, "{v:?}")?;
101 for v in iter {
102 write!(f, "{sep}")?;
103 write!(f, "{v:?}")?;
104 }
105 }
106 Ok(())
107}
108
109#[cfg(test)]
110mod test {
111 use tracing::level_filters::LevelFilter;
112 use tracing_subscriber::fmt::time::LocalTime;
113
114 #[ctor::ctor]
115 fn init_tracing() {
116 tracing_subscriber::fmt()
117 .with_test_writer()
118 .pretty()
119 .with_max_level(LevelFilter::TRACE)
120 .with_timer(LocalTime::rfc_3339())
121 .try_init();
122 }
123}