Skip to content

Commit

Permalink
write exmaple to file (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed May 8, 2023
1 parent efcb830 commit 8dac8fa
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2

- name: Super-Linter
uses: github/super-linter@v4
uses: github/super-linter@v5
env:
VALIDATE_ALL_CODEBASE: false
DEFAULT_BRANCH: main
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
]

[workspace.package]
version = "0.5.1"
version = "0.6.0"
edition = "2021"
authors = ["Antonio Yang <yanganto@gmail.com>"]
license = "MIT"
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ fn default_int() -> usize {
fn default_str() -> String {
"default".into()
}

Config::to_toml_example("example.toml"); // write example to a file
let example = Config::toml_example();
```

Expand All @@ -63,7 +65,6 @@ f = "default"

## Will do later
- nesting structure
- function to write example file, `to_toml_example(file_name)`

[crates-badge]: https://img.shields.io/crates/v/toml-example.svg
[crate-url]: https://crates.io/crates/toml-example
Expand Down
21 changes: 10 additions & 11 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fn parse_type(ty: &Type, default: &mut String, optional: &mut bool) -> Option<St
}) = arguments
{
if let Some(GenericArgument::Type(ty)) = args.first() {
parse_type(&ty, default, &mut false);
parse_type(ty, default, &mut false);
}
}
} else if id == "Vec" {
Expand All @@ -57,9 +57,9 @@ fn parse_type(ty: &Type, default: &mut String, optional: &mut bool) -> Option<St
{
if let Some(GenericArgument::Type(ty)) = args.first() {
let mut item_default_value = String::new();
parse_type(&ty, &mut item_default_value, &mut false);
parse_type(ty, &mut item_default_value, &mut false);
*default = if item_default_value.is_empty() {
format!("[ ]")
"[ ]".to_string()
} else {
format!("[ {item_default_value:}, ]")
}
Expand All @@ -72,14 +72,14 @@ fn parse_type(ty: &Type, default: &mut String, optional: &mut bool) -> Option<St
r#type
}

fn parse_doc_default_attrs(attrs: &Vec<Attribute>) -> (Vec<String>, Option<DefaultSource>) {
fn parse_doc_default_attrs(attrs: &[Attribute]) -> (Vec<String>, Option<DefaultSource>) {
let mut docs = Vec::new();
let mut default_source = None;
for attr in attrs.iter() {
match (attr.style, &attr.meta) {
(Outer, NameValue(MetaNameValue { path, value, .. })) => {
for seg in path.segments.iter() {
if seg.ident.to_string() == "doc" {
if seg.ident == "doc" {
if let Lit(ExprLit {
lit: Str(lit_str), ..
}) = value
Expand All @@ -93,9 +93,8 @@ fn parse_doc_default_attrs(attrs: &Vec<Attribute>) -> (Vec<String>, Option<Defau
if path
.segments
.last()
.map(|s| s.ident.to_string() == "serde")
.unwrap_or_default()
== true =>
.map(|s| s.ident == "serde")
.unwrap_or_default() =>
{
let token_str = tokens.to_string();
if token_str.starts_with("default") {
Expand Down Expand Up @@ -155,7 +154,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
if let Named(fields_named) = fields {
for f in fields_named.named.iter() {
if let Some(field_name) = f.ident.as_ref().map(|i| i.to_string()) {
let (default, doc_str, optional) = get_default_and_doc_from_field(&f);
let (default, doc_str, optional) = get_default_and_doc_from_field(f);
push_doc_string(&mut example, doc_str, false);

if optional {
Expand All @@ -165,7 +164,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
DefaultSource::DefaultValue(default) => {
example.push_str(&field_name);
example.push_str(" = ");
example.push_str(&default.replace("\\", "\\\\").replace("\"", "\\\""));
example.push_str(&default.replace('\\', "\\\\").replace('\"', "\\\""));
example.push('\n');
}
DefaultSource::DefaultFn(None) => {
Expand All @@ -182,7 +181,7 @@ pub fn derive_patch(item: TokenStream) -> TokenStream {
example.push_str(&field_name);
example.push_str(" = \".to_string()");
example.push_str(&format!(" + &format!(\"{{:?}}\", {fn_str}())"));
example.push_str(&"+ &\"\n");
example.push_str("+ &\"\n");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
readme.workspace = true

[dependencies]
toml-example-derive = { version = "=0.5", path = "../derive" }
toml-example-derive = { version = "=0.6", path = "../derive" }

[dev-dependencies]
serde = { version = "1.0", features = ["derive"] }
Expand Down
13 changes: 12 additions & 1 deletion lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,18 @@ b = ""
assert_eq!(
toml::from_str::<Config>(&Config::toml_example()).unwrap(),
Config::default()
)
);
let mut tmp_file = std::env::temp_dir();
tmp_file.push("config.toml");
Config::to_toml_example(&tmp_file.as_path().to_str().unwrap()).unwrap();
assert_eq!(
std::fs::read_to_string(tmp_file).unwrap(),
r#"# Config.a should be a number
a = 0
# Config.b should be a string
b = ""
"#
);
}

#[test]
Expand Down
8 changes: 8 additions & 0 deletions lib/src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use std::fs::File;
use std::io::prelude::*;

pub trait TomlExample {
/// structure to toml example
fn toml_example() -> String;
fn to_toml_example(file_name: &str) -> std::io::Result<()> {
let mut file = File::create(file_name)?;
file.write_all(Self::toml_example().as_bytes())?;
Ok(())
}
}

0 comments on commit 8dac8fa

Please sign in to comment.