Skip to content

Commit 199cda6

Browse files
author
Alexander Weber
committed
complate 0.14 - now with lib (preliminary)
1 parent 1825cf3 commit 199cda6

File tree

9 files changed

+123
-139
lines changed

9 files changed

+123
-139
lines changed

Cargo.lock

Lines changed: 2 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ categories = ["command-line-utilities"]
1212
readme = "docs/README.md"
1313
autobins = false
1414

15+
[lib]
16+
name = "complate"
17+
path = "./src/lib.rs"
18+
1519
[[bin]]
1620
name = "complate"
1721
path = "./src/main.rs"
@@ -33,12 +37,11 @@ mime = "0.3.17"
3337
serde = { version = "1.0.181", features = ["derive"] }
3438
serde_json = "1.0.104"
3539
serde_yaml = "0.9.25"
36-
thiserror = "1.0.44"
37-
anyhow = "1.0.72"
3840
dialoguer = { version = "0.10.4", optional = true }
3941
schemars = "0.8.12"
4042
fancy-regex = "0.11.0"
4143
indoc = "2.0.3"
44+
anyhow = "1.0.86"
4245

4346
[dev-dependencies]
4447
rusty-hook = "0.11.2"

src/args.rs

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use {
2-
crate::error::Error,
32
anyhow::Result,
43
clap::{
54
Arg,
@@ -48,30 +47,7 @@ pub enum Command {
4847
Autocomplete { path: String, shell: clap_complete::Shell },
4948
Init,
5049
Schema,
51-
Render(RenderArguments),
52-
}
53-
54-
#[derive(Debug)]
55-
pub struct RenderArguments {
56-
pub configuration: String,
57-
pub template: Option<String>,
58-
pub value_overrides: HashMap<String, String>,
59-
pub shell_trust: ShellTrust,
60-
pub loose: bool,
61-
pub backend: Backend,
62-
}
63-
64-
#[derive(Debug, Eq, PartialEq)]
65-
pub enum ShellTrust {
66-
None,
67-
Ultimate,
68-
}
69-
70-
#[derive(Debug)]
71-
pub enum Backend {
72-
Headless,
73-
#[cfg(feature = "backend+cli")]
74-
CLI,
50+
Render(crate::render::RenderArguments),
7551
}
7652

7753
pub struct ClapArgumentLoader {}
@@ -193,7 +169,7 @@ impl ClapArgumentLoader {
193169
format: match subc.get_one::<String>("format").unwrap().as_str() {
194170
| "manpages" => ManualFormat::Manpages,
195171
| "markdown" => ManualFormat::Markdown,
196-
| _ => return Err(Error::Argument("unknown format".into()).into()),
172+
| _ => return Err(anyhow::anyhow!("unknown format")),
197173
},
198174
},
199175
privileges,
@@ -220,9 +196,9 @@ impl ClapArgumentLoader {
220196
let config = std::fs::read_to_string(subc.get_one::<String>("config").unwrap())?;
221197
let template = subc.get_one::<String>("template").map(|v| v.into());
222198
let shell_trust = if subc.get_flag("trust") {
223-
ShellTrust::Ultimate
199+
crate::render::ShellTrust::Ultimate
224200
} else {
225-
ShellTrust::None
201+
crate::render::ShellTrust::None
226202
};
227203
let loose = subc.get_flag("loose");
228204

@@ -234,15 +210,15 @@ impl ClapArgumentLoader {
234210
}
235211
}
236212
let backend = match subc.get_one::<String>("backend").unwrap().as_str() {
237-
| "headless" => Backend::Headless,
213+
| "headless" => crate::render::Backend::Headless,
238214
#[cfg(feature = "backend+cli")]
239-
| "cli" => Backend::CLI,
240-
| _ => return Err(Error::Argument("no backend specified".into()).into()),
215+
| "cli" => crate::render::Backend::CLI,
216+
| _ => return Err(anyhow::anyhow!("no backend specified")),
241217
};
242218

243219
Ok(CallArgs {
244220
privileges,
245-
command: Command::Render(RenderArguments {
221+
command: Command::Render(crate::render::RenderArguments {
246222
configuration: config,
247223
template,
248224
value_overrides,
@@ -252,7 +228,7 @@ impl ClapArgumentLoader {
252228
}),
253229
})
254230
} else {
255-
return Err(Error::UnknownCommand.into());
231+
return Err(anyhow::anyhow!("unknown command"));
256232
}
257233
}
258234
}

src/error.rs

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod render;
2+
pub mod config;

src/main.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use {
44
std::path::PathBuf,
55
};
66

7-
pub mod args;
8-
pub mod config;
9-
pub mod error;
10-
pub mod reference;
11-
pub mod render;
7+
mod args;
8+
mod config;
9+
mod reference;
10+
mod render;
1211

1312
#[tokio::main]
1413
async fn main() -> Result<()> {
@@ -58,15 +57,14 @@ async fn main() -> Result<()> {
5857
#[cfg(test)]
5958
mod tests {
6059
use {
61-
crate::error::Error,
6260
anyhow::Result,
6361
std::process::Command,
6462
};
6563

6664
fn exec(command: &str) -> Result<String> {
6765
let output = Command::new("sh").arg("-c").arg(command).output()?;
6866
if output.status.code().unwrap() != 0 {
69-
return Err(Error::ShellCommand(String::from_utf8(output.stderr)?).into());
67+
return Err(anyhow::anyhow!(String::from_utf8(output.stderr)?));
7068
}
7169
Ok(String::from_utf8(output.stdout)?)
7270
}

src/render/cli.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use {
22
super::UserInput,
3-
crate::error::Error,
43
anyhow::Result,
54
async_trait::async_trait,
65
std::collections::{
@@ -24,11 +23,11 @@ impl<'a> UserInput for CLIBackend<'a> {
2423
async fn prompt(&self, text: &str) -> Result<String> {
2524
match dialoguer::Input::new().allow_empty(true).with_prompt(text).interact() {
2625
| Ok(res) => Ok(res),
27-
| Err(_) => Err(Error::InteractAbort.into()),
26+
| Err(_) => Err(anyhow::anyhow!("interaction aborted")),
2827
}
2928
}
3029

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

@@ -43,7 +42,7 @@ impl<'a> UserInput for CLIBackend<'a> {
4342
}
4443
}
4544

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

src/render/headless.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use {
2-
super::UserInput,
3-
crate::error::Error,
42
anyhow::Result,
53
async_trait::async_trait,
64
};
@@ -14,25 +12,25 @@ impl HeadlessBackend {
1412
}
1513

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

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

3028
async fn check(
3129
&self,
3230
_prompt: &str,
3331
_separator: &str,
34-
_options: &std::collections::BTreeMap<String, super::Option>,
32+
_options: &std::collections::BTreeMap<String, crate::config::Option>,
3533
) -> Result<String> {
36-
Err(Error::Invalid("can not prompt in headless backend".into()).into())
34+
Err(anyhow::anyhow!("can not prompt in headless backend").into())
3735
}
3836
}

0 commit comments

Comments
 (0)