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