From 8b9bb6526f9c26f855545bba1c708c0e6b4edeb8 Mon Sep 17 00:00:00 2001 From: 1uciuszzz Date: Fri, 13 Oct 2023 16:15:20 +0800 Subject: [PATCH] v1 --- .gitignore | 1 + Cargo.lock | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 ++++ src/lib.rs | 69 ++++++++++++++++++++++++ src/main.rs | 54 +++++++++++++++++++ 5 files changed, 284 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/lib.rs create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..67d23d3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,151 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bitflags" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" + +[[package]] +name = "colored" +version = "2.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2674ec482fbc38012cf31e6c42ba0177b431a0cb6f15fe40efa5aab1bda516f6" +dependencies = [ + "is-terminal", + "lazy_static", + "windows-sys", +] + +[[package]] +name = "errno" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3e13f66a2f95e32a39eaa81f6b95d42878ca0e1db0c7543723dfe12557e860" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "hermit-abi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7" + +[[package]] +name = "is-terminal" +version = "0.4.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" +dependencies = [ + "hermit-abi", + "rustix", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a08173bc88b7955d1b3145aa561539096c421ac8debde8cbc3612ec635fee29b" + +[[package]] +name = "linux-raw-sys" +version = "0.4.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da2479e8c062e40bf0066ffa0bc823de0a9368974af99c9f6df941d2c231e03f" + +[[package]] +name = "punc2en" +version = "0.1.0" +dependencies = [ + "colored", +] + +[[package]] +name = "rustix" +version = "0.38.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..1a233dd --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "punc2en" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +colored = "2.0.4" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..56c7bb3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,69 @@ +use colored::Colorize; + +pub struct Config { + pub path: String, + pub is_dir: bool, +} + +impl Config { + pub fn new(args: &[String]) -> Result { + if args.len() < 2 { + return Err("not enough arguments"); + } + let path = args[1].clone(); + let is_dir = args.len() == 3 && args[2] == "-d"; + Ok(Config { path, is_dir }) + } +} + +pub fn run(path: String) { + let content: String = match std::fs::read_to_string(&path) { + Err(e) => { + println!( + "{} {}", + "Something went wrong reading the file: ".red().bold(), + e.to_string().red().bold() + ); + std::process::exit(1); + } + Ok(content) => content, + }; + let mut res: Vec = vec![]; + content.lines().for_each(|line| { + let mut muted = line.replace(",", ","); + muted = muted.replace("。", "."); + muted = muted.replace("、", "\\"); + muted = muted.replace(";", ";"); + muted = muted.replace("‘", "\'"); + muted = muted.replace("’", "\'"); + muted = muted.replace("【", "["); + muted = muted.replace("】", "]"); + muted = muted.replace("·", "`"); + muted = muted.replace("《", "<"); + muted = muted.replace("》", ">"); + muted = muted.replace("?", "?"); + muted = muted.replace(":", ":"); + muted = muted.replace("“", "\""); + muted = muted.replace("”", "\""); + muted = muted.replace("!", "!"); + muted = muted.replace("¥", "$"); + muted = muted.replace("……", "..."); + muted = muted.replace("(", "("); + muted = muted.replace(")", ")"); + muted = muted.replace("——", "_"); + res.push(muted); + }); + match std::fs::write(&path, res.join("\n")) { + Err(e) => { + println!( + "{} {}", + "Something went wrong writing the file: ".red().bold(), + e.to_string().red().bold() + ); + std::process::exit(1); + } + Ok(_) => { + println!("{}", "Done!".green().bold()); + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..64358b6 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,54 @@ +use colored::Colorize; + +fn main() { + let args: Vec = std::env::args().collect(); + let config = match punc2en::Config::new(&args) { + Err(e) => { + println!("Problem parsing arguments: {}", e); + std::process::exit(1); + } + Ok(config) => config, + }; + if !config.is_dir { + return punc2en::run(config.path); + } + let read_dir = match std::fs::read_dir(config.path) { + Ok(files) => files, + Err(e) => { + println!( + "{} {}", + "Problem reading directory: ".red().bold(), + e.to_string().red().bold() + ); + std::process::exit(1); + } + }; + read_dir.for_each(|f| { + let file = match f { + Err(e) => { + println!( + "{} {}", + "Problem reading file: ".red().bold(), + e.to_string().red().bold() + ); + std::process::exit(1); + } + Ok(f) => f, + }; + let is_file = match file.file_type() { + Ok(t) => t.is_file(), + Err(e) => { + println!( + "{} {}", + "Problem reading file type: ".red().bold(), + e.to_string().red().bold() + ); + std::process::exit(1); + } + }; + if is_file { + let file_path = file.path().to_str().unwrap().to_string(); + punc2en::run(file_path); + } + }) +}