1mod json_writer;
6mod test_buffer;
7mod tool_io;
8mod writer;
9
10pub use json_writer::{JsonWriter, format_output};
11pub use test_buffer::{TestBuffer, TestBuffers};
12pub use tool_io::ToolIO;
13pub use writer::Writer;
14
15#[derive(Clone, Copy, Debug, PartialEq, Eq)]
17pub enum Format {
18 Json,
19 JsonPretty,
20 Raw,
21}
22
23impl std::str::FromStr for Format {
24 type Err = Error;
25
26 fn from_str(s: &str) -> Result<Self, Self::Err> {
27 match s.to_lowercase().as_ref() {
28 "json-pretty" => Ok(Format::JsonPretty),
29 "json" | "j" => Ok(Format::Json),
30 "raw" => Ok(Format::Raw),
31 other => Err(Error::InvalidFormat(other.into())),
32 }
33 }
34}
35
36#[derive(thiserror::Error, Debug)]
37#[error("Error while presenting output")]
38pub enum Error {
39 #[error("Error on the underlying IO stream")]
40 Io(#[from] std::io::Error),
41 #[error("Error formatting JSON output")]
42 Json(#[from] serde_json::Error),
43 #[error("Error parsing utf8 from buffer")]
44 Utf8(#[from] std::string::FromUtf8Error),
45 #[error("`{0}` is not a valid machine format")]
46 InvalidFormat(String),
47 #[error("Schema validation failed: {0}")]
48 SchemaFailure(String),
49}
50
51pub type Result<O, E = Error> = std::result::Result<O, E>;
52
53impl From<Error> for ffx_command_error::Error {
54 fn from(error: Error) -> Self {
55 use Error::*;
56 use ffx_command_error::Error::*;
57 match error {
58 error @ (Io(_) | Json(_) | Utf8(_) | SchemaFailure(_)) => Unexpected(error.into()),
59 error @ InvalidFormat(_) => User(error.into()),
60 }
61 }
62}