Restructure files

master
Mathias Rothenhaeusler 2022-10-09 15:46:39 +02:00
parent d23365cb84
commit bc23ddceeb
6 changed files with 94 additions and 94 deletions

View File

@ -0,0 +1,2 @@
pub mod params;
pub mod response;

View File

@ -0,0 +1,61 @@
use std::env;
use clap::{App, load_yaml};
use directories::BaseDirs;
use dotenv;
use sprintf::sprintf;
pub struct Params {
pub source_lang: String,
pub target_lang: String,
key: String,
uri: String,
text: String,
}
impl Params {
pub fn new() -> Self {
let base_dir = match BaseDirs::new() {
Some(dirs) => dirs,
None => {
panic!("Please provide key in ~/.config/deepl/.env file")
}
};
let my_path = base_dir.config_dir().join("deepl/.env");
match dotenv::from_path(my_path.as_path()) {
Ok(env) => env,
Err(_) => panic!("Could not load .env file {:?}", my_path.as_path()),
};
let yaml = load_yaml!("./../../cli.yml");
let matches = App::from_yaml(yaml).get_matches();
Self {
source_lang: matches.value_of("source").unwrap_or("0").to_string(),
target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(),
key: env::var("key").unwrap(),
uri: env::var("uri").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(),
}
}
pub fn url(&self) -> String {
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()
}
pub fn body(&self) -> String {
let mut body = sprintf!(
"auth_key=%s&text=%s&target_lang=%s",
self.key,
self.text,
self.target_lang
)
.unwrap();
if self.source_lang != "0" {
let add = sprintf!("&source_lang=%s", self.source_lang).unwrap();
body.push_str(&add);
}
body
}
}

View File

@ -0,0 +1,28 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Default)]
struct Translations {
detected_source_language: String,
text: String,
}
#[derive(Serialize, Deserialize)]
pub struct DeeplResponse {
translations: Vec<Translations>,
}
impl DeeplResponse {
pub fn get_text(&self, target_language: &str) -> String {
let mut trans: String = String::new();
for translation in self.translations.iter() {
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

@ -1,92 +0,0 @@
pub mod deepl_helper {
use clap::{load_yaml, App};
use directories::BaseDirs;
use dotenv;
use serde::{Deserialize, Serialize};
use sprintf::sprintf;
use std::{env::{self}};
pub struct Params {
pub source_lang: String,
pub target_lang: String,
key: String,
uri: String,
text: String,
}
impl Params {
pub fn new() -> Self {
let base_dir = match BaseDirs::new() {
Some(dirs) => dirs,
None => {
panic!("Please provide key in ~/.config/deepl/.env file")
}
};
let my_path = base_dir.config_dir().join("deepl/.env");
match dotenv::from_path(my_path.as_path()) {
Ok(env) => env,
Err(_) => panic!("Could not load .env file {:?}", my_path.as_path()),
};
let yaml = load_yaml!("cli.yml");
let matches = App::from_yaml(yaml).get_matches();
Self {
source_lang: matches.value_of("source").unwrap_or("0").to_string(),
target_lang: matches.value_of("target").unwrap_or("EN-US").to_string(),
key: env::var("key").unwrap(),
uri: env::var("uri").unwrap(),
text: matches.value_of("INPUT").unwrap().to_string(),
}
}
pub fn url(&self) -> String {
sprintf!("%s?auth_key=%s", self.uri, self.key).unwrap()
}
pub fn body(&self) -> String {
let mut body = sprintf!(
"auth_key=%s&text=%s&target_lang=%s",
self.key,
self.text,
self.target_lang
)
.unwrap();
if self.source_lang != "0" {
let add = sprintf!("&source_lang=%s", self.source_lang).unwrap();
body.push_str(&add);
}
body
}
}
#[derive(Serialize, Deserialize, Default)]
struct Translations {
detected_source_language: String,
text: String,
}
#[derive(Serialize, Deserialize)]
pub struct DeeplResponse {
translations: Vec<Translations>,
}
impl DeeplResponse {
pub fn get_text(&self, target_language: &str) -> String {
let mut trans: String = String::new();
for translation in self.translations.iter() {
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

@ -1,8 +1,9 @@
mod engine;
use deepl::deepl_helper::{DeeplResponse, Params};
use hyper::{Body, Client, Method, Request, body};
use hyper_tls::HttpsConnector;
use crate::engine::params::Params;
use crate::engine::response::DeeplResponse;
#[tokio::main(flavor = "current_thread")]