From 301eb8e3a0be081cfd217cc7033aaccdfd6fb688 Mon Sep 17 00:00:00 2001 From: chodak166 Date: Sun, 23 Nov 2025 18:54:30 +0100 Subject: [PATCH] WIP: app structure --- lib/src/core/entities.rs | 45 +++++++++++++++++++ lib/src/core/errors.rs | 15 +++++++ lib/src/core/major.rs | 2 +- lib/src/core/major/dict_en.rs | 4 +- lib/src/core/major/dict_pl.rs | 4 +- lib/src/core/major/encoder.rs | 38 ++-------------- .../core/major/{tests.rs => encoder_tests.rs} | 36 ++++++++++----- lib/src/core/major/indexer.rs | 0 lib/src/core/mod.rs | 4 ++ lib/src/core/traits.rs | 15 +++++++ 10 files changed, 113 insertions(+), 50 deletions(-) create mode 100644 lib/src/core/entities.rs create mode 100644 lib/src/core/errors.rs rename lib/src/core/major/{tests.rs => encoder_tests.rs} (76%) create mode 100644 lib/src/core/major/indexer.rs diff --git a/lib/src/core/entities.rs b/lib/src/core/entities.rs new file mode 100644 index 0000000..deaa7b1 --- /dev/null +++ b/lib/src/core/entities.rs @@ -0,0 +1,45 @@ +use std::collections::HashMap; + +#[derive(Debug, Default, Clone)] +pub struct DictEntry { + pub phoneme_in: String, + pub phoneme_out: String, + + pub not_before: Vec, + pub not_after: Vec, + + pub only_before: Vec, + pub only_after: Vec, +} + +impl DictEntry { + pub fn into_lowercase(self) -> Self { + DictEntry { + phoneme_in: self.phoneme_in.to_lowercase(), + phoneme_out: self.phoneme_out.to_lowercase(), + not_before: Self::lower_vec(self.not_before), + not_after: Self::lower_vec(self.not_after), + only_before: Self::lower_vec(self.only_before), + only_after: Self::lower_vec(self.only_after), + } + } + + fn lower_vec(vec: Vec) -> Vec { + vec.into_iter().map(|s| s.to_lowercase()).collect() + } +} + +pub type DictEntries = Vec; +pub struct Dict { + name: String, + entries: DictEntries, +} + +pub type WordEntryId = u32; + +#[derive(Debug, Default)] +pub struct WordEntry { + id: Option, + word: String, + metadata: HashMap, +} diff --git a/lib/src/core/errors.rs b/lib/src/core/errors.rs new file mode 100644 index 0000000..703cc7f --- /dev/null +++ b/lib/src/core/errors.rs @@ -0,0 +1,15 @@ +use std::fmt; + +#[derive(Debug)] +pub enum RepositoryError { + NotFound, + ConnectionFailed, + InvalidData(String), + Unexpected(String), +} + +impl fmt::Display for RepositoryError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self) + } +} diff --git a/lib/src/core/major.rs b/lib/src/core/major.rs index 621ac9d..206925e 100644 --- a/lib/src/core/major.rs +++ b/lib/src/core/major.rs @@ -3,6 +3,6 @@ pub mod dict_pl; mod encoder; #[cfg(test)] -mod tests; +mod encoder_tests; pub use encoder::*; diff --git a/lib/src/core/major/dict_en.rs b/lib/src/core/major/dict_en.rs index 3068233..37292df 100644 --- a/lib/src/core/major/dict_en.rs +++ b/lib/src/core/major/dict_en.rs @@ -1,6 +1,6 @@ -use crate::core::major::{Dict, DictEntry}; +use crate::core::entities::{DictEntries, DictEntry}; -pub fn get_dict() -> Dict { +pub fn get_dict() -> DictEntries { vec![ DictEntry { phoneme_in: "EN".to_string(), diff --git a/lib/src/core/major/dict_pl.rs b/lib/src/core/major/dict_pl.rs index 2cdfab3..36562e2 100644 --- a/lib/src/core/major/dict_pl.rs +++ b/lib/src/core/major/dict_pl.rs @@ -1,6 +1,6 @@ -use crate::core::major::{Dict, DictEntry}; +use crate::core::entities::{DictEntries, DictEntry}; -pub fn get_dict() -> Dict { +pub fn get_dict() -> DictEntries { vec![ DictEntry { phoneme_in: "PL".to_string(), diff --git a/lib/src/core/major/encoder.rs b/lib/src/core/major/encoder.rs index 3134a65..c226c86 100644 --- a/lib/src/core/major/encoder.rs +++ b/lib/src/core/major/encoder.rs @@ -1,51 +1,21 @@ +use crate::core::entities::{DictEntries, DictEntry}; use crate::core::traits::SystemEncoder; -#[derive(Debug, Default, Clone)] -pub struct DictEntry { - pub phoneme_in: String, - pub phoneme_out: String, - - pub not_before: Vec, - pub not_after: Vec, - - pub only_before: Vec, - pub only_after: Vec, -} - -impl DictEntry { - pub fn into_lowercase(self) -> Self { - DictEntry { - phoneme_in: self.phoneme_in.to_lowercase(), - phoneme_out: self.phoneme_out.to_lowercase(), - not_before: Self::lower_vec(self.not_before), - not_after: Self::lower_vec(self.not_after), - only_before: Self::lower_vec(self.only_before), - only_after: Self::lower_vec(self.only_after), - } - } - - fn lower_vec(vec: Vec) -> Vec { - vec.into_iter().map(|s| s.to_lowercase()).collect() - } -} - -pub type Dict = Vec; - /// (index, encoded value) type DictMatches = Vec<(usize, String)>; pub struct Encoder { - dict: Dict, + dict: DictEntries, } impl Encoder { - pub fn new(dict: Dict) -> Self { + pub fn new(dict: DictEntries) -> Self { Encoder { dict: Encoder::to_lower_dict(dict), } } - fn to_lower_dict(dict: Dict) -> Dict { + fn to_lower_dict(dict: DictEntries) -> DictEntries { dict.into_iter() .map(|entry| entry.into_lowercase()) .collect() diff --git a/lib/src/core/major/tests.rs b/lib/src/core/major/encoder_tests.rs similarity index 76% rename from lib/src/core/major/tests.rs rename to lib/src/core/major/encoder_tests.rs index 8a36075..635220c 100644 --- a/lib/src/core/major/tests.rs +++ b/lib/src/core/major/encoder_tests.rs @@ -1,3 +1,4 @@ +use crate::core::entities::{DictEntries, DictEntry}; use crate::core::major::*; use crate::core::traits::SystemEncoder; @@ -5,7 +6,7 @@ use crate::core::traits::SystemEncoder; mod tests { use super::*; - fn create_single_dict() -> Dict { + fn create_single_dict() -> DictEntries { vec![DictEntry { phoneme_in: "B".to_string(), phoneme_out: "2".to_string(), @@ -16,7 +17,7 @@ mod tests { }] } - fn create_single_dict_min() -> Dict { + fn create_single_dict_min() -> DictEntries { vec![DictEntry { phoneme_in: "B".to_string(), phoneme_out: "2".to_string(), @@ -24,15 +25,22 @@ mod tests { }] } - fn create_double_dict() -> Dict { - vec![DictEntry { - phoneme_in: "CD".to_string(), - phoneme_out: "2".to_string(), - not_after: vec!["YZ".to_string()], - not_before: vec!["WX".to_string()], - only_after: vec!["AB".to_string()], - only_before: vec!["EF".to_string()], - }] + fn create_double_dict() -> DictEntries { + vec![ + DictEntry { + phoneme_in: "CD".to_string(), + phoneme_out: "2".to_string(), + not_after: vec!["00".to_string(), "YZ".to_string()], + not_before: vec!["11".to_string(), "WX".to_string()], + only_after: vec!["22".to_string(), "AB".to_string()], + only_before: vec!["33".to_string(), "EF".to_string()], + }, + DictEntry { + phoneme_in: "MN".to_string(), + phoneme_out: "3".to_string(), + ..Default::default() + }, + ] } #[test] @@ -118,4 +126,10 @@ mod tests { let output = encoder.encode("AXBYC"); assert_eq!(output, "2") } + #[test] + fn test_encoding_multiple_phonemes() { + let encoder = Encoder::new(create_double_dict()); + let output = encoder.encode("VvmNabCd33mn00CD22cdefmn"); + assert_eq!(output, "32323") + } } diff --git a/lib/src/core/major/indexer.rs b/lib/src/core/major/indexer.rs new file mode 100644 index 0000000..e69de29 diff --git a/lib/src/core/mod.rs b/lib/src/core/mod.rs index 5c75643..fc7a881 100644 --- a/lib/src/core/mod.rs +++ b/lib/src/core/mod.rs @@ -1,7 +1,11 @@ +pub mod entities; +pub mod errors; pub mod major; pub mod system; pub mod traits; // pub use self::major::*; +pub use self::entities::*; +pub use self::errors::*; pub use self::system::*; pub use self::traits::*; diff --git a/lib/src/core/traits.rs b/lib/src/core/traits.rs index e183d91..6195afd 100644 --- a/lib/src/core/traits.rs +++ b/lib/src/core/traits.rs @@ -1,3 +1,18 @@ +use crate::core::entities::{Dict, WordEntry, WordEntryId}; +use crate::core::errors::RepositoryError; + pub trait SystemEncoder { fn encode(&self, word: &str) -> String; } + +pub trait WordRepository { + fn save(word: &WordEntry) -> Result; + fn save_many(words: &Vec) -> Result<(), RepositoryError>; + fn fetch(id: WordEntryId) -> Result; + fn fetch_many(ids: &Vec) -> Result, RepositoryError>; +} + +pub trait DictRepository { + fn save(dict: &Dict) -> Result<(), RepositoryError>; + fn fetch(name: &str) -> Result; +}