Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Jan 14, 2024
1 parent d3ffe4f commit 4f0e976
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 30 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ log = "0.4"
itertools = "0.12.0"
once_cell = "1.19.0"
xx = "0.2"
strum = { version = "0.25.0", features = ["derive"] }

[features]
default = ["clap"]
Expand Down
58 changes: 45 additions & 13 deletions cli/src/cli/generate/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ impl Markdown {
}

#[derive(Debug, EnumIs)]
#[strum(serialize_all = "snake_case")]
enum UsageMdDirective {
Load { file: PathBuf },
Title,
Expand Down Expand Up @@ -294,7 +295,7 @@ impl UsageMdDirective {
ctx.plain = false;
let spec = ctx.spec.as_ref().unwrap();
ctx.push(self.to_string());
print_config(&ctx, &spec.config)?;
ctx.push(print_config(&spec.config)?);
ctx.push("<!-- [USAGE] -->".to_string());
}
UsageMdDirective::EndToken => {
Expand Down Expand Up @@ -360,24 +361,55 @@ fn print_commands(ctx: &UsageMdContext, cmds: &[&SchemaCmd]) -> miette::Result<(
Ok(())
}

fn print_config(ctx: &UsageMdContext, config: &SpecConfig) -> miette::Result<()> {
static CONFIG_TEMPLATE: &str = r#"
### `!KEY!`
!ENV!
!DEFAULT!
!HELP!
!LONG_HELP!
"#;

fn print_config(config: &SpecConfig) -> miette::Result<String> {
let mut all = vec![];
for (key, prop) in &config.props {
ctx.push(format!("### `{key}`", key = key));
let mut out = CONFIG_TEMPLATE.to_string();
let mut tmpl = |k, d: String| {
out = out.replace(k, &d);
};
tmpl("!KEY!", key.to_string());
// out = out.replace("!KEY!", &format!("### `{key}`"));
if let Some(env) = &prop.env {
ctx.push(format!("env: `{env}`", env = env));
tmpl("!ENV!", format!("* env: `{env}`"));
// out = out.replace("!ENV!", &format!("* env: `{env}`"));
}
if !prop.default.is_null() {
ctx.push(format!("default: `{default}`", default = prop.default));
if let Some(default) = prop
.default_note
.clone()
.or_else(|| match prop.default.is_null() {
true => None,
false => Some(prop.default.to_string()),
})
{
tmpl("!DEFAULT!", format!("* default: `{default}`"));
// out = out.replace("!DEFAULT!", &format!("* default: `{default}`"));
}
if let Some(help) = &prop.help {
ctx.push(help.to_string());
if let Some(help) = prop.long_help.clone().or(prop.help.clone()) {
// out = out.replace("!HELP!", &format!("* help: `{help}`"));
tmpl("!HELP!", help);
}
if let Some(long_help) = &prop.long_help {
ctx.push(long_help.to_string());
}
ctx.push("Used by commnds: global|*".to_string());
out = regex!(r#"!.+!\n"#)
.replace_all(&out, "")
.trim_start()
.trim_end()
.to_string()
+ "\n";
all.push(out)
// TODO: data type
// TODO: show which commands use this prop ctx.push("Used by commnds: global|*".to_string());
}
Ok(())
Ok(all.join("\n"))
}

#[derive(Error, Diagnostic, Debug)]
Expand Down
31 changes: 18 additions & 13 deletions examples/MISE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,22 +151,27 @@ mise.usage.kdl [flags] [args]
## Config
<!-- [USAGE] config -->
### `activate_accessive`
env: `MISE_ACTIVATE_ACCESSIVE`
default: `false`
Pushes tools' bin-paths to the front of PATH instead of allowing modifications of PATH after activation to take precedence.

* env: `MISE_ACTIVATE_ACCESSIVE`
* default: `false`

foooooooo
Used by commnds: global|*

### `color`
env: `MISE_COLOR`
default: `true`
Used by commnds: global|*

* env: `MISE_COLOR`
* default: `true`

### `jobs`
default: `4`
Used by commnds: global|*

* default: `4`

### `timeout`
default: `1.5`
Used by commnds: global|*

* default: `1.5`

### `user`
default: `"admin"`
Used by commnds: global|*

* default: `"admin"`

<!-- [USAGE] -->
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ pub enum UsageErr {
#[error(transparent)]
IO(#[from] std::io::Error),

#[error(transparent)]
Strum(#[from] strum::ParseError),

#[error(transparent)]
#[diagnostic(transparent)]
KdlError(#[from] kdl::KdlError),
Expand Down
12 changes: 8 additions & 4 deletions src/parse/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ use kdl::{KdlDocument, KdlEntry, KdlNode, KdlValue};

use crate::bail_parse;
use crate::error::UsageErr;
use crate::parse::data_types::SpecDataTypes;
use crate::parse::helpers::NodeHelper;

#[derive(Debug)]
#[derive(Default)]
#[derive(Debug, Default)]
pub struct SpecConfig {
pub props: BTreeMap<String, SpecConfigProp>,
}
Expand All @@ -21,6 +21,8 @@ impl SpecConfig {
#[derive(Debug)]
pub struct SpecConfigProp {
pub default: KdlValue,
pub default_note: Option<String>,
pub data_type: SpecDataTypes,
pub env: Option<String>,
pub help: Option<String>,
pub long_help: Option<String>,
Expand Down Expand Up @@ -62,6 +64,8 @@ impl TryFrom<&KdlNode> for SpecConfig {
for (k, v) in ph.props() {
match k {
"default" => prop.default = v.value.clone(),
"default_note" => prop.default_note = Some(v.ensure_string()?),
"data_type" => prop.data_type = v.ensure_string()?.parse()?,
"env" => prop.env = v.ensure_string()?.to_string().into(),
"help" => prop.help = v.ensure_string()?.to_string().into(),
"long_help" => {
Expand All @@ -80,12 +84,12 @@ impl TryFrom<&KdlNode> for SpecConfig {
}
}



impl Default for SpecConfigProp {
fn default() -> Self {
Self {
default: KdlValue::Null,
default_note: None,
data_type: SpecDataTypes::Null,
env: None,
help: None,
long_help: None,
Expand Down
11 changes: 11 additions & 0 deletions src/parse/data_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use strum::EnumString;

#[derive(Debug, EnumString)]
#[strum(serialize_all = "snake_case")]
pub enum SpecDataTypes {
Null,
String,
Integer,
Float,
Boolean,
}
1 change: 1 addition & 0 deletions src/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod arg;
pub mod cmd;
pub mod config;
mod data_types;
pub mod flag;
pub(crate) mod helpers;
pub mod spec;

0 comments on commit 4f0e976

Please sign in to comment.