Skip to content

Commit ab6c45e

Browse files
authored
Merge pull request npm#49 from googleworkspace/fix/unknown-format-warning
fix: warn to stderr when unknown --format value is provided (fixes npm#38)
2 parents 666f9a8 + ee35e4a commit ab6c45e

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"gws": patch
3+
---
4+
5+
fix: warn to stderr when unknown --format value is provided

src/formatter.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,26 @@ pub enum OutputFormat {
3535

3636
impl OutputFormat {
3737
/// Parse from a string argument.
38-
pub fn from_str(s: &str) -> Self {
38+
///
39+
/// Returns `Ok(format)` for known values, or `Err(unknown_value)` if the
40+
/// string is not recognised. Call sites should warn the user on `Err` and
41+
/// decide whether to fall back to JSON or surface an error.
42+
pub fn parse(s: &str) -> Result<Self, String> {
3943
match s.to_lowercase().as_str() {
40-
"table" => Self::Table,
41-
"yaml" | "yml" => Self::Yaml,
42-
"csv" => Self::Csv,
43-
_ => Self::Json,
44+
"json" => Ok(Self::Json),
45+
"table" => Ok(Self::Table),
46+
"yaml" | "yml" => Ok(Self::Yaml),
47+
"csv" => Ok(Self::Csv),
48+
other => Err(other.to_string()),
4449
}
4550
}
51+
52+
/// Parse from a string argument, falling back to JSON for unknown values.
53+
///
54+
/// Prefer `parse()` at call sites where you want to surface a warning.
55+
pub fn from_str(s: &str) -> Self {
56+
Self::parse(s).unwrap_or(Self::Json)
57+
}
4658
}
4759

4860
/// Format a JSON value according to the specified output format.
@@ -374,6 +386,25 @@ mod tests {
374386
assert_eq!(OutputFormat::from_str("unknown"), OutputFormat::Json);
375387
}
376388

389+
#[test]
390+
fn test_output_format_parse_known() {
391+
assert_eq!(OutputFormat::parse("json"), Ok(OutputFormat::Json));
392+
assert_eq!(OutputFormat::parse("table"), Ok(OutputFormat::Table));
393+
assert_eq!(OutputFormat::parse("yaml"), Ok(OutputFormat::Yaml));
394+
assert_eq!(OutputFormat::parse("yml"), Ok(OutputFormat::Yaml));
395+
assert_eq!(OutputFormat::parse("csv"), Ok(OutputFormat::Csv));
396+
// Case-insensitive
397+
assert_eq!(OutputFormat::parse("JSON"), Ok(OutputFormat::Json));
398+
assert_eq!(OutputFormat::parse("TABLE"), Ok(OutputFormat::Table));
399+
}
400+
401+
#[test]
402+
fn test_output_format_parse_unknown_returns_err() {
403+
assert!(OutputFormat::parse("bogus").is_err());
404+
assert_eq!(OutputFormat::parse("bogus").unwrap_err(), "bogus");
405+
assert!(OutputFormat::parse("").is_err());
406+
}
407+
377408
#[test]
378409
fn test_format_json() {
379410
let val = json!({"name": "test"});

src/main.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,18 @@ async fn run() -> Result<(), GwsError> {
139139
})?;
140140

141141
// Resolve --format flag
142-
let output_format = matches
143-
.get_one::<String>("format")
144-
.map(|s| formatter::OutputFormat::from_str(s))
145-
.unwrap_or_default();
142+
let output_format = match matches.get_one::<String>("format") {
143+
Some(s) => match formatter::OutputFormat::parse(s) {
144+
Ok(fmt) => fmt,
145+
Err(unknown) => {
146+
eprintln!(
147+
"warning: unknown output format '{unknown}'; falling back to json (valid options: json, table, yaml, csv)"
148+
);
149+
formatter::OutputFormat::Json
150+
}
151+
},
152+
None => formatter::OutputFormat::default(),
153+
};
146154

147155
// Resolve --sanitize template (flag or env var)
148156
let sanitize_template = matches

0 commit comments

Comments
 (0)