diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..f47c63a --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# 强制所有文件使用 LF 换行符 +* text=auto eol=lf diff --git a/.gitignore b/.gitignore index ea8c4bf..70bfe5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/fixtures diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index dee6400..f4d2fd9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -55,7 +55,7 @@ repos: - id: cargo-test name: cargo test description: unit test for the project - entry: bash -c 'cargo nextest run --all-features' + entry: bash -c 'cargo nextest run --all-features --no-tests="warn"' language: rust files: \.rs$ pass_filenames: false diff --git a/Cargo.lock b/Cargo.lock index be35886..ead00e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,7 +1,375 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] -name = "rust-template" +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "anyhow" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a23eb6b1614318a8071c9b2521f36b424b2c83db5eb3a0fead4a6c0809af6e61" + +[[package]] +name = "clap" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9e340e012a1bf4935f5282ed1436d1489548e8f72308207ea5df0e23d2d03f8" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.53" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d76b5d13eaa18c901fd2f7fca939fefe3a0727a953561fefdf3b2922b8569d00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d728cc89cf3aee9ff92b05e62b19ee65a02b5702cff7d5a377e32c6ae29d8d" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "csv" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52cd9d68cf7efc6ddfaaee42e7288d3a99d613d4b50f76ce9827ae0c6e14f938" +dependencies = [ + "csv-core", + "itoa", + "ryu", + "serde_core", +] + +[[package]] +name = "csv-core" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704a3c26996a80471189265814dbc2c257598b96b8a7feae2d31ace646bb9782" +dependencies = [ + "memchr", +] + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ad4bb2b565bca0645f4d68c5c9af97fba094e9791da685bf83cb5f3ce74acf2" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "proc-macro2" +version = "1.0.103" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ee95bc4ef87b8d5ba32e8b7714ccc834865276eab0aed5c9958d00ec45f49e8" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a338cc41d27e6cc6dce6cefc13a0729dfbb81c262b1f519331575dd80ef3067f" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rcli" version = "0.1.0" +dependencies = [ + "anyhow", + "clap", + "csv", + "serde", + "serde_json", + "serde_yaml", + "toml", +] + +[[package]] +name = "ryu" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "402a6f66d8c709116cf22f558eab210f5a50187f702eb4d7e5ef38d9a7f1c79c" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", + "serde_core", +] + +[[package]] +name = "serde_spanned" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e24345aa0fe688594e73770a5f6d1b216508b4f93484c0026d521acd30134392" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.111" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "390cc9a294ab71bdb1aa2e99d13be9c753cd2d7bd6560c77118597410c4d2e87" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "toml" +version = "0.9.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0dc8b1fb61449e27716ec0e1bdf0f6b8f3e8f6b05391e8497b8b6d7804ea6d8" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533" +dependencies = [ + "serde_core", +] + +[[package]] +name = "toml_parser" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e" +dependencies = [ + "winnow", +] + +[[package]] +name = "toml_writer" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2" + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "winnow" +version = "0.7.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5364e9d77fcdeeaa6062ced926ee3381faa2ee02d3eb83a5c27a8825540829" diff --git a/Cargo.toml b/Cargo.toml index 9c206ec..df4bc2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,17 @@ [package] -name = "rust-template" +name = "rcli" version = "0.1.0" +authors = ["Bowen Hu "] edition = "2024" +license = "MIT" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +anyhow = "1.0.100" +clap = { version = "4.5.53", features = ["derive"] } +csv = "1.4.0" +serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0.145" +serde_yaml = "0.9.34" +toml = "0.9.8" diff --git a/deny.toml b/deny.toml index ba7d5ad..4821214 100644 --- a/deny.toml +++ b/deny.toml @@ -17,7 +17,7 @@ # this list would mean the nix crate, as well as any of its exclusive # dependencies not shared by any other crates, would be ignored, as the target # list here is effectively saying which targets you are building for. -targets = [ +# targets = [ # The triple can be any string, but only the target triples built in to # rustc (as of 1.40) can be checked against actual config expressions #{ triple = "x86_64-unknown-linux-musl" }, @@ -25,26 +25,28 @@ targets = [ # particular target. target_features are currently not validated against # the actual valid features supported by the target architecture. #{ triple = "wasm32-unknown-unknown", features = ["atomics"] }, -] +# ] # This section is considered when running `cargo deny check advisories` # More documentation for the advisories section can be found here: # https://embarkstudios.github.io/cargo-deny/checks/advisories/cfg.html +# https://github.com/EmbarkStudios/cargo-deny/pull/611 [advisories] +version = 2 # The path where the advisory database is cloned/fetched into db-path = "~/.cargo/advisory-db" # The url(s) of the advisory databases to use db-urls = ["https://github.com/rustsec/advisory-db"] # The lint level for security vulnerabilities -vulnerability = "deny" +# vulnerability = "deny" # The lint level for unmaintained crates -unmaintained = "warn" +# unmaintained = "all" # The lint level for crates that have been yanked from their source registry yanked = "warn" # The lint level for crates with security notices. Note that as of # 2019-12-17 there are no security notice advisories in # https://github.com/rustsec/advisory-db -notice = "warn" +# notice = "deny" # A list of advisory IDs to ignore. Note that ignored advisories will still # output a note when they are encountered. ignore = [ @@ -63,9 +65,11 @@ ignore = [ # This section is considered when running `cargo deny check licenses` # More documentation for the licenses section can be found here: # https://embarkstudios.github.io/cargo-deny/checks/licenses/cfg.html +# https://github.com/EmbarkStudios/cargo-deny/pull/611 [licenses] +version = 2 # The lint level for crates which do not have a detectable license -unlicensed = "allow" +# unlicensed = "allow" # List of explicitly allowed licenses # See https://spdx.org/licenses/ for list of possible licenses # [possible values: any SPDX 3.7 short identifier (+ optional exception)]. @@ -73,6 +77,7 @@ allow = [ "MIT", "Apache-2.0", "Unicode-DFS-2016", + "Unicode-3.0", "MPL-2.0", "BSD-2-Clause", "BSD-3-Clause", @@ -82,23 +87,23 @@ allow = [ # List of explicitly disallowed licenses # See https://spdx.org/licenses/ for list of possible licenses # [possible values: any SPDX 3.7 short identifier (+ optional exception)]. -deny = [ +# deny = [ #"Nokia", -] +# ] # Lint level for licenses considered copyleft -copyleft = "warn" +# copyleft = "deny" # Blanket approval or denial for OSI-approved or FSF Free/Libre licenses # * both - The license will be approved if it is both OSI-approved *AND* FSF # * either - The license will be approved if it is either OSI-approved *OR* FSF # * osi-only - The license will be approved if is OSI-approved *AND NOT* FSF # * fsf-only - The license will be approved if is FSF *AND NOT* OSI-approved # * neither - This predicate is ignored and the default lint level is used -allow-osi-fsf-free = "neither" +# allow-osi-fsf-free = "neither" # Lint level used when no other predicates are matched # 1. License isn't in the allow or deny lists # 2. License isn't copyleft # 3. License isn't OSI/FSF, or allow-osi-fsf-free = "neither" -default = "deny" +# default = "deny" # The confidence threshold for detecting a license from license text. # The higher the value, the more closely the license text must be to the # canonical license text of a valid SPDX license file. diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..acb91b3 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,5 @@ +mod opts; +mod process; + +pub use opts::{Opts, SubCommand}; +pub use process::process_csv; diff --git a/src/main.rs b/src/main.rs index e7a11a9..fcd582e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,15 @@ -fn main() { - println!("Hello, world!"); +use clap::Parser; +use rcli::{Opts, SubCommand, process_csv}; + +// rcli csv -i input.csv -o output.json --header -d ',' + +fn main() -> anyhow::Result<()> { + let opts = Opts::parse(); + match opts.cmd { + SubCommand::Csv(opts) => { + process_csv(&opts)?; + } + } + + Ok(()) } diff --git a/src/opts.rs b/src/opts.rs new file mode 100644 index 0000000..19a2b5f --- /dev/null +++ b/src/opts.rs @@ -0,0 +1,88 @@ +use std::fmt; +use std::{path::Path, str::FromStr}; + +use clap::Parser; + +#[derive(Debug, Parser)] +#[command(name = "rcli", version, author, about, long_about = None)] +pub struct Opts { + #[command(subcommand)] + pub cmd: SubCommand, +} + +#[derive(Debug, Parser)] +pub enum SubCommand { + #[command(name = "csv", about = "Show CSV, or convert CSV to other formats")] + Csv(CsvOpts), +} + +#[derive(Debug, Clone, Copy)] +pub enum OutputFormat { + Json, + Yaml, + Toml, +} + +impl From<&OutputFormat> for &'static str { + fn from(format: &OutputFormat) -> Self { + match format { + OutputFormat::Json => "json", + OutputFormat::Yaml => "yaml", + OutputFormat::Toml => "toml", + } + } +} + +impl FromStr for OutputFormat { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + match s.to_lowercase().as_str() { + "json" => Ok(OutputFormat::Json), + "yaml" => Ok(OutputFormat::Yaml), + "toml" => Ok(OutputFormat::Toml), + _ => Err(anyhow::anyhow!("Unsupported output format: {}", s)), + } + } +} + +impl fmt::Display for OutputFormat { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s: &str = self.into(); + write!(f, "{}", s) + } +} + +#[derive(Debug, Parser)] +pub struct CsvOpts { + #[arg(short, long, value_parser = verify_input_file, help = "Input CSV file")] + pub input: String, + + #[arg(short, long, help = "Output file")] + pub output: Option, + + #[arg(short, long, value_parser = parse_format, help = "Output format", default_value = "json")] + pub format: OutputFormat, + + #[arg( + long, + help = "Indicates that the CSV file has a header row", + default_value_t = true + )] + pub header: bool, + + #[arg(short, long, help = "Delimiter character", default_value_t = ',')] + pub delimiter: char, +} + +fn verify_input_file(filename: &str) -> Result { + if Path::new(filename).exists() { + Ok(filename.to_string()) + } else { + Err(format!("Input file '{}' does not exist.", filename)) + } +} + +fn parse_format(format: &str) -> Result { + format.parse() +} diff --git a/src/process.rs b/src/process.rs new file mode 100644 index 0000000..95ad676 --- /dev/null +++ b/src/process.rs @@ -0,0 +1,65 @@ +use std::{collections::HashMap, fs}; + +use csv::Reader; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use crate::opts::{CsvOpts, OutputFormat}; +use anyhow::Ok; + +const TOML_ROOT_KEY: &str = "data"; + +#[allow(dead_code)] +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "PascalCase")] +struct Player { + name: String, + position: String, + #[serde(rename = "DOB")] + dob: String, + nationality: String, + #[serde(rename = "Kit Number")] + kit: u8, +} + +pub fn process_csv(opts: &CsvOpts) -> anyhow::Result<()> { + let output = match &opts.output { + Some(output) => output.clone(), + None => format!("output.{}", opts.format), + }; + println!( + "Processing CSV file: {}, output file: {}", + opts.input, output + ); + let mut reader_builder = Reader::from_path(&opts.input)?; + let mut parsed_records = Vec::with_capacity(128); + let headers = reader_builder.headers()?.clone(); + for result in reader_builder.records() { + let record = result?; + let json_value = serde_json::Value::Object( + headers + .iter() + // zip() the headers and record into a JSON object + .zip(record.iter()) + .map(|(k, v)| (k.to_string(), Value::String(v.to_string()))) + .collect(), + ); + parsed_records.push(json_value); + } + + let content = match opts.format { + OutputFormat::Json => serde_json::to_string_pretty(&parsed_records)?, + OutputFormat::Yaml => serde_yaml::to_string(&parsed_records)?, + // TOML format requires the root element + OutputFormat::Toml => { + let mut map = HashMap::new(); + map.insert(TOML_ROOT_KEY, &parsed_records); + toml::to_string(&map)? + } + }; + + fs::write(output, content)?; + println!("File converted successfully."); + + Ok(()) +}