Skip to content

Commit

Permalink
complate 0.14 - now with lib (preliminary)
Browse files Browse the repository at this point in the history
  • Loading branch information
cchexcode committed Jul 17, 2024
1 parent 1825cf3 commit 199cda6
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 139 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ categories = ["command-line-utilities"]
readme = "docs/README.md"
autobins = false

[lib]
name = "complate"
path = "./src/lib.rs"

[[bin]]
name = "complate"
path = "./src/main.rs"
Expand All @@ -33,12 +37,11 @@ mime = "0.3.17"
serde = { version = "1.0.181", features = ["derive"] }
serde_json = "1.0.104"
serde_yaml = "0.9.25"
thiserror = "1.0.44"
anyhow = "1.0.72"
dialoguer = { version = "0.10.4", optional = true }
schemars = "0.8.12"
fancy-regex = "0.11.0"
indoc = "2.0.3"
anyhow = "1.0.86"

[dev-dependencies]
rusty-hook = "0.11.2"
42 changes: 9 additions & 33 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use {
crate::error::Error,
anyhow::Result,
clap::{
Arg,
Expand Down Expand Up @@ -48,30 +47,7 @@ pub enum Command {
Autocomplete { path: String, shell: clap_complete::Shell },
Init,
Schema,
Render(RenderArguments),
}

#[derive(Debug)]
pub struct RenderArguments {
pub configuration: String,
pub template: Option<String>,
pub value_overrides: HashMap<String, String>,
pub shell_trust: ShellTrust,
pub loose: bool,
pub backend: Backend,
}

#[derive(Debug, Eq, PartialEq)]
pub enum ShellTrust {
None,
Ultimate,
}

#[derive(Debug)]
pub enum Backend {
Headless,
#[cfg(feature = "backend+cli")]
CLI,
Render(crate::render::RenderArguments),
}

pub struct ClapArgumentLoader {}
Expand Down Expand Up @@ -193,7 +169,7 @@ impl ClapArgumentLoader {
format: match subc.get_one::<String>("format").unwrap().as_str() {
| "manpages" => ManualFormat::Manpages,
| "markdown" => ManualFormat::Markdown,
| _ => return Err(Error::Argument("unknown format".into()).into()),
| _ => return Err(anyhow::anyhow!("unknown format")),
},
},
privileges,
Expand All @@ -220,9 +196,9 @@ impl ClapArgumentLoader {
let config = std::fs::read_to_string(subc.get_one::<String>("config").unwrap())?;
let template = subc.get_one::<String>("template").map(|v| v.into());
let shell_trust = if subc.get_flag("trust") {
ShellTrust::Ultimate
crate::render::ShellTrust::Ultimate
} else {
ShellTrust::None
crate::render::ShellTrust::None
};
let loose = subc.get_flag("loose");

Expand All @@ -234,15 +210,15 @@ impl ClapArgumentLoader {
}
}
let backend = match subc.get_one::<String>("backend").unwrap().as_str() {
| "headless" => Backend::Headless,
| "headless" => crate::render::Backend::Headless,
#[cfg(feature = "backend+cli")]
| "cli" => Backend::CLI,
| _ => return Err(Error::Argument("no backend specified".into()).into()),
| "cli" => crate::render::Backend::CLI,
| _ => return Err(anyhow::anyhow!("no backend specified")),
};

Ok(CallArgs {
privileges,
command: Command::Render(RenderArguments {
command: Command::Render(crate::render::RenderArguments {
configuration: config,
template,
value_overrides,
Expand All @@ -252,7 +228,7 @@ impl ClapArgumentLoader {
}),
})
} else {
return Err(Error::UnknownCommand.into());
return Err(anyhow::anyhow!("unknown command"));
}
}
}
25 changes: 0 additions & 25 deletions src/error.rs

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod render;
pub mod config;
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ use {
std::path::PathBuf,
};

pub mod args;
pub mod config;
pub mod error;
pub mod reference;
pub mod render;
mod args;
mod config;
mod reference;
mod render;

#[tokio::main]
async fn main() -> Result<()> {
Expand Down Expand Up @@ -58,15 +57,14 @@ async fn main() -> Result<()> {
#[cfg(test)]
mod tests {
use {
crate::error::Error,
anyhow::Result,
std::process::Command,
};

fn exec(command: &str) -> Result<String> {
let output = Command::new("sh").arg("-c").arg(command).output()?;
if output.status.code().unwrap() != 0 {
return Err(Error::ShellCommand(String::from_utf8(output.stderr)?).into());
return Err(anyhow::anyhow!(String::from_utf8(output.stderr)?));
}
Ok(String::from_utf8(output.stdout)?)
}
Expand Down
7 changes: 3 additions & 4 deletions src/render/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use {
super::UserInput,
crate::error::Error,
anyhow::Result,
async_trait::async_trait,
std::collections::{
Expand All @@ -24,11 +23,11 @@ impl<'a> UserInput for CLIBackend<'a> {
async fn prompt(&self, text: &str) -> Result<String> {
match dialoguer::Input::new().allow_empty(true).with_prompt(text).interact() {
| Ok(res) => Ok(res),
| Err(_) => Err(Error::InteractAbort.into()),
| Err(_) => Err(anyhow::anyhow!("interaction aborted")),
}
}

async fn select(&self, prompt: &str, options: &BTreeMap<String, super::Option>) -> Result<String> {
async fn select(&self, prompt: &str, options: &BTreeMap<String, crate::config::Option>) -> Result<String> {
let keys = options.keys().cloned().collect::<Vec<String>>();
let display_vals = options.values().map(|x| x.display.to_owned()).collect::<Vec<String>>();

Expand All @@ -43,7 +42,7 @@ impl<'a> UserInput for CLIBackend<'a> {
}
}

async fn check(&self, prompt: &str, separator: &str, options: &BTreeMap<String, super::Option>) -> Result<String> {
async fn check(&self, prompt: &str, separator: &str, options: &BTreeMap<String, crate::config::Option>) -> Result<String> {
let keys = options.keys().cloned().collect::<Vec<String>>();
let display_vals = options.values().map(|x| x.display.to_owned()).collect::<Vec<String>>();

Expand Down
14 changes: 6 additions & 8 deletions src/render/headless.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use {
super::UserInput,
crate::error::Error,
anyhow::Result,
async_trait::async_trait,
};
Expand All @@ -14,25 +12,25 @@ impl HeadlessBackend {
}

#[async_trait]
impl UserInput for HeadlessBackend {
impl super::UserInput for HeadlessBackend {
async fn prompt(&self, _text: &str) -> Result<String> {
Err(Error::Invalid("can not prompt in headless backend".into()).into())
Err(anyhow::anyhow!("can not prompt in headless backend").into())
}

async fn select(
&self,
_prompt: &str,
_options: &std::collections::BTreeMap<String, super::Option>,
_options: &std::collections::BTreeMap<String, crate::config::Option>,
) -> Result<String> {
Err(Error::Invalid("can not prompt in headless backend".into()).into())
Err(anyhow::anyhow!("can not prompt in headless backend").into())
}

async fn check(
&self,
_prompt: &str,
_separator: &str,
_options: &std::collections::BTreeMap<String, super::Option>,
_options: &std::collections::BTreeMap<String, crate::config::Option>,
) -> Result<String> {
Err(Error::Invalid("can not prompt in headless backend".into()).into())
Err(anyhow::anyhow!("can not prompt in headless backend").into())
}
}
Loading

0 comments on commit 199cda6

Please sign in to comment.