Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9e0a4d087b | ||
|
|
e18a62267f | ||
|
|
6fd9e66ce8 | ||
|
|
53682cf1b7 | ||
|
|
5db6014654 | ||
|
|
6e7b2e9cc5 | ||
|
|
2b76f50d4c | ||
|
|
66363879c8 |
@@ -22,7 +22,7 @@ and a pull request. See below for making changes on your own.
|
|||||||
1. Create a new issue with a summary of your proposed changes and more.
|
1. Create a new issue with a summary of your proposed changes and more.
|
||||||
2. Fork the repository.
|
2. Fork the repository.
|
||||||
3. Implement your changes
|
3. Implement your changes
|
||||||
4. Test your changes. Run `cargo fmt`, `cargo check` ad `cargo clippy` to verify it works.
|
4. Test your changes. Run `cargo fmt`, `cargo check` and `cargo clippy` to verify it works.
|
||||||
5. Create a pull request from your fork.
|
5. Create a pull request from your fork.
|
||||||
6. If it gets accepted, your changes will be in the next release.
|
6. If it gets accepted, your changes will be in the next release.
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -5,7 +5,7 @@
|
|||||||
Localizer helps localize (translate) your rust applications using json files.
|
Localizer helps localize (translate) your rust applications using json files.
|
||||||
</h3>
|
</h3>
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://img.shields.io/crates/v/qstash-rs">
|
<img src="https://img.shields.io/crates/v/localizer-rs">
|
||||||
<img src="https://www.codefactor.io/repository/github/ElBe-Development/localizer-rs/badge">
|
<img src="https://www.codefactor.io/repository/github/ElBe-Development/localizer-rs/badge">
|
||||||
<img src="https://github.com/ElBe-Development/localizer-rs/actions/workflows/megalinter.yml/badge.svg?branch=main&event=push">
|
<img src="https://github.com/ElBe-Development/localizer-rs/actions/workflows/megalinter.yml/badge.svg?branch=main&event=push">
|
||||||
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit">
|
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit">
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
| Version | Supported |
|
| Version | Supported |
|
||||||
| -------- | ------------------ |
|
| -------- | ------------------ |
|
||||||
| `v1.0.0` | :white_check_mark: |
|
| `v1.0.0` | :white_check_mark: |
|
||||||
|
| `v1.1.0` | :white_check_mark: |
|
||||||
|
|
||||||
## Reporting a Vulnerability
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "localizer-rs"
|
name = "localizer-rs"
|
||||||
description = "Localizer helps localize (translate) your rust applications using json files."
|
description = "Localizer helps localize (translate) your rust applications using json files."
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
authors = [
|
authors = [
|
||||||
"ElBe-Plaq <elbe.dev.plaq@gmail.com>"
|
"ElBe-Plaq <elbe.dev.plaq@gmail.com>"
|
||||||
]
|
]
|
||||||
|
|||||||
+11
-2
@@ -3,8 +3,17 @@ use localizer_rs;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
||||||
|
|
||||||
println!("{:}", config.t("error", vec![("details", "Something went wrong when trying to do stuff")]));
|
println!(
|
||||||
println!("{:}", config.t("success", vec![("balance", "$10"), ("user", "John Doe")]));
|
"{:}",
|
||||||
|
config.t(
|
||||||
|
"error",
|
||||||
|
vec![("details", "Something went wrong when trying to do stuff")]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"{:}",
|
||||||
|
config.t("success", vec![("balance", "$10"), ("user", "John Doe")])
|
||||||
|
);
|
||||||
|
|
||||||
println!("{:}", config.t("all", vec![]));
|
println!("{:}", config.t("all", vec![]));
|
||||||
}
|
}
|
||||||
+131
@@ -0,0 +1,131 @@
|
|||||||
|
// localizer-rs errors
|
||||||
|
// Version: 1.1.0
|
||||||
|
|
||||||
|
// Copyright (c) 2023-present ElBe Development.
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the 'Software'),
|
||||||
|
// to deal in the Software without restriction, including without limitation
|
||||||
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
// and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
// Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
////////////////////////////////
|
||||||
|
// IMPORTS AND USE STATEMENTS //
|
||||||
|
////////////////////////////////
|
||||||
|
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
|
||||||
|
///////////
|
||||||
|
// ERROR //
|
||||||
|
///////////
|
||||||
|
|
||||||
|
/// Error object.
|
||||||
|
///
|
||||||
|
/// Use [`Error::new()`] to create error objects instead of using this struct.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// - `name`: The errors name.
|
||||||
|
/// - `description`: The error description.
|
||||||
|
/// - `exit_code`: The errors exit code.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A new `Error` object with the specified name and description.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use localizer_rs;
|
||||||
|
/// localizer_rs::errors::Error {
|
||||||
|
/// name: "name".to_owned(),
|
||||||
|
/// description: "description".to_owned(),
|
||||||
|
/// exit_code: 1
|
||||||
|
/// };
|
||||||
|
/// ```
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
pub struct Error {
|
||||||
|
pub name: String,
|
||||||
|
pub description: String,
|
||||||
|
pub exit_code: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "\x1b[31;1m{}\x1b[0m: {}", self.name, self.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error {
|
||||||
|
/// Creates a new error object.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// - `name`: The errors name.
|
||||||
|
/// - `description`: The error description.
|
||||||
|
/// - `exit_code`: The errors exit code.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// A new `Error` object with the specified name and description.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use localizer_rs;
|
||||||
|
/// localizer_rs::errors::Error::new("name", "description", 1);
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # See also
|
||||||
|
///
|
||||||
|
/// - [`Error`]
|
||||||
|
pub fn new(name: &str, description: &str, exit_code: i32) -> Error {
|
||||||
|
return Error {
|
||||||
|
name: name.to_owned(),
|
||||||
|
description: description.to_owned(),
|
||||||
|
exit_code: exit_code,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Raises the error and exits with the specified exit code.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// - `self`: The error object.
|
||||||
|
/// - `details`: The error details.
|
||||||
|
///
|
||||||
|
/// # Aborts
|
||||||
|
///
|
||||||
|
/// Exits with the specified exit code.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```should_panic
|
||||||
|
/// # use localizer_rs;
|
||||||
|
/// # let error: localizer_rs::errors::Error = localizer_rs::errors::Error::new("name", "description", 1);
|
||||||
|
/// error.raise("Something went very wrong");
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// # See also
|
||||||
|
///
|
||||||
|
/// - [`Error`]
|
||||||
|
pub fn raise(&self, details: &str) {
|
||||||
|
eprintln!("{}", self);
|
||||||
|
eprintln!("{}", details);
|
||||||
|
|
||||||
|
std::process::exit(self.exit_code);
|
||||||
|
}
|
||||||
|
}
|
||||||
+142
-31
@@ -1,5 +1,6 @@
|
|||||||
|
#![doc = ".github/README.md"]
|
||||||
// localizer-rs
|
// localizer-rs
|
||||||
// Version: 1.0.0
|
// Version: 1.1.0
|
||||||
|
|
||||||
// Copyright (c) 2023-present ElBe Development.
|
// Copyright (c) 2023-present ElBe Development.
|
||||||
|
|
||||||
@@ -21,6 +22,13 @@
|
|||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
/////////////
|
||||||
|
// EXPORTS //
|
||||||
|
/////////////
|
||||||
|
|
||||||
|
pub mod errors;
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
// IMPORTS AND USE STATEMENTS //
|
// IMPORTS AND USE STATEMENTS //
|
||||||
////////////////////////////////
|
////////////////////////////////
|
||||||
@@ -28,7 +36,8 @@
|
|||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::BufReader;
|
use std::io::BufReader;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::process::exit;
|
|
||||||
|
use errors as error_lib;
|
||||||
|
|
||||||
use serde_json;
|
use serde_json;
|
||||||
|
|
||||||
@@ -62,7 +71,7 @@ use serde_json;
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub language: String
|
pub language: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -99,8 +108,9 @@ impl Config {
|
|||||||
pub fn new(path: &str, language: &str) -> Config {
|
pub fn new(path: &str, language: &str) -> Config {
|
||||||
let mut config: Config = Config {
|
let mut config: Config = Config {
|
||||||
path: "".to_string(),
|
path: "".to_string(),
|
||||||
language: "".to_string()
|
language: "".to_string(),
|
||||||
}.to_owned();
|
}
|
||||||
|
.to_owned();
|
||||||
config = config.set_language(language).to_owned();
|
config = config.set_language(language).to_owned();
|
||||||
config = config.set_path(path).to_owned();
|
config = config.set_path(path).to_owned();
|
||||||
|
|
||||||
@@ -111,7 +121,7 @@ impl Config {
|
|||||||
///
|
///
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// - `self`: The config object.
|
/// - `self`: The config object. This must be mutable.
|
||||||
/// - `str_path`: The directory containing the translation files. The directory is relative to the path the executable was executed from.
|
/// - `str_path`: The directory containing the translation files. The directory is relative to the path the executable was executed from.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
@@ -126,7 +136,7 @@ impl Config {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use localizer_rs;
|
/// # use localizer_rs;
|
||||||
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
/// # let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||||
/// config.set_path("examples");
|
/// config.set_path("examples");
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -139,17 +149,27 @@ impl Config {
|
|||||||
match path.try_exists() {
|
match path.try_exists() {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
if !value {
|
if !value {
|
||||||
eprintln!("Translation path {:?} does not exist", str_path);
|
let error: error_lib::Error =
|
||||||
exit(1);
|
error_lib::Error::new("OS Error", "Translation path was not found", 1);
|
||||||
|
error.raise(format!("Path: {:?}", str_path).as_str());
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Err(error) => {
|
Err(_error) => {
|
||||||
eprintln!("Can't open translation path {:?}: {}", str_path, error);
|
let error: error_lib::Error =
|
||||||
exit(2);
|
error_lib::Error::new("OS Error", "Could not open path", 2);
|
||||||
|
error.raise(format!("Path: {:?}\nDetails: {}", str_path, _error).as_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.path = String::from(path.to_owned().to_str().expect("Expected valid path"));
|
self.path = String::from(match path.to_owned().to_str() {
|
||||||
|
Some(value) => value,
|
||||||
|
None => {
|
||||||
|
let error: error_lib::Error =
|
||||||
|
error_lib::Error::new("OS Error", "Path does not seem to be valid", 3);
|
||||||
|
error.raise(format!("Path: {:?}", str_path).as_str());
|
||||||
|
""
|
||||||
|
}
|
||||||
|
});
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +177,7 @@ impl Config {
|
|||||||
///
|
///
|
||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// - `self`: The config object.
|
/// - `self`: The config object. This must be mutable.
|
||||||
/// - `language`: The language to translate to.
|
/// - `language`: The language to translate to.
|
||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
@@ -168,7 +188,7 @@ impl Config {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use localizer_rs;
|
/// # use localizer_rs;
|
||||||
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
/// # let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||||
/// config.set_language("en");
|
/// config.set_language("en");
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
@@ -209,39 +229,130 @@ impl Config {
|
|||||||
|
|
||||||
fn translate<T>(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String
|
fn translate<T>(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String
|
||||||
where
|
where
|
||||||
T: serde::Serialize + for<'de> serde::Deserialize<'de>
|
T: serde::Serialize + for<'de> serde::Deserialize<'de>,
|
||||||
{
|
{
|
||||||
let mut colors: Vec<(&str, &str)> = vec![
|
let mut colors: Vec<(&str, &str)> = vec![
|
||||||
// Formatting codes
|
// Formatting codes
|
||||||
("end", "\x1b[0m"), ("bold", "\x1b[1m"), ("italic", "\x1b[3m"), ("underline", "\x1b[4m"), ("overline", "\x1b[53m"),
|
("end", "\x1b[0m"),
|
||||||
|
("bold", "\x1b[1m"),
|
||||||
|
("italic", "\x1b[3m"),
|
||||||
|
("underline", "\x1b[4m"),
|
||||||
|
("overline", "\x1b[53m"),
|
||||||
|
|
||||||
// Foreground colors
|
// Foreground colors
|
||||||
("color.black", "\x1b[30m"), ("color.red", "\x1b[31m"), ("color.green", "\x1b[32m"), ("color.yellow", "\x1b[33m"),
|
("color.black", "\x1b[30m"),
|
||||||
("color.blue", "\x1b[34m"), ("color.magenta", "\x1b[35m"), ("color.cyan", "\x1b[36m"), ("color.white", "\x1b[37m"),
|
("color.red", "\x1b[31m"),
|
||||||
|
("color.green", "\x1b[32m"),
|
||||||
|
("color.yellow", "\x1b[33m"),
|
||||||
|
("color.blue", "\x1b[34m"),
|
||||||
|
("color.magenta", "\x1b[35m"),
|
||||||
|
("color.cyan", "\x1b[36m"),
|
||||||
|
("color.white", "\x1b[37m"),
|
||||||
|
|
||||||
// Bright foreground colors
|
// Bright foreground colors
|
||||||
("color.bright_black", "\x1b[90m"), ("color.bright_red", "\x1b[91m"), ("color.bright_green", "\x1b[92m"),
|
("color.bright_black", "\x1b[90m"),
|
||||||
("color.bright_yellow", "\x1b[93m"), ("color.bright_blue", "\x1b[94m"), ("color.bright_magenta", "\x1b[95m"),
|
("color.bright_red", "\x1b[91m"),
|
||||||
("color.bright_cyan", "\x1b[96m"), ("color.bright_white", "\x1b[97m"),
|
("color.bright_green", "\x1b[92m"),
|
||||||
|
("color.bright_yellow", "\x1b[93m"),
|
||||||
|
("color.bright_blue", "\x1b[94m"),
|
||||||
|
("color.bright_magenta", "\x1b[95m"),
|
||||||
|
("color.bright_cyan", "\x1b[96m"),
|
||||||
|
("color.bright_white", "\x1b[97m"),
|
||||||
|
|
||||||
// Background colors
|
// Background colors
|
||||||
("back.black", "\x1b[40m"), ("back.red", "\x1b[41m"), ("back.green", "\x1b[42m"), ("back.yellow", "\x1b[43m"),
|
("back.black", "\x1b[40m"),
|
||||||
("back.blue", "\x1b[44m"), ("back.magenta", "\x1b[45m"), ("back.cyan", "\x1b[46m"), ("back.white", "\x1b[47m"),
|
("back.red", "\x1b[41m"),
|
||||||
|
("back.green", "\x1b[42m"),
|
||||||
|
("back.yellow", "\x1b[43m"),
|
||||||
|
("back.blue", "\x1b[44m"),
|
||||||
|
("back.magenta", "\x1b[45m"),
|
||||||
|
("back.cyan", "\x1b[46m"),
|
||||||
|
("back.white", "\x1b[47m"),
|
||||||
|
|
||||||
// Bright background colors
|
// Bright background colors
|
||||||
("back.bright_black", "\x1b[100m"), ("back.bright_red", "\x1b[101m"), ("back.bright_green", "\x1b[102m"),
|
("back.bright_black", "\x1b[100m"),
|
||||||
("back.bright_yellow", "\x1b[103m"), ("back.bright_blue", "\x1b[104m"), ("back.bright_magenta", "\x1b[105m"),
|
("back.bright_red", "\x1b[101m"),
|
||||||
("back.bright_cyan", "\x1b[106m"), ("back.bright_white", "\x1b[107m"),
|
("back.bright_green", "\x1b[102m"),
|
||||||
|
("back.bright_yellow", "\x1b[103m"),
|
||||||
|
("back.bright_blue", "\x1b[104m"),
|
||||||
|
("back.bright_magenta", "\x1b[105m"),
|
||||||
|
("back.bright_cyan", "\x1b[106m"),
|
||||||
|
("back.bright_white", "\x1b[107m"),
|
||||||
];
|
];
|
||||||
arguments.append(&mut colors);
|
arguments.append(&mut colors);
|
||||||
|
|
||||||
let file: File = File::open(Path::new(format!("./{}/{}.json", &self.path, &self.language).as_str())).unwrap();
|
let file: File = match File::open(Path::new(
|
||||||
|
format!("./{}/{}.json", &self.path, &self.language).as_str(),
|
||||||
|
)) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_error) => {
|
||||||
|
let error: errors::Error =
|
||||||
|
errors::Error::new("OS Error", "Could not open translation file", 4);
|
||||||
|
error.raise(
|
||||||
|
format!(
|
||||||
|
"File: ./{}/{}.json\nError: {}",
|
||||||
|
&self.path, &self.language, _error
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return "".to_owned();
|
||||||
|
}
|
||||||
|
};
|
||||||
let reader: BufReader<File> = BufReader::new(file);
|
let reader: BufReader<File> = BufReader::new(file);
|
||||||
|
|
||||||
let json: serde_json::Value = serde_json::to_value::<T>(serde_json::from_reader::<BufReader<File>, T>(reader).unwrap()).unwrap().to_owned();
|
let json: serde_json::Value = match serde_json::to_value::<T>(
|
||||||
|
match serde_json::from_reader::<BufReader<File>, T>(reader) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_error) => {
|
||||||
|
let error: errors::Error = errors::Error::new(
|
||||||
|
"Parsing error",
|
||||||
|
"Translation file could not be parsed",
|
||||||
|
5,
|
||||||
|
);
|
||||||
|
error.raise(
|
||||||
|
format!(
|
||||||
|
"File: ./{}/{}.json\nError: {}",
|
||||||
|
&self.path, &self.language, _error
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return "".to_owned();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(_error) => {
|
||||||
|
let error: errors::Error =
|
||||||
|
errors::Error::new("Converting error", "Could not convert to json value", 6);
|
||||||
|
error.raise(
|
||||||
|
format!(
|
||||||
|
"File: ./{}/{}.json\nError: {}",
|
||||||
|
&self.path, &self.language, _error
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return "".to_owned();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.to_owned();
|
||||||
let mut result: String = match json[key].as_str() {
|
let mut result: String = match json[key].as_str() {
|
||||||
Some(value) => value.to_string(),
|
Some(value) => value.to_string(),
|
||||||
None => "".to_string()
|
None => {
|
||||||
|
let error: errors::Error =
|
||||||
|
errors::Error::new("Indexing error", "Could not index json value", 6);
|
||||||
|
error.raise(
|
||||||
|
format!(
|
||||||
|
"Index: {}\nFile: ./{}/{}.json",
|
||||||
|
key, &self.path, &self.language
|
||||||
|
)
|
||||||
|
.as_str(),
|
||||||
|
);
|
||||||
|
|
||||||
|
return "".to_owned();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (key, value) in arguments {
|
for (key, value) in arguments {
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
// localizer-rs error tests
|
||||||
|
// Version: 1.1.0
|
||||||
|
|
||||||
|
// Copyright (c) 2023-present ElBe Development.
|
||||||
|
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||||
|
// copy of this software and associated documentation files (the 'Software'),
|
||||||
|
// to deal in the Software without restriction, including without limitation
|
||||||
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
// and/or sell copies of the Software, and to permit persons to whom the
|
||||||
|
// Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
// The above copyright notice and this permission notice shall be included in
|
||||||
|
// all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
// THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||||
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
|
////////////////////////////////
|
||||||
|
// IMPORTS AND USE STATEMENTS //
|
||||||
|
////////////////////////////////
|
||||||
|
|
||||||
|
#[allow(unused_imports)]
|
||||||
|
use localizer_rs;
|
||||||
|
|
||||||
|
|
||||||
|
///////////
|
||||||
|
// TESTS //
|
||||||
|
///////////
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn test_error() {
|
||||||
|
let error: localizer_rs::errors::Error =
|
||||||
|
localizer_rs::errors::Error::new("name", "description", 1);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
error,
|
||||||
|
localizer_rs::errors::Error {
|
||||||
|
name: "name".to_owned(),
|
||||||
|
description: "description".to_owned(),
|
||||||
|
exit_code: 1
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[ignore]
|
||||||
|
fn raise_helper() {
|
||||||
|
let error: localizer_rs::errors::Error =
|
||||||
|
localizer_rs::errors::Error::new("name", "description", 1);
|
||||||
|
error.raise("details");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_raise() {
|
||||||
|
let status = std::process::Command::new("cargo")
|
||||||
|
.args(&["test", "--", "--ignored"])
|
||||||
|
.stdout(std::process::Stdio::null())
|
||||||
|
.stderr(std::process::Stdio::null())
|
||||||
|
.status()
|
||||||
|
.expect("Unable to run program");
|
||||||
|
|
||||||
|
assert_eq!(Some(1), status.code())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
// localizer-rs tests
|
// localizer-rs tests
|
||||||
// Version: 1.0.0-alpha1
|
// Version: 1.1.0
|
||||||
|
|
||||||
// Copyright (c) 2023-present ElBe Development.
|
// Copyright (c) 2023-present ElBe Development.
|
||||||
|
|
||||||
@@ -39,33 +39,43 @@ mod tests {
|
|||||||
fn test_config() {
|
fn test_config() {
|
||||||
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
||||||
|
|
||||||
assert_eq!(config, localizer_rs::Config {
|
assert_eq!(
|
||||||
|
config,
|
||||||
|
localizer_rs::Config {
|
||||||
path: "examples/translations".to_owned(),
|
path: "examples/translations".to_owned(),
|
||||||
language: "en".to_owned()
|
language: "en".to_owned()
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_path() {
|
fn test_set_path() {
|
||||||
let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
let mut config: localizer_rs::Config =
|
||||||
|
localizer_rs::Config::new("examples/translations", "en");
|
||||||
config.set_path("examples");
|
config.set_path("examples");
|
||||||
|
|
||||||
assert_eq!(config, localizer_rs::Config {
|
assert_eq!(
|
||||||
|
config,
|
||||||
|
localizer_rs::Config {
|
||||||
path: "examples".to_owned(),
|
path: "examples".to_owned(),
|
||||||
language: "en".to_owned()
|
language: "en".to_owned()
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_set_language() {
|
fn test_set_language() {
|
||||||
let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
let mut config: localizer_rs::Config =
|
||||||
|
localizer_rs::Config::new("examples/translations", "en");
|
||||||
config.set_language("not_en");
|
config.set_language("not_en");
|
||||||
|
|
||||||
assert_eq!(config, localizer_rs::Config {
|
assert_eq!(
|
||||||
|
config,
|
||||||
|
localizer_rs::Config {
|
||||||
path: "examples/translations".to_owned(),
|
path: "examples/translations".to_owned(),
|
||||||
language: "not_en".to_owned()
|
language: "not_en".to_owned()
|
||||||
});
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -73,6 +83,9 @@ mod tests {
|
|||||||
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
|
||||||
let translation: String = config.t("error", vec![("details", "Something went wrong")]);
|
let translation: String = config.t("error", vec![("details", "Something went wrong")]);
|
||||||
|
|
||||||
assert_eq!(translation.as_str(), "\x1b[31m\x1b[1mError:\x1b[0m Something went wrong");
|
assert_eq!(
|
||||||
|
translation.as_str(),
|
||||||
|
"\x1b[31m\x1b[1mError:\x1b[0m Something went wrong"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user