Skip to content

Commit

Permalink
version 0.2.0 : yaml support (#15)
Browse files Browse the repository at this point in the history
* Add yaml support!

* version 0.2.0

* tiny doc fix
  • Loading branch information
aobatact authored Jan 22, 2022
1 parent c681bed commit b3182fd
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
.vscode/
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "clap-serde"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "Provides a wrapper to deserialize clap app using serde."
Expand All @@ -9,16 +9,17 @@ repository = "https://github.com/aobatact/clap-serde"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = [ "kebab-case-setting" ]
default = [ "kebab-case-setting", "yaml" ]
env = [ "clap/env" ]
kebab-case-setting = []
snake-case-setting = []
yaml = [ "yaml-rust"]

[dependencies]
clap = { version = "3.0", default-features = false, features = ["std"]}
serde = { version = "1", features = ["derive"]}
yaml-rust = { version = "0.4.5", default-features = false, optional = true }

[dev-dependencies]
serde_yaml = { version = "0.8.23" }
serde_json = { version = "1.0.75" }
toml = { version = "0.5.8" }
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,39 @@ assert_eq!(app.get_about(), Some("test-clap-serde"));
```

## yaml
Not working now because [serde_yaml](https://crates.io/crates/serde_yaml) only accepts `DeserializeOwned`.
```
const YAML_STR: &'static str = r#"
name: app_clap_serde
version : "1.0"
about : yaml_support!
author : yaml_supporter
args:
- apple :
- short: a
- banana:
- short: b
- long: banana
- aliases :
- musa_spp
subcommands:
- sub1:
- about : subcommand_1
- sub2:
- about : subcommand_2
"#;
let yaml = yaml_rust::Yaml::Array(yaml_rust::YamlLoader::load_from_str(YAML_STR).expect("not a yaml"));
let app = clap_serde::yaml_to_app(&yaml).expect("parse failed from yaml");
assert_eq!(app.get_name(), "app_clap_serde");
```

# features
- env
Enables env feature in clap.
- yaml
Enables to use yaml.

## (settings name letter)
Settings names format for [`AppSettings`](`clap::AppSettings`) and [`ArgSettings`](`clap::ArgSettings`).
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
#![doc = include_str!("../README.md")]

use std::ops::Deref;
use clap::{App, Arg, ArgGroup};
use serde::Deserializer;
use std::ops::Deref;

#[cfg(all(feature = "kebab-case-setting", feature = "snake-case-setting"))]
compile_error!("Feature \"kebab-case-setting\" and \"snake-case-setting\" collides. At most one should be set.");

#[macro_use]
mod de;
#[cfg(feature = "yaml")]
mod yaml;

#[cfg(test)]
mod tests;

#[cfg(feature = "yaml")]
pub use yaml::{YamlWrap, yaml_to_app};

/**
Deserialize [`App`] from [`Deserializer`].
```
Expand Down
74 changes: 58 additions & 16 deletions src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,64 @@
use crate::AppWrap;
use clap::App;

//currently fails... beacuse serde_yaml only supports `DeserializeOwned` and no zero copy deserialization
// #[test]
// fn name_yaml() {
// const NAME_YAML: &'static str = "name : app_clap_serde";
// let app: App = serde_yaml::from_str::<AppWrap>(NAME_YAML).expect("parse failed").into();
// assert_eq!(app.get_name(), "app_clap_serde");
// }

// #[test]
// fn name_yaml_2(){
// use serde::de::Deserialize;
// const NAME_YAML: &'static str = "name: app_clap_serde";
// let de = serde_yaml::Deserializer::from_str(NAME_YAML);
// let app: App = AppWrap::deserialize(de).expect("parse failed").into();
// assert_eq!(app.get_name(), "app_clap_serde");
// }
#[cfg(feature = "yaml")]
#[test]
fn name_yaml() {
use yaml_rust::Yaml;

const NAME_YAML: &'static str = "name: app_clap_serde\n";
let yaml =
Yaml::Array(yaml_rust::YamlLoader::load_from_str(NAME_YAML).expect("fail to make yaml"));
let app = crate::load(crate::yaml::YamlWrap::new(&yaml)).expect("parse failed");
assert_eq!(app.get_name(), "app_clap_serde");
}

#[cfg(feature = "yaml")]
#[test]
fn test_yaml() {
use yaml_rust::Yaml;

const NAME_YAML: &'static str = r#"
name: app_clap_serde
version : "1.0"
about : yaml_support!
author : yaml_supporter
args:
- apple :
- short: a
- banana:
- short: b
- long: banana
- aliases :
- musa_spp
subcommands:
- sub1:
- about : subcommand_1
- sub2:
- about : subcommand_2
"#;
let yaml =
Yaml::Array(yaml_rust::YamlLoader::load_from_str(NAME_YAML).expect("fail to make yaml"));
let app = crate::load(crate::yaml::YamlWrap::new(&yaml)).expect("parse failed");
assert_eq!(app.get_name(), "app_clap_serde");
let subs = app.get_subcommands().collect::<Vec<_>>();
assert!(subs
.iter()
.any(|x| x.get_name() == "sub1" && x.get_about() == Some("subcommand_1")));
assert!(subs
.iter()
.any(|x| x.get_name() == "sub2" && x.get_about() == Some("subcommand_2")));
let args = app.get_arguments().collect::<Vec<_>>();
assert!(args
.iter()
.any(|x| x.get_name() == "apple" && x.get_short() == Some('a')));
assert!(args.iter().any(|x| x.get_name() == "banana"
&& x.get_short() == Some('b')
&& x.get_long() == Some("banana")));
}

#[test]
fn name_json() {
Expand Down
Loading

0 comments on commit b3182fd

Please sign in to comment.