diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 0db9433..3eebb67 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,8 +1,6 @@ name: Publish on: push: - branches: - - master tags: - v* jobs: diff --git a/madato/Cargo.toml b/madato/Cargo.toml index 8a49b7b..ca297f3 100644 --- a/madato/Cargo.toml +++ b/madato/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "madato" -version = "0.5.3" +version = "0.5.4" authors = ["Ramon Buckland "] license = "MIT/Apache-2.0" description = "A library and command line tool for working tabular data (XLS, ODS, CSV, YAML), and Markdown" @@ -20,7 +20,8 @@ crate-type = ["rlib"] regex = "1" serde = "1.0" serde_derive = "1.0" -serde_yaml = "0.7.5" +serde_yaml = "0.8" +serde_json = "1.0" indexmap = "1.0.1" [dependencies.linked-hash-map] diff --git a/madato/src/lib.rs b/madato/src/lib.rs index b890411..48151f8 100644 --- a/madato/src/lib.rs +++ b/madato/src/lib.rs @@ -144,7 +144,7 @@ pub fn mk_md_data( ) -> String { let filters: Option> = render_options.clone().and_then(|ro| ro.filters); - let iter: Box>> = match filters { + let iter: Box>> = match filters { None => Box::new(data.iter()), Some(vfilts) => Box::new( data.iter() diff --git a/madato/src/yaml.rs b/madato/src/yaml.rs index cdacf62..0ca4ed9 100644 --- a/madato/src/yaml.rs +++ b/madato/src/yaml.rs @@ -4,8 +4,6 @@ use linked_hash_map::LinkedHashMap; use std::fs::File; use std::io::prelude::*; use types::*; -use serde_yaml::from_str; -use serde_yaml::to_string; #[allow(unused_imports)] use utils::StripMargin; @@ -79,7 +77,12 @@ fn can_yaml_to_md_with_headings() { } fn load_yaml(yaml: &str) -> Table { - let deserialized_map: Table = from_str(&yaml).unwrap(); + let deserialized_map: Table = serde_yaml::from_str(&yaml).unwrap(); + deserialized_map +} + +fn _load_json(json: &str) -> Table { + let deserialized_map: Table = serde_json::from_str(&json).unwrap(); deserialized_map } @@ -131,9 +134,24 @@ pub fn mk_yaml_from_table_result( // if we only have one table, strip off the key (get just the value) if table_map.len() == 1 { - to_string(&table_map.values().next().unwrap()).unwrap() + serde_yaml::to_string(&table_map.values().next().unwrap()).unwrap() + } else { + serde_yaml::to_string(&table_map).unwrap() + } +} + +/// Given results of tables, throw them back out as JSON +pub fn mk_json_from_table_result( + tables: Vec, ErroredTable>>, +) -> String { + let table_map: LinkedHashMap> = + tables.into_iter().filter_map(Result::ok).collect(); + + // if we only have one table, strip off the key (get just the value) + if table_map.len() == 1 { + serde_json::to_string_pretty(&table_map.values().next().unwrap()).unwrap() } else { - to_string(&table_map).unwrap() + serde_json::to_string_pretty(&table_map).unwrap() } } diff --git a/madato_cal/Cargo.toml b/madato_cal/Cargo.toml index 5cf3126..714c6b4 100644 --- a/madato_cal/Cargo.toml +++ b/madato_cal/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "madato_cal" -version = "0.5.3" +version = "0.5.4" authors = ["Ramon Buckland "] license = "MIT/Apache-2.0" description = "XLS, ODS Support for the madato lib and command line" @@ -15,4 +15,4 @@ path = "src/lib.rs" [dependencies] madato = { path = "../madato/" } -calamine = "0.14" +calamine = "0.18" diff --git a/madato_cmd/Cargo.toml b/madato_cmd/Cargo.toml index 5db8a30..f547df7 100644 --- a/madato_cmd/Cargo.toml +++ b/madato_cmd/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "madato_cmd" -version = "0.5.3" +version = "0.5.4" authors = ["Ramon Buckland "] license = "MIT/Apache-2.0" description = "Command Line Binary for madato" diff --git a/madato_cmd/src/main.rs b/madato_cmd/src/main.rs index eec4cfd..5dcc427 100644 --- a/madato_cmd/src/main.rs +++ b/madato_cmd/src/main.rs @@ -21,19 +21,27 @@ Usage: madato --version Options: - table Generate Makrdown or YAML tables from a Source. + table Generate Makrdown or YAML tables from a Source (YAML, ODS, XLSX, CSV) sheetlist Read an Excel/ODS file and list out the names in the sheet. Input Filename. - -t --type Input Type. + -t --type Input Type. XLSX(xls, xlsx, xlsm, xlsb, ods), YAML(table/row structure) or CSV -s --sheetname When a Spreadsheet, restrict to just one of the sheets. - -o --outputtype MD (Markdown) or YAML. [default: MD] + -o --outputtype JSON, MD (Markdown) or YAML. [default: MD] -f --filters Filter data in the results based on a simple, key=value -c --columns List of Columns to output 'only' -h --help Show this screen. --version Show version. +Quick examples + + madato table -t XLSX -o JSON workbook3.xlsx + madato table -t XLSX -o MD --sheetname Sheet2 someSheet_workbook.ods + madato table -t XLSX -o YAML workbook3.xlsx + madato table -t YAML -o MD my_structured_data.yaml + madato table -t XLSX -o YAML --filters 'Col1=Year.* Col[4-9]=.*' workbook3.xlsx + Filtering Example: Basic Filtering support occurs on a row by row basis where the key=value pair need to match. @@ -77,6 +85,7 @@ enum FileType { #[derive(Debug, Deserialize)] enum OutputType { YAML, + JSON, MD, } @@ -134,11 +143,16 @@ fn main() -> Result<(), String> { OutputType::MD => match args.flag_type { Some(FileType::YAML) => yaml_file_to_md(args.arg_filename, &render_options), Some(FileType::XLSX) => spreadsheet_to_md(args.arg_filename, &render_options), - _ => Err(String::from("not implemented")), + Some(FileType::CSV) => Err(String::from("CSV not implemented")), + _ => Err(String::from("No file type specified")) }, OutputType::YAML => { let tables = read_excel_to_named_tables(args.arg_filename, args.flag_sheetname); Ok(mk_yaml_from_table_result(tables)) + }, + OutputType::JSON => { + let tables = read_excel_to_named_tables(args.arg_filename, args.flag_sheetname); + Ok(mk_json_from_table_result(tables)) } }; diff --git a/madato_wasm/Cargo.toml b/madato_wasm/Cargo.toml index 3a66d11..665c5e2 100644 --- a/madato_wasm/Cargo.toml +++ b/madato_wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "madato_wasm" -version = "0.5.3" +version = "0.5.4" authors = ["Ramon Buckland "] license = "MIT/Apache-2.0" description = "JS API WASM Binding for madato"