Write the JSON object to the console # Arguments `json` - The JSON to write `format` - The format to use `include_separator` - Whether to include a separator for YAML before the object
(json: &str, format: Option<&OutputFormat>, include_separator: bool)
| 223 | /// * `format` - The format to use |
| 224 | /// * `include_separator` - Whether to include a separator for YAML before the object |
| 225 | pub fn write_object(json: &str, format: Option<&OutputFormat>, include_separator: bool) { |
| 226 | let mut is_json = true; |
| 227 | let mut output_format = format; |
| 228 | let mut syntax_color = false; |
| 229 | if std::io::stdout().is_terminal() { |
| 230 | syntax_color = true; |
| 231 | if output_format.is_none() { |
| 232 | output_format = Some(&OutputFormat::Yaml); |
| 233 | } |
| 234 | } |
| 235 | else if output_format.is_none() { |
| 236 | output_format = Some(&OutputFormat::Json); |
| 237 | } |
| 238 | |
| 239 | let output = match output_format { |
| 240 | Some(OutputFormat::Json) => json.to_string(), |
| 241 | Some(OutputFormat::PrettyJson) => { |
| 242 | let value: serde_json::Value = match serde_json::from_str(json) { |
| 243 | Ok(value) => value, |
| 244 | Err(err) => { |
| 245 | error!("JSON: {err}"); |
| 246 | exit(EXIT_JSON_ERROR); |
| 247 | } |
| 248 | }; |
| 249 | match serde_json::to_string_pretty(&value) { |
| 250 | Ok(json) => json, |
| 251 | Err(err) => { |
| 252 | error!("JSON: {err}"); |
| 253 | exit(EXIT_JSON_ERROR); |
| 254 | } |
| 255 | } |
| 256 | }, |
| 257 | Some(OutputFormat::Yaml) | None => { |
| 258 | is_json = false; |
| 259 | if include_separator { |
| 260 | println!("---"); |
| 261 | } |
| 262 | |
| 263 | let value: serde_json::Value = match serde_json::from_str(json) { |
| 264 | Ok(value) => value, |
| 265 | Err(err) => { |
| 266 | error!("JSON: {err}"); |
| 267 | exit(EXIT_JSON_ERROR); |
| 268 | } |
| 269 | }; |
| 270 | match serde_yaml::to_string(&value) { |
| 271 | Ok(yaml) => yaml, |
| 272 | Err(err) => { |
| 273 | error!("YAML: {err}"); |
| 274 | exit(EXIT_JSON_ERROR); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | }; |
| 279 | |
| 280 | if syntax_color { |
| 281 | let ps = SyntaxSet::load_defaults_newlines(); |
| 282 | let ts = ThemeSet::load_defaults(); |
no test coverage detected