1use usv::*;
2
3pub fn usv_to_json<
5 >(
7 usv: &str
8) -> Result<String, serde_json::Error> {
9 if usv.contains("\u{001C}") || usv.contains("␜") {
10 let files: Files = usv.files().collect();
11 return Ok(serde_json::to_string(&files)?)
12 }
13 if usv.contains("\u{001D}") || usv.contains("␝") {
14 let groups: Groups = usv.groups().collect();
15 return Ok(serde_json::to_string(&groups)?)
16 }
17 if usv.contains("\u{001E}") || usv.contains("␞") {
18 let records: Records = usv.records().collect();
19 return Ok(serde_json::to_string(&records)?)
20 }
21 if usv.contains("\u{001F}") || usv.contains("␟") {
22 let units: Units = usv.units().collect();
23 return Ok(serde_json::to_string(&units)?)
24 }
25 Err(serde::de::Error::custom(usv))
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31
32 #[test]
33 fn units_test() {
34 let usv = String::from("a␟b␟");
35 let json = String::from(r#"["a","b"]"#);
36 assert_eq!(usv_to_json(&usv).unwrap(), json);
37 }
38
39 #[test]
40 fn records_test() {
41 let usv = String::from("a␟b␟␞c␟d␟␞");
42 let json = String::from(r#"[["a","b"],["c","d"]]"#);
43 assert_eq!(usv_to_json(&usv).unwrap(), json);
44 }
45
46 #[test]
47 fn groups_test() {
48 let usv = String::from("a␟b␟␞c␟d␟␞␝e␟f␟␞g␟h␟␞␝");
49 let json = String::from(r#"[[["a","b"],["c","d"]],[["e","f"],["g","h"]]]"#);
50 assert_eq!(usv_to_json(&usv).unwrap(), json);
51 }
52
53 #[test]
54 fn files_test() {
55 let usv = String::from("a␟b␟␞c␟d␟␞␝e␟f␟␞g␟h␟␞␝␜i␟j␟␞k␟l␟␞␝m␟n␟␞o␟p␟␞␝␜");
56 let json = String::from(r#"[[[["a","b"],["c","d"]],[["e","f"],["g","h"]]],[[["i","j"],["k","l"]],[["m","n"],["o","p"]]]]"#);
57 assert_eq!(usv_to_json(&usv).unwrap(), json);
58 }
59
60}