commit a79b7e76b2d10ea962ffde4a26b21dd8bf0ed3d5 Author: Shaz hemani Date: Mon Aug 31 15:25:12 2020 +0500 toml configs and sheet reading diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7d577d8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,28 @@ +# will have compiled files and executables # +debug/ +target/ + +# These are backup files generated by rustfmt # +**/*.rs.bk + +# IDE files # +.vscode +.idea +.iml +*.sublime-workspace + +# OS generated files # +.DS_Store +.DS_Store? +._* +.Trashes + +# Logs # +*.log + +# Temporary files # +*.bak +*.swp +*.swo +*~ +*# diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..11b051a --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,179 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "ansi_term" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "hermit-abi 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "bitflags" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "clap" +version = "2.33.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", + "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "hermit-abi" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.76" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "1.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "serde_derive" +version = "1.0.115" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "strsim" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "syn" +version = "1.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "textwrap" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "toml" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "xlsx_parser_extractor" +version = "0.1.0" +dependencies = [ + "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)", + "toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" +"checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +"checksum clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)" = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" +"checksum hermit-abi 0.1.15 (registry+https://github.com/rust-lang/crates.io-index)" = "3deed196b6e7f9e44a2ae8d94225d80302d81208b1bb673fd21fe634645c85a9" +"checksum libc 0.2.76 (registry+https://github.com/rust-lang/crates.io-index)" = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3" +"checksum proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12" +"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +"checksum serde 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "e54c9a88f2da7238af84b5101443f0c0d0a3bbdc455e34a5c9497b1903ed55d5" +"checksum serde_derive 1.0.115 (registry+https://github.com/rust-lang/crates.io-index)" = "609feed1d0a73cc36a0182a840a9b37b4a82f0b1150369f0536a9e3f2a31dc48" +"checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +"checksum syn 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)" = "891d8d6567fe7c7f8835a3a98af4208f3846fba258c1bc3c31d6e506239f11f9" +"checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" +"checksum toml 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" +"checksum unicode-width 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" +"checksum unicode-xid 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +"checksum vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +"checksum winapi 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..93dbfd1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "xlsx_parser_extractor" +version = "0.1.0" +authors = ["Shahzad "] +edition = "2018" + +[lib] +name = "xlsx_parser_extractor" +path = "src/lib.rs" + +[[bin]] +name = "xlsx_parser_extractor" +path = "src/main.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[dependencies] +clap = "2.33.3" +toml = "0.5" +serde = "1.0.97" +serde_derive = "1.0" + diff --git a/Makefile.toml b/Makefile.toml new file mode 100644 index 0000000..4ead859 --- /dev/null +++ b/Makefile.toml @@ -0,0 +1,3 @@ +# create new flow to avoid format task +[tasks.rust-flow] +dependencies = ["clean", "build", "test"] diff --git a/assets/2020-6_BKM_Rapor.xlsx b/assets/2020-6_BKM_Rapor.xlsx new file mode 100644 index 0000000..7db329c Binary files /dev/null and b/assets/2020-6_BKM_Rapor.xlsx differ diff --git a/src/cli_manager.rs b/src/cli_manager.rs new file mode 100644 index 0000000..ee96b04 --- /dev/null +++ b/src/cli_manager.rs @@ -0,0 +1,75 @@ +use clap::{App, Arg}; +#[path = "./herlpers.rs"] +mod herlpers; + +#[derive(Debug)] +pub enum Commands { + SheetParsing, + SheetGenRow, + SheetGenCurated, +} + +pub struct Args { + pub file: String, + pub command: Commands, +} + +static TYPE_HELP: &'static str = "sheet-parsing Parse the Excel File.{n}\ +sheet-gen-row Generate Rows.{n}\ +sheet-gen-curated Generate Curated Sheet"; + +impl Args { + pub fn parse() -> Self { + // New instance of application + let matches = App::new("XLSX Parser Extractor") + .version("0.1.0") + .about("Transofrm data in Excel files into the JSON format") + .arg( + Arg::with_name("PATH") + .long("path") + .short("p") + .required(true) + .takes_value(true) + .help("Sets the excel file to use"), + ) + .arg( + Arg::with_name("COMMAND") + // .help("sheet_parsing | sheet_gen_row | sheet_gen_curated") + .help(TYPE_HELP) + .next_line_help(true) + .required(true), + ) + .get_matches(); + // Calling .unwrap() is safe here because "FIELD" is required (if "FIELD" wasn't + // required we could have used an 'if let' to conditionally get the value) + let file = matches.value_of("PATH").unwrap(); + let command_args = matches.value_of("COMMAND").unwrap().into(); + let command = match command_args { + "sheet-parsing" => Commands::SheetParsing, + "sheet-gen-row" => Commands::SheetGenRow, + "sheet-gen-curated" => Commands::SheetGenCurated, + _ => panic!( + "Unknown command '{}'. For more information try --help", + command_args + ), + }; + + // helper methods to check file extension and path + // TODO: replace panic with humanize error message + let excel_type = herlpers::check_if_excel(file); + let path_exist = herlpers::path_exists(file); + + if !excel_type { + panic!("Expecting an excel file"); + } + if !path_exist { + panic!("File doesn't exist"); + } + + // returning Args struct + Self { + file: file.into(), + command: command, + } + } +} diff --git a/src/configs/configs_input_sheet.toml b/src/configs/configs_input_sheet.toml new file mode 100644 index 0000000..68c8af1 --- /dev/null +++ b/src/configs/configs_input_sheet.toml @@ -0,0 +1,7 @@ +[[input_file]] +file_name = "assets/2020-6_BKM_Rapor.xlsx" +sheet_name = "Sheet1" + +[[input_file]] +file_name = "assets/2020-6_BKM_Rapor.xlsx" +sheet_name = "Sheet1" diff --git a/src/configuration_manager.rs b/src/configuration_manager.rs new file mode 100644 index 0000000..3850d8d --- /dev/null +++ b/src/configuration_manager.rs @@ -0,0 +1,28 @@ +use serde_derive::Deserialize; +use std::fs; +use std::path::Path; + + +#[derive(Debug, Deserialize)] +struct InputFile { + file_name: Option, + sheet_name: Option, +} + +#[derive(Debug, Deserialize)] +pub struct Config { + input_file: Option>, +} + +impl Config { + pub fn get_configs(file_name: &str) -> Self { + + let string_path : String = format!("{}{}", "./src/configs/", file_name).into(); + let path = Path::new(string_path.as_str() ); + + let contents = fs::read_to_string(path) + .expect("Something went wrong reading the file"); + + toml::from_str(contents.as_str()).unwrap() + } +} \ No newline at end of file diff --git a/src/herlpers.rs b/src/herlpers.rs new file mode 100644 index 0000000..8de8d00 --- /dev/null +++ b/src/herlpers.rs @@ -0,0 +1,14 @@ +use std::path::Path; +use std::path::PathBuf; + +pub fn check_if_excel(file: &str) -> bool { + let sce = PathBuf::from(file); + match sce.extension().and_then(|s| s.to_str()) { + Some("xlsx") | Some("xlsm") | Some("xlsb") | Some("xls") => true, + _ => false, + } +} + +pub fn path_exists(file: &str) -> bool { + Path::new(file).exists() +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b86e153 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,2 @@ +pub mod cli_manager; +pub mod configuration_manager; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..48e0bbe --- /dev/null +++ b/src/main.rs @@ -0,0 +1,17 @@ +// using libarary. defined in lib.rs +use xlsx_parser_extractor::{cli_manager::Args, cli_manager::Commands, configuration_manager::Config}; + +fn main() { + let args = Args::parse(); + let Args { file, command } = args; + match command { + Commands::SheetParsing => { + println!("File Path: {} and command: {:?}", file, command); + println!("{:?}", Config::get_configs("configs_input_sheet.toml")); + } + _ => println!("Command: '{:?}'", command), + } + + thre + +}