Skip to content

Commit

Permalink
basic reverter (unicode to ascii) impl
Browse files Browse the repository at this point in the history
  • Loading branch information
caiogeraldes committed Jan 18, 2024
1 parent 18c0c08 commit 3f7d274
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 51 deletions.
4 changes: 2 additions & 2 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "betaconvert"
version = "0.1.4"
version = "0.2.0"
edition = "2021"
authors = ["Caio Geraldes <caio.geraldes@usp.br>"]
description = "A rust CLI for Betacode conversion"
Expand All @@ -11,7 +11,7 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
betacode = { path = "../betacode", version = ">=1.1.0" }
betacode = { path = "../betacode", version = ">=1.2.0" }
clap = { version = "3.2.17", features = ["derive"] }
log = "0.4.19"
pretty_env_logger = "0.5.0"
117 changes: 68 additions & 49 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pub struct Args {
/// Output file for conversion (default is stdout)
#[clap(short, long, action)]
pub output: Option<String>,

#[clap(short, long, action)]
pub inverse: bool,
}

fn convert_line(input: String) -> Result<String, validator::ValidationError> {
Expand All @@ -34,6 +37,9 @@ fn convert_line(input: String) -> Result<String, validator::ValidationError> {
},
}
}
fn revert_line(input: String) -> String {
converter::revert(input)
}

fn convert_line_strict(input: String) -> Result<String, validator::ValidationError> {
match validator::validate(&input) {
Expand Down Expand Up @@ -68,72 +74,85 @@ fn main() -> Result<(), validator::ValidationError> {
false => args.text,
};

match args.strict {
match args.inverse {
true => match input_str {
None => {
eprintln! {"Empty string"};
std::process::exit(1)
}
Some(input_str) => match convert_line_strict(input_str) {
Ok(string) => {
println!("{string}");
Ok(())
Some(input_str) => {
println!("{}", revert_line(input_str));
Ok(())
}
},

false => match args.strict {
true => match input_str {
None => {
eprintln! {"Empty string"};
std::process::exit(1)
}
Err(e) => match e {
validator::ValidationError::NotASCII(_) => {
eprintln!("Text passed is not in ASCII.\n{e}");
std::process::exit(1)
Some(input_str) => match convert_line_strict(input_str) {
Ok(string) => {
println!("{string}");
Ok(())
}
validator::ValidationError::InvalidChars(_) => {
eprintln!(
Err(e) => match e {
validator::ValidationError::NotASCII(_) => {
eprintln!("Text passed is not in ASCII.\n{e}");
std::process::exit(1)
}
validator::ValidationError::InvalidChars(_) => {
eprintln!(
"Text passed violates ASCII Betacode standards as applied here.\n{e}"
);
std::process::exit(1)
}
validator::ValidationError::InvalidDiacriticOrder(_) => {
eprintln!(
std::process::exit(1)
}
validator::ValidationError::InvalidDiacriticOrder(_) => {
eprintln!(
"Text passed violates ASCII Betacode standards as applied here.\n{e}"
);
std::process::exit(1)
}
validator::ValidationError::MixedCaseNotation => {
eprintln!("Text passed contains both * and upper case ASCII for Greek Upper notation.\n{e}");
std::process::exit(1)
}
},
},
},
false => match input_str {
None => {
eprintln! {"Empty string"};
std::process::exit(1)
}
Some(input_str) => match convert_line(input_str) {
Ok(string) => match args.output {
None => {
println!("{string}");
Ok(())
}
Some(path) => match fs::write(PathBuf::from(path), string) {
Ok(_) => Ok(()),
Err(e) => {
eprintln!("{e}");
std::process::exit(1)
}
validator::ValidationError::MixedCaseNotation => {
eprintln!("Text passed contains both * and upper case ASCII for Greek Upper notation.\n{e}");
std::process::exit(1)
}
},
},
Err(e) => match e {
validator::ValidationError::NotASCII(_) => {
eprintln!("Text passed is not in ASCII.\n{e}");
std::process::exit(1)
}
validator::ValidationError::InvalidChars(_) => {
eprintln!(
},
false => match input_str {
None => {
eprintln! {"Empty string"};
std::process::exit(1)
}
Some(input_str) => match convert_line(input_str) {
Ok(string) => match args.output {
None => {
println!("{string}");
Ok(())
}
Some(path) => match fs::write(PathBuf::from(path), string) {
Ok(_) => Ok(()),
Err(e) => {
eprintln!("{e}");
std::process::exit(1)
}
},
},
Err(e) => match e {
validator::ValidationError::NotASCII(_) => {
eprintln!("Text passed is not in ASCII.\n{e}");
std::process::exit(1)
}
validator::ValidationError::InvalidChars(_) => {
eprintln!(
"Text passed violates ASCII Betacode standards as applied here.\n{e}"
);
std::process::exit(1)
}
_ => Ok(()),
std::process::exit(1)
}
_ => Ok(()),
},
},
},
},
Expand Down

0 comments on commit 3f7d274

Please sign in to comment.