some improvements

master
Mathias Rothenhaeusler 2026-01-24 12:43:56 +01:00
parent db5a1aa674
commit 6907656cf2
5 changed files with 11 additions and 13 deletions

2
Cargo.lock generated
View File

@ -152,7 +152,7 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]] [[package]]
name = "deepl" name = "deepl"
version = "1.0.3" version = "1.0.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"clap", "clap",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "deepl" name = "deepl"
version = "1.0.3" version = "1.0.4"
edition = "2024" edition = "2024"
license = "MIT OR Apache-2.0" license = "MIT OR Apache-2.0"
authors = ["Mathias Rothenhäusler <safemind@posteo.net"] authors = ["Mathias Rothenhäusler <safemind@posteo.net"]

View File

@ -28,7 +28,7 @@ pub struct Params {
pub source_lang: String, pub source_lang: String,
pub target_lang: String, pub target_lang: String,
pub key: String, pub key: String,
uri: String, pub uri: String,
pub input: String, pub input: String,
} }
@ -53,13 +53,6 @@ impl Params {
}) })
} }
pub fn url(&self) -> Result<String> {
let url = sprintf!("%s", self.uri)
.map_err(|e| anyhow!("could not create request body: {e:?}"))?;
Ok(url)
}
pub fn body(&self) -> Result<String> { pub fn body(&self) -> Result<String> {
let mut body = sprintf!( let mut body = sprintf!(
"text=%s&target_lang=%s", "text=%s&target_lang=%s",

View File

@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::engine::params::Params; use crate::engine::params::Params;
use anyhow::Result; use anyhow::{Result, anyhow};
#[derive(Serialize, Deserialize, Default)] #[derive(Serialize, Deserialize, Default)]
struct Translations { struct Translations {
@ -16,7 +16,6 @@ pub struct DeeplResponse {
impl DeeplResponse { impl DeeplResponse {
pub fn get_text(&self, params: &Params) -> Result<String> { pub fn get_text(&self, params: &Params) -> Result<String> {
let max_src = self let max_src = self
.translations .translations
.iter() .iter()
@ -27,6 +26,12 @@ impl DeeplResponse {
let mut out = String::new(); let mut out = String::new();
for t in &self.translations { for t in &self.translations {
if t.detected_source_language == params.target_lang {
return Err(anyhow!(
"Add target language, source and target identical ({})",
params.target_lang
));
}
out.push_str(&format!( out.push_str(&format!(
"{src} >>> {:<width$} \n{tgt} >>> {}\n", "{src} >>> {:<width$} \n{tgt} >>> {}\n",
params.input, params.input,

View File

@ -15,7 +15,7 @@ async fn main() -> Result<()> {
let request = Request::builder() let request = Request::builder()
.method(Method::POST) .method(Method::POST)
.uri(params.url()?) .uri(&params.uri)
.header("Authorization", format!("DeepL-Auth-Key {}", params.key)) .header("Authorization", format!("DeepL-Auth-Key {}", params.key))
.header("content-type", "application/x-www-form-urlencoded") .header("content-type", "application/x-www-form-urlencoded")
.body(Body::from(params.body()?))?; .body(Body::from(params.body()?))?;