1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
#[macro_use] extern crate anyhow; use anyhow::Result; use serde::Serialize; use serde_json::Value as Json; use serde_yaml::Value as Yaml; use toml::Value as Toml; pub enum Format { Toml, Yaml, Json, } pub fn convert(content: &str, target: Format) -> Result<String> { let json_str = serde_json::from_str::<Json>(content) .map_err(|err| anyhow!("Error deserializing Json content : \n{}", err)); if let Ok(json) = json_str { return Ok(get_output(&json, &target).unwrap()); }; let toml_str = toml::from_str::<Toml>(content) .map_err(|err| anyhow!("Error deserializing Toml content : \n{}", err)); if let Ok(toml) = toml_str { return Ok(get_output(&toml, &target).unwrap()); } let yaml_str = serde_yaml::from_str::<Yaml>(&content) .map_err(|err| anyhow!("Error deserializing Yaml content : \n{}", err)); if let Ok(yaml) = yaml_str { Ok(get_output(&yaml, &target).unwrap()) } else { Err(anyhow!("err")) } } fn get_output<T>(content: &T, target: &Format) -> Result<String> where T: Serialize, { match target { Format::Toml => toml::to_string(&content) .map_err(|err| anyhow!("Error serializing Toml content : \n{}", err)), Format::Yaml => serde_yaml::to_string(&content) .map_err(|err| anyhow!("Error serializing Yaml content : \n{}", err)), Format::Json => serde_json::to_string_pretty(&&content) .map_err(|err| anyhow!("Error serializing Json content : \n{}", err)), } } #[cfg(test)] mod tests { use crate::{convert, Format}; use anyhow::Result; use std::fs; #[test] fn yaml_to_toml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.yaml")?; let to = convert(&from, Format::Toml)?; let expected = fs::read_to_string("tests/replica-set.toml")?; assert_eq!(to, expected); Ok(()) } #[test] fn yaml_to_json_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.yaml")?; let to = convert(&from, Format::Json)?; let expected = fs::read_to_string("tests/replica-set.json")?; assert_eq!(to, expected); Ok(()) } #[test] fn yaml_to_yaml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.yaml")?; let to = convert(&from, Format::Yaml)?; let expected = fs::read_to_string("tests/replica-set.yaml")?; assert_eq!(to, expected); Ok(()) } #[test] fn toml_to_toml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.toml")?; let to = convert(&from, Format::Toml)?; let expected = fs::read_to_string("tests/replica-set.toml")?; assert_eq!(to, expected); Ok(()) } #[test] fn toml_to_json_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.toml")?; let to = convert(&from, Format::Json)?; let expected = fs::read_to_string("tests/replica-set.json")?; assert_eq!(to, expected); Ok(()) } #[test] fn toml_to_yaml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.toml")?; let to = convert(&from, Format::Yaml)?; let expected = fs::read_to_string("tests/replica-set.yaml")?; assert_eq!(to, expected); Ok(()) } #[test] fn json_to_toml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.json")?; let to = convert(&from, Format::Toml)?; let expected = fs::read_to_string("tests/replica-set.toml")?; assert_eq!(to, expected); Ok(()) } #[test] fn json_to_json_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.json")?; let to = convert(&from, Format::Json)?; let expected = fs::read_to_string("tests/replica-set.json")?; assert_eq!(to, expected); Ok(()) } #[test] fn json_to_yaml_ok() -> Result<()> { let from = fs::read_to_string("tests/replica-set.json")?; let to = convert(&from, Format::Yaml)?; let expected = fs::read_to_string("tests/replica-set.yaml")?; assert_eq!(to, expected); Ok(()) } }