Browse Source

Enc -> Dec rename

develop-refactor
chodak166 5 months ago
parent
commit
c281ea2b2f
  1. 4
      app/src/app.rs
  2. 10
      app/src/config.rs
  3. 2
      lib/src/application/config.rs
  4. 6
      lib/src/core/sys_major.rs
  5. 16
      lib/src/core/sys_major/decoder.rs
  6. 56
      lib/src/core/sys_major/decoder_tests.rs
  7. 2
      lib/src/core/sys_major/rules_en.rs
  8. 78
      lib/src/core/sys_major/rules_pl.rs
  9. 8
      lib/src/core/system.rs
  10. 4
      lib/src/core/traits.rs
  11. 2
      lib/src/lib.rs
  12. 6
      lib/src/presentation/cli/cli_args.rs
  13. 2
      lib/src/presentation/cli/commands.rs
  14. 10
      lib/src/presentation/cli/commands/decode.rs
  15. 10
      lib/src/presentation/cli/commands/encode.rs
  16. 8
      lib/src/presentation/cli/defaults.rs

4
app/src/app.rs

@ -40,8 +40,8 @@ impl Application {
Command::Server(_) => {
commands::server::run(self.config.server, Self::wait_for_shutdown_signal()).await;
}
Command::Encode(_) => {
commands::encode::run(self.config.encoder).await;
Command::Decode(_) => {
commands::decode::run(self.config.decoder).await;
}
Command::ImportDict(_) => {
commands::import_dict::run(

10
app/src/config.rs

@ -3,12 +3,12 @@ use config::{Config, Environment, File};
use serde::Deserialize;
use applib::cli::{Command, GlobalArgs, defaults::set_defaults};
use applib::config::{EncoderConfig, ImportDictConfig, ServerConfig};
use applib::config::{DecoderConfig, ImportDictConfig, ServerConfig};
#[derive(Debug, Deserialize, Clone)]
pub struct AppConfig {
pub server: ServerConfig,
pub encoder: EncoderConfig,
pub decoder: DecoderConfig,
pub import_dict: ImportDictConfig,
pub log_level: String,
}
@ -40,11 +40,11 @@ impl AppConfig {
builder = builder.set_override("server.port", port)?;
}
}
Command::Encode(cmd_args) => {
Command::Decode(cmd_args) => {
if let Some(name) = &cmd_args.system {
builder = builder.set_override("encoder.system", name.as_str())?;
builder = builder.set_override("decoder.system", name.as_str())?;
}
builder = builder.set_override("encoder.input", cmd_args.input.clone())?;
builder = builder.set_override("decoder.input", cmd_args.input.clone())?;
}
Command::ImportDict(cmd_args) => {
builder = builder.set_override("import_dict.name", cmd_args.name.clone())?;

2
lib/src/application/config.rs

@ -7,7 +7,7 @@ pub struct ServerConfig {
}
#[derive(Debug, Deserialize, Clone)]
pub struct EncoderConfig {
pub struct DecoderConfig {
pub system: System,
pub input: String,
}

6
lib/src/core/sys_major.rs

@ -1,8 +1,8 @@
mod encoder;
mod decoder;
pub mod rules_en;
pub mod rules_pl;
#[cfg(test)]
mod encoder_tests;
mod decoder_tests;
pub use encoder::*;
pub use decoder::*;

16
lib/src/core/sys_major/encoder.rs → lib/src/core/sys_major/decoder.rs

@ -1,4 +1,4 @@
use crate::core::traits::SystemEncoder;
use crate::core::traits::SystemDecoder;
#[derive(Debug, Default, Clone)]
pub struct Rule {
@ -35,17 +35,17 @@ pub type Rules = Vec<Rule>;
// entries: Rules,
// }
/// (index, encoded value)
/// (index, decoded value)
type RuleMatches = Vec<(usize, String)>;
pub struct Encoder {
pub struct Decoder {
rules: Rules,
}
impl Encoder {
impl Decoder {
pub fn new(rules: Rules) -> Self {
Encoder {
rules: Encoder::to_lower_rules(rules),
Decoder {
rules: Decoder::to_lower_rules(rules),
}
}
@ -107,8 +107,8 @@ impl Encoder {
}
}
impl SystemEncoder for Encoder {
fn encode(&self, word: &str) -> String {
impl SystemDecoder for Decoder {
fn decode(&self, word: &str) -> String {
let mut matches: RuleMatches = self
.rules
.iter()

56
lib/src/core/sys_major/encoder_tests.rs → lib/src/core/sys_major/decoder_tests.rs

@ -1,5 +1,5 @@
use super::encoder::{Encoder, Rule, Rules};
use crate::core::traits::SystemEncoder;
use super::decoder::{Decoder, Rule, Rules};
use crate::core::traits::SystemDecoder;
#[cfg(test)]
mod tests {
@ -44,91 +44,91 @@ mod tests {
#[test]
fn test_single_symbol_encoding_only_before_only_after_matched() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("ABC");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("ABC");
assert_eq!(output, "2")
}
#[test]
fn test_double_symbol_encoding_only_before_only_after_matched() {
let encoder = Encoder::new(create_double_rules());
let output = encoder.encode("ABCDEF");
let decoder = Decoder::new(create_double_rules());
let output = decoder.decode("ABCDEF");
assert_eq!(output, "2")
}
#[test]
fn test_single_symbol_encoding_only_before_not_matched_with_other() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("DBC");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("DBC");
assert_eq!(output, "")
}
#[test]
fn test_double_symbol_encoding_only_before_not_matched_with_other() {
let encoder = Encoder::new(create_double_rules());
let output = encoder.encode("AACDEE");
let decoder = Decoder::new(create_double_rules());
let output = decoder.decode("AACDEE");
assert_eq!(output, "")
}
#[test]
fn test_case_insensitivity() {
let encoder = Encoder::new(create_double_rules());
let output = encoder.encode("abcdef");
let decoder = Decoder::new(create_double_rules());
let output = decoder.decode("abcdef");
assert_eq!(output, "2")
}
#[test]
fn test_single_symbol_encoding_only_before_not_matched_with_empty() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("BC");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("BC");
assert_eq!(output, "")
}
#[test]
fn test_single_symbol_encoding_only_before_not_matched_with_not_before() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("XBC");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("XBC");
assert_eq!(output, "")
}
#[test]
fn test_single_symbol_encoding_only_after_not_matched_with_other() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("ABD");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("ABD");
assert_eq!(output, "")
}
#[test]
fn test_single_symbol_encoding_only_after_not_matched_with_empty() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("AB");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("AB");
assert_eq!(output, "")
}
#[test]
fn test_single_symbol_encoding_only_after_not_matched_with_not_after() {
let encoder = Encoder::new(create_single_rules());
let output = encoder.encode("ABY");
let decoder = Decoder::new(create_single_rules());
let output = decoder.decode("ABY");
assert_eq!(output, "")
}
#[test]
fn test_single_symbol_encoding_empty_before_after_matched_with_empty() {
let encoder = Encoder::new(create_single_rules_min());
let output = encoder.encode("B");
let decoder = Decoder::new(create_single_rules_min());
let output = decoder.decode("B");
assert_eq!(output, "2")
}
#[test]
fn test_single_symbol_encoding_empty_before_after_matched_with_others() {
let encoder = Encoder::new(create_single_rules_min());
let output = encoder.encode("AXBYC");
let decoder = Decoder::new(create_single_rules_min());
let output = decoder.decode("AXBYC");
assert_eq!(output, "2")
}
#[test]
fn test_encoding_multiple_phonemes() {
let encoder = Encoder::new(create_double_rules());
let output = encoder.encode("VvmNabCd33mn00CD22cdefmn");
let decoder = Decoder::new(create_double_rules());
let output = decoder.decode("VvmNabCd33mn00CD22cdefmn");
assert_eq!(output, "32323")
}
}

2
lib/src/core/sys_major/rules_en.rs

@ -1,4 +1,4 @@
use super::encoder::{Rule, Rules};
use super::decoder::{Rule, Rules};
pub fn get_rules() -> Rules {
vec![

78
lib/src/core/sys_major/rules_pl.rs

@ -1,4 +1,4 @@
use super::encoder::{Rule, Rules};
use super::decoder::{Rule, Rules};
pub fn get_rules() -> Rules {
vec![
@ -133,90 +133,90 @@ pub fn get_rules() -> Rules {
#[cfg(test)]
mod tests {
use super::*;
use crate::SystemEncoder;
use crate::core::sys_major::Encoder;
use crate::SystemDecoder;
use crate::core::sys_major::Decoder;
#[test]
fn test_major_dict_pl_encode_0_1() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("SZSCZ");
fn test_major_dict_pl_decode_0_1() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("SZSCZ");
assert_eq!(output, "0")
}
#[test]
fn test_major_dict_pl_encode_0_2() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("SZSICZ");
fn test_major_dict_pl_decode_0_2() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("SZSICZ");
assert_eq!(output, "")
}
#[test]
fn test_major_dict_pl_encode_0_3() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("SZCZRZZCZDZSZ");
fn test_major_dict_pl_decode_0_3() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("SZCZRZZCZDZSZ");
assert_eq!(output, "0")
}
#[test]
fn test_major_dict_pl_encode_0_4() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("SZCZRZZICZDZSZ");
fn test_major_dict_pl_decode_0_4() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("SZCZRZZICZDZSZ");
assert_eq!(output, "")
}
#[test]
fn test_major_dict_pl_encode_1_1() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("SZTCZ");
fn test_major_dict_pl_decode_1_1() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("SZTCZ");
assert_eq!(output, "1")
}
#[test]
fn test_major_dict_pl_encode_1_2() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("DZDŻDŹDDZDŻDŹ");
fn test_major_dict_pl_decode_1_2() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("DZDŻDŹDDZDŻDŹ");
assert_eq!(output, "1")
}
#[test]
fn test_major_dict_pl_encode_1_3() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("DZDŻDŹDZDZDŻDŹ");
fn test_major_dict_pl_decode_1_3() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("DZDŻDŹDZDZDŻDŹ");
assert_eq!(output, "")
}
#[test]
fn test_major_dict_pl_encode_2_1() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("NINNI");
fn test_major_dict_pl_decode_2_1() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("NINNI");
assert_eq!(output, "2")
}
#[test]
fn test_major_dict_pl_encode_2_2() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("NININI");
fn test_major_dict_pl_decode_2_2() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("NININI");
assert_eq!(output, "")
}
#[test]
fn test_major_dict_pl_encode_4_1() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("RZRRZ");
fn test_major_dict_pl_decode_4_1() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("RZRRZ");
assert_eq!(output, "4")
}
#[test]
fn test_major_dict_pl_encode_4_2() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("RZRZRZ");
fn test_major_dict_pl_decode_4_2() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("RZRZRZ");
assert_eq!(output, "")
}
#[test]
fn test_major_dict_pl_encode_full_1() {
let encoder = Encoder::new(get_rules());
let output = encoder.encode("ATADANAMARALAJAKAGAFAWAPABA");
fn test_major_dict_pl_decode_full_1() {
let decoder = Decoder::new(get_rules());
let output = decoder.decode("ATADANAMARALAJAKAGAFAWAPABA");
assert_eq!(output, "1123456778899")
}
}

8
lib/src/core/system.rs

@ -1,6 +1,6 @@
use serde::Deserialize;
use crate::core::SystemEncoder;
use crate::core::SystemDecoder;
use crate::core::sys_major as major;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
@ -22,9 +22,9 @@ impl From<&str> for System {
}
}
pub fn create_encoder(system: &System) -> Box<dyn SystemEncoder> {
pub fn create_decoder(system: &System) -> Box<dyn SystemDecoder> {
match system {
System::MajorPl => Box::new(major::Encoder::new(major::rules_pl::get_rules())),
System::MajorEn => Box::new(major::Encoder::new(major::rules_en::get_rules())),
System::MajorPl => Box::new(major::Decoder::new(major::rules_pl::get_rules())),
System::MajorEn => Box::new(major::Decoder::new(major::rules_en::get_rules())),
}
}

4
lib/src/core/traits.rs

@ -1,8 +1,8 @@
use super::entities::{Dict, DictEntry};
use super::errors::RepositoryError;
pub trait SystemEncoder {
fn encode(&self, word: &str) -> String;
pub trait SystemDecoder {
fn decode(&self, word: &str) -> String;
}
// pub trait SystenDecoder {

2
lib/src/lib.rs

@ -5,5 +5,5 @@ mod presentation;
pub use self::application::config;
pub use self::core::system;
pub use self::core::traits::SystemEncoder;
pub use self::core::traits::SystemDecoder;
pub use self::presentation::cli;

6
lib/src/presentation/cli/cli_args.rs

@ -27,8 +27,8 @@ pub enum Command {
/// Start the application server
Server(ServerArgs),
/// Encode a word using given system
Encode(EncodeArgs),
/// Decode a word using given system
Decode(DecodeArgs),
/// Import dictionary
ImportDict(ImportDictArgs),
@ -41,7 +41,7 @@ pub struct ServerArgs {
}
#[derive(ClapArgs, Debug, Clone)]
pub struct EncodeArgs {
pub struct DecodeArgs {
#[arg(long, help = defaults::HELP_ENC_SYSTEM)]
pub system: Option<String>,

2
lib/src/presentation/cli/commands.rs

@ -1,3 +1,3 @@
pub mod encode;
pub mod decode;
pub mod import_dict;
pub mod server;

10
lib/src/presentation/cli/commands/decode.rs

@ -0,0 +1,10 @@
use crate::application::config::DecoderConfig;
use crate::core::system;
use tracing::debug;
pub async fn run(config: DecoderConfig) {
debug!("Running greeter with config {:?}", config);
let decoder = system::create_decoder(&config.system);
let result = decoder.decode(&config.input);
println!("{}", result);
}

10
lib/src/presentation/cli/commands/encode.rs

@ -1,10 +0,0 @@
use crate::application::config::EncoderConfig;
use crate::core::system;
use tracing::debug;
pub async fn run(config: EncoderConfig) {
debug!("Running greeter with config {:?}", config);
let encoder = system::create_encoder(&config.system);
let result = encoder.encode(&config.input);
println!("{}", result);
}

8
lib/src/presentation/cli/defaults.rs

@ -11,7 +11,7 @@ pub const SYSTEM_NAME: &str = "major_pl";
pub const HELP_PORT: &str = formatcp!("Override Port [default: {}]", PORT);
pub const HELP_LOG: &str = formatcp!("Override Log Level [default: {}]", LOG_LEVEL);
pub const HELP_ENC_SYSTEM: &str = formatcp!("System to use [default: {}]", SYSTEM_NAME);
pub const HELP_ENC_INPUT: &str = formatcp!("Text to encode");
pub const HELP_ENC_INPUT: &str = formatcp!("Text to decode");
pub const HELP_IMPORT_DICT_NAME: &str = formatcp!("Dictionary name");
pub const HELP_IMPORT_DICT_INPUT: &str = formatcp!("Dictionary file path");
@ -23,9 +23,9 @@ pub fn set_defaults(
// Server
.set_default("server.host", HOST)?
.set_default("server.port", PORT)?
// Encoder
.set_default("encoder.system", SYSTEM_NAME)?
.set_default("encoder.input", "")
// Decoder
.set_default("decoder.system", SYSTEM_NAME)?
.set_default("decoder.input", "")
// Wrapping in Result
.map_err(|e| e.into())
}

Loading…
Cancel
Save