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
// Copyright 2021 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

mod dynamic_writer;
mod machine_writer;
mod simple_writer;
mod test_buffer;
mod tool_io;

pub use dynamic_writer::*;
pub use machine_writer::*;
pub use simple_writer::*;
pub use test_buffer::*;
pub use tool_io::*;

#[derive(thiserror::Error, Debug)]
#[error("Error while presenting output")]
pub enum Error {
    #[error("Error on the underlying IO stream")]
    Io(#[from] std::io::Error),
    #[error("Error formatting JSON output")]
    Json(#[from] serde_json::Error),
    #[error("Error parsing utf8 from buffer")]
    Utf8(#[from] std::string::FromUtf8Error),
    #[error("`{0}` is not a valid machine format")]
    InvalidFormat(String),
}

type Result<O, E = Error> = std::result::Result<O, E>;

/// The valid formats possible to output for machine consumption.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Format {
    Json,
    JsonPretty,
}

impl std::str::FromStr for Format {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_ref() {
            "json-pretty" => Ok(Format::JsonPretty),
            "json" | "j" => Ok(Format::Json),
            other => Err(Error::InvalidFormat(other.to_owned())),
        }
    }
}

impl From<Error> for ffx_command_error::Error {
    fn from(error: Error) -> Self {
        use ffx_command_error::Error::*;
        use Error::*;
        match error {
            error @ (Io(_) | Json(_) | Utf8(_)) => Unexpected(error.into()),
            error @ InvalidFormat(_) => User(error.into()),
        }
    }
}