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}};
pub struct Params {
source_lang: String,
target_lang: String,
file: String,
pub source_lang: String,
pub target_lang: String,
key: String,
uri: String,
text: String,
@ -36,7 +35,6 @@ pub mod deepl_helper {
Self {
source_lang: matches.value_of("source").unwrap_or("0").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(),
uri: env::var("uri").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(),
@ -77,11 +75,13 @@ pub mod deepl_helper {
}
impl DeeplResponse {
pub fn get_text(&self) -> String {
let mut trans = "\n".to_string();
pub fn get_text(&self, target_language: &str) -> String {
let mut trans: String = String::new();
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
}

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 = String::from_utf8(body_bytes.to_vec()).expect("response was not valid utf-8");
let v: DeeplResponse = serde_json::from_str(&body)?;
println!("{}", v.get_text());
println!("{}", v.get_text(&params.target_lang));
Ok(())
}