Skip to content

Commit

Permalink
draft error implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
orzklv committed Aug 14, 2024
1 parent 8d9bc6d commit ac8d645
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "orzklv"
version = "0.0.8"
version = "0.1.8"
edition = "2021"
homepage = "https://github.com/dark-voyage/liborzklv"
repository = "https://github.com/dark-voyage/liborzklv"
Expand Down Expand Up @@ -29,6 +29,7 @@ url = { version = "2.5.0", optional = true }
tokio = { version = "1.36.0", optional = true }
async-trait = { version = "0.1.77", optional = true }
toml = { version = "0.8", optional = true }
thiserror = "1"

[dev-dependencies]
serde = "1.0.196"
Expand Down
27 changes: 20 additions & 7 deletions src/telegram/i18n/i18n.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::language::Language;
use std::collections::HashMap;
use std::io::BufRead;
use crate::telegram::i18n::I18nError;

#[derive(Debug, Clone, Default)]
pub struct I18n {
Expand All @@ -12,22 +14,33 @@ impl I18n {
I18n::default()
}

pub fn load_translations(&mut self) -> () {
let locales = std::fs::read_dir("locales").unwrap();
pub fn load_translations(&mut self) -> Result<(), I18nError> {
let locales = std::fs::read_dir("locales")?;

for locale in locales {
let safe = locale.unwrap();
let safe = locale?;

let path = safe.path();
let file = path.to_str().unwrap();
let file = match path.to_str() {
Some(c) => c,
None => continue
};

let mut language = Language::new();
language.parse_translation(file);

let name = file.split("/").last().unwrap().split(".").next().unwrap();
let name = file
.split("/")
.last()
.unwrap()
.split(".")
.next()
.unwrap();

self.languages.insert(name.to_owned(), language);
}

Ok(())
}

pub fn add_user(&mut self, user_id: i64, language: &str) {
Expand All @@ -52,7 +65,7 @@ mod test {
fn test_i18n_init() {
// Create instance and load locales
let mut i18n = I18n::new();
i18n.load_translations();
i18n.load_translations().unwrap();

// Get a translation
let translation = i18n.get_translation("en", "lorem");
Expand All @@ -65,7 +78,7 @@ mod test {
fn test_user() {
// Create instance and load locales
let mut i18n = I18n::new();
i18n.load_translations();
i18n.load_translations().unwrap();

//
i18n.add_user(123123123123, "en");
Expand Down
20 changes: 20 additions & 0 deletions src/telegram/i18n/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
pub mod i18n;
pub mod language;

use thiserror::Error;

#[derive(Error, Debug)]
pub enum I18nError {
#[error("can't parse direntry")]
IoError(#[from] std::io::Error),

#[error("the data for key `{0}` is not available")]
Redaction(String),

#[error("invalid header (expected {expected:?}, found {found:?})")]
InvalidHeader {
expected: String,
found: String,
},

#[error("unknown data store error")]
Unknown,
}

0 comments on commit ac8d645

Please sign in to comment.