some improvements
parent
db5a1aa674
commit
6907656cf2
|
|
@ -152,7 +152,7 @@ checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
|
|||
|
||||
[[package]]
|
||||
name = "deepl"
|
||||
version = "1.0.3"
|
||||
version = "1.0.4"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "deepl"
|
||||
version = "1.0.3"
|
||||
version = "1.0.4"
|
||||
edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
authors = ["Mathias Rothenhäusler <safemind@posteo.net"]
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ pub struct Params {
|
|||
pub source_lang: String,
|
||||
pub target_lang: String,
|
||||
pub key: String,
|
||||
uri: String,
|
||||
pub uri: 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> {
|
||||
let mut body = sprintf!(
|
||||
"text=%s&target_lang=%s",
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::engine::params::Params;
|
||||
use anyhow::Result;
|
||||
use anyhow::{Result, anyhow};
|
||||
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
struct Translations {
|
||||
|
|
@ -16,7 +16,6 @@ pub struct DeeplResponse {
|
|||
|
||||
impl DeeplResponse {
|
||||
pub fn get_text(&self, params: &Params) -> Result<String> {
|
||||
|
||||
let max_src = self
|
||||
.translations
|
||||
.iter()
|
||||
|
|
@ -27,6 +26,12 @@ impl DeeplResponse {
|
|||
let mut out = String::new();
|
||||
|
||||
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!(
|
||||
"{src} >>> {:<width$} \n{tgt} >>> {}\n",
|
||||
params.input,
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ async fn main() -> Result<()> {
|
|||
|
||||
let request = Request::builder()
|
||||
.method(Method::POST)
|
||||
.uri(params.url()?)
|
||||
.uri(¶ms.uri)
|
||||
.header("Authorization", format!("DeepL-Auth-Key {}", params.key))
|
||||
.header("content-type", "application/x-www-form-urlencoded")
|
||||
.body(Body::from(params.body()?))?;
|
||||
|
|
|
|||
Loading…
Reference in New Issue