added cli struct to replace clap yaml

master
Mathias Rothenhaeusler 2026-01-19 20:09:33 +01:00
parent 7dcf5bb201
commit 498c963c20
5 changed files with 190 additions and 106 deletions

199
Cargo.lock generated
View File

@ -3,12 +3,53 @@
version = 4
[[package]]
name = "ansi_term"
version = "0.11.0"
name = "anstream"
version = "0.6.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a"
dependencies = [
"winapi",
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78"
[[package]]
name = "anstyle-parse"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
@ -17,17 +58,6 @@ version = "1.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61"
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.0.1"
@ -60,20 +90,50 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.33.3"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
"yaml-rust",
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.54"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.114",
]
[[package]]
name = "clap_lex"
version = "0.7.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32"
[[package]]
name = "colorchoice"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75"
[[package]]
name = "core-foundation"
version = "0.9.1"
@ -229,6 +289,12 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "hermit-abi"
version = "0.1.19"
@ -328,6 +394,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itoa"
version = "0.4.8"
@ -435,6 +507,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "openssl"
version = "0.10.36"
@ -519,18 +597,18 @@ checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "proc-macro2"
version = "1.0.29"
version = "1.0.105"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9f5105d4fdaab20335ca9565e106a5d9b82b6219b5ba735731124ac6711d23d"
checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7"
dependencies = [
"unicode-xid",
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.9"
version = "1.0.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a"
dependencies = [
"proc-macro2",
]
@ -665,7 +743,7 @@ checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.76",
]
[[package]]
@ -718,9 +796,9 @@ checksum = "7e0c270adf444e0025dea43f5f45c17ef9bf0e603d58381b95dc79cf9bfc0cea"
[[package]]
name = "strsim"
version = "0.8.0"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f"
[[package]]
name = "syn"
@ -733,6 +811,17 @@ dependencies = [
"unicode-xid",
]
[[package]]
name = "syn"
version = "2.0.114"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.2.0"
@ -747,15 +836,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "tokio"
version = "1.11.0"
@ -784,7 +864,7 @@ checksum = "54473be61f4ebe4efd09cec9bd5d16fa51d70ea0192213d754d2d500457db110"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 1.0.76",
]
[[package]]
@ -844,10 +924,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642"
[[package]]
name = "unicode-width"
version = "0.1.9"
name = "unicode-ident"
version = "1.0.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5"
[[package]]
name = "unicode-xid"
@ -855,18 +935,18 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "want"
version = "0.3.0"
@ -906,7 +986,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "yaml-rust"
version = "0.3.5"
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e66366e18dc58b46801afbf2ca7661a9f59cc8c5962c29892b6039b4f86fa992"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]

View File

@ -11,7 +11,7 @@ hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
clap = {version = "~2.33.0", features = ["yaml"]}
clap = { version = "4", features = ["derive"] }
directories = "4.0"
dotenv = "0.15.0"
sprintf = "0.1.1"

View File

@ -1,18 +1,18 @@
deepl 1.0
Mathias Rothenhaeusler <safemind@posteo.net>
# Deepl
Cli API for deepl translations. Create a .env config file in your user config folder (e.g. ~/config/deepl/.env).
Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg).
Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg). <br>
USAGE:
deepl [OPTIONS] <INPUT>
USAGE:<br>
&emsp;deepl [OPTIONS] \<INPUT><br>
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
FLAGS:<br>
&emsp;-h, --help Prints help information<br>
&emsp;-V, --version Prints version information<br>
OPTIONS:
-s, --source <source_lang> The language you want to translate. Default is autodetect.
-t, --target <target_lang> The language your text should be tranlated to. Default is DE.
OPTIONS:<br>
&emsp;-s, --source <source_lang> The language you want to translate. Default is autodectect.<br>
&emsp;-t, --target <target_lang> The language your text should be tranlated to. Default is DE.<br>
ARGS:
<INPUT> Text to translate
ARGS:<br>
&emsp;\<INPUT> Text to translate<br>

24
cli.yml
View File

@ -1,24 +0,0 @@
name: deepl
version: "1.0"
author: Mathias Rothenhaeusler <safemind@posteo.net>
about: Cli API for deepl translations. Create a .env config file in your user config folder (e.g. ~/config/deepl/.env). Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg).
args:
- source:
short: s
long: source
value_name: source_lang
help: The language you want to translate. Default is autodetect.
takes_value: true
required: false
- target:
short: t
long: target
value_name: target_lang
help: The language your text should be tranlated to. Default is DE.
takes_value: true
required: false
- INPUT:
help: Text to translate
required: true
index: 1

View File

@ -1,9 +1,28 @@
use anyhow::{anyhow, Context, Result};
use clap::{load_yaml, App, ArgMatches};
use clap::Parser;
use directories::BaseDirs;
use dotenv;
use sprintf::sprintf;
use std::{env, path::PathBuf};
use std::env;
#[derive(Parser, Debug)]
#[command(
name = "deepl",
version,
about = "Cli API for deepl translations. Create a .env config file in your user config folder (e.g. ~/config/deepl/.env). Required keys URI (eg. https://api-free.deepl.com/v2/translate) and KEY (eg. XYZ:rg)."
)]
pub struct Cli {
/// Source language (e.g. EN)
#[arg(short, long, default_value = "0")]
pub source: String,
/// Target language (e.g. DE)
#[arg(short, long, default_value = "DE")]
pub target: String,
/// Text to translate
pub input: String,
}
pub struct Params {
pub source_lang: String,
@ -15,22 +34,22 @@ pub struct Params {
impl Params {
pub fn new() -> Result<Self> {
let base_dir: BaseDirs = BaseDirs::new().context("failed to determine base directories")?;
let cli = Cli::parse();
let my_path: PathBuf = base_dir.config_dir().join("deepl/.env");
let base_dirs = BaseDirs::new()
.context("failed to determine base directories")?;
dotenv::from_path(my_path.as_path())
.context(format!("Missing .env file {}", my_path.display()))?;
let env_path = base_dirs.config_dir().join("deepl/.env");
let yaml = load_yaml!("./../../cli.yml");
let matches: ArgMatches = App::from_yaml(yaml).get_matches();
dotenv::from_path(&env_path)
.with_context(|| format!("failed to load env file at {}", env_path.display()))?;
Ok(Self {
source_lang: matches.value_of("source").unwrap_or("0").to_string(),
target_lang: matches.value_of("target").unwrap_or("DE").to_string(),
key: env::var("KEY").context("KEY missing")?,
uri: env::var("URI").context("URI missing")?,
text: matches.value_of("INPUT").context("input missing")?.to_string(),
source_lang: cli.source,
target_lang: cli.target,
text: cli.input,
key: env::var("KEY").context("KEY env var missing")?,
uri: env::var("URI").context("URI env var missing")?,
})
}