1#![warn(clippy::pedantic)]
10#![forbid(unsafe_code)]
11#![deny(missing_docs)]
12
13pub fn json_pretty(input: &str) -> Result<String, String> {
18 let v: serde_json::Value = serde_json::from_str(input).map_err(|e| e.to_string())?;
19 serde_json::to_string_pretty(&v).map_err(|e| e.to_string())
20}
21
22pub fn json_minify(input: &str) -> Result<String, String> {
27 let v: serde_json::Value = serde_json::from_str(input).map_err(|e| e.to_string())?;
28 serde_json::to_string(&v).map_err(|e| e.to_string())
29}
30
31pub fn yaml_format(input: &str) -> Result<String, String> {
36 let v: serde_norway::Value = serde_norway::from_str(input).map_err(|e| e.to_string())?;
37 serde_norway::to_string(&v).map_err(|e| e.to_string())
38}
39
40pub fn toml_format(input: &str) -> Result<String, String> {
45 let v: toml::Value = toml::from_str(input).map_err(|e| e.to_string())?;
46 toml::to_string_pretty(&v).map_err(|e| e.to_string())
47}
48
49#[cfg(test)]
50mod tests {
51 use super::*;
52
53 #[test]
54 fn json_pretty_indents() {
55 assert_eq!(json_pretty(r#"{"a":1}"#).unwrap(), "{\n \"a\": 1\n}");
56 }
57
58 #[test]
59 fn json_minify_compacts() {
60 assert_eq!(json_minify("{\n \"a\": 1\n}").unwrap(), r#"{"a":1}"#);
61 }
62
63 #[test]
64 fn json_rejects_bad_input() {
65 assert!(json_pretty("{nope}").is_err());
66 assert!(json_minify("{nope}").is_err());
67 }
68
69 #[test]
70 fn yaml_roundtrips() {
71 let out = yaml_format("a: 1\nb: 2\n").unwrap();
72 assert!(out.contains("a: 1"), "got: {out}");
73 assert!(yaml_format("a: [unclosed").is_err());
74 }
75
76 #[test]
77 fn toml_roundtrips() {
78 let out = toml_format("a=1\nb=2\n").unwrap();
79 assert!(out.contains("a = 1"), "got: {out}");
80 assert!(toml_format("= bad").is_err());
81 }
82}