optimize output

master
Mathias Rothenhaeusler 2022-10-01 16:50:29 +02:00
parent 3c92b26599
commit 6222cff418
2 changed files with 10 additions and 10 deletions

View File

@ -7,9 +7,8 @@ pub mod deepl_helper {
use std::{env::{self}}; use std::{env::{self}};
pub struct Params { pub struct Params {
source_lang: String, pub source_lang: String,
target_lang: String, pub target_lang: String,
file: String,
key: String, key: String,
uri: String, uri: String,
text: String, text: String,
@ -36,7 +35,6 @@ pub mod deepl_helper {
Self { Self {
source_lang: matches.value_of("source").unwrap_or("0").to_string(), source_lang: matches.value_of("source").unwrap_or("0").to_string(),
target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(), target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(),
file: matches.value_of("file").unwrap_or_default().to_string(),
key: env::var("key").unwrap(), key: env::var("key").unwrap(),
uri: env::var("uri").unwrap(), uri: env::var("uri").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(), text: matches.value_of("INPUT").unwrap().to_string(),
@ -54,7 +52,7 @@ pub mod deepl_helper {
self.text, self.text,
self.target_lang self.target_lang
) )
.unwrap(); .unwrap();
if self.source_lang != "0" { if self.source_lang != "0" {
let add = sprintf!("&source_lang=%s", self.source_lang).unwrap(); let add = sprintf!("&source_lang=%s", self.source_lang).unwrap();
@ -77,13 +75,15 @@ pub mod deepl_helper {
} }
impl DeeplResponse { impl DeeplResponse {
pub fn get_text(&self) -> String { pub fn get_text(&self, target_language: &str) -> String {
let mut trans = "\n".to_string(); let mut trans: String = String::new();
for translation in self.translations.iter() { for translation in self.translations.iter() {
trans.push_str(&translation.text); trans.push_str(&format!("--- Detected Source language {} -------------------------\n", translation.detected_source_language));
trans.push_str(&format!("{}: {}", target_language, translation.text));
trans.push_str("\n"); trans.push_str("\n");
trans.push_str("---------------------------------------------------------\n");
} }
trans trans
} }
} }
} }

View File

@ -22,6 +22,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let body_bytes = body::to_bytes(res.into_body()).await?; let body_bytes = body::to_bytes(res.into_body()).await?;
let body = String::from_utf8(body_bytes.to_vec()).expect("response was not valid utf-8"); let body = String::from_utf8(body_bytes.to_vec()).expect("response was not valid utf-8");
let v: DeeplResponse = serde_json::from_str(&body)?; let v: DeeplResponse = serde_json::from_str(&body)?;
println!("{}", v.get_text()); println!("{}", v.get_text(&params.target_lang));
Ok(()) Ok(())
} }