Skip to content

Commit

Permalink
Merge pull request #8 from inosion/cmdline_fix
Browse files Browse the repository at this point in the history
clarified help + json
  • Loading branch information
rbuckland authored Jun 8, 2021
2 parents f727393 + 25e1b4f commit 5bec61f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 18 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
name: Publish
on:
push:
branches:
- master
tags:
- v*
jobs:
Expand Down
5 changes: 3 additions & 2 deletions madato/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madato"
version = "0.5.3"
version = "0.5.4"
authors = ["Ramon Buckland <[email protected]>"]
license = "MIT/Apache-2.0"
description = "A library and command line tool for working tabular data (XLS, ODS, CSV, YAML), and Markdown"
Expand All @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion madato/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub fn mk_md_data(
) -> String {
let filters: Option<Vec<KVFilter>> = render_options.clone().and_then(|ro| ro.filters);

let iter: Box<Iterator<Item = &TableRow<String, String>>> = match filters {
let iter: Box<dyn Iterator<Item = &TableRow<String, String>>> = match filters {
None => Box::new(data.iter()),
Some(vfilts) => Box::new(
data.iter()
Expand Down
28 changes: 23 additions & 5 deletions madato/src/yaml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -79,7 +77,12 @@ fn can_yaml_to_md_with_headings() {
}

fn load_yaml(yaml: &str) -> Table<String, String> {
let deserialized_map: Table<String, String> = from_str(&yaml).unwrap();
let deserialized_map: Table<String, String> = serde_yaml::from_str(&yaml).unwrap();
deserialized_map
}

fn _load_json(json: &str) -> Table<String, String> {
let deserialized_map: Table<String, String> = serde_json::from_str(&json).unwrap();
deserialized_map
}

Expand Down Expand Up @@ -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<Result<NamedTable<String, String>, ErroredTable>>,
) -> String {
let table_map: LinkedHashMap<String, Table<String, String>> =
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()
}
}

Expand Down
4 changes: 2 additions & 2 deletions madato_cal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madato_cal"
version = "0.5.3"
version = "0.5.4"
authors = ["Ramon Buckland <[email protected]>"]
license = "MIT/Apache-2.0"
description = "XLS, ODS Support for the madato lib and command line"
Expand All @@ -15,4 +15,4 @@ path = "src/lib.rs"

[dependencies]
madato = { path = "../madato/" }
calamine = "0.14"
calamine = "0.18"
2 changes: 1 addition & 1 deletion madato_cmd/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madato_cmd"
version = "0.5.3"
version = "0.5.4"
authors = ["Ramon Buckland <[email protected]>"]
license = "MIT/Apache-2.0"
description = "Command Line Binary for madato"
Expand Down
22 changes: 18 additions & 4 deletions madato_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<filename> Input Filename.
-t --type <type> Input Type.
-t --type <type> Input Type. XLSX(xls, xlsx, xlsm, xlsb, ods), YAML(table/row structure) or CSV
-s --sheetname <sheetname> When a Spreadsheet, restrict to just one of the sheets.
-o --outputtype <outputtype> MD (Markdown) or YAML. [default: MD]
-o --outputtype <outputtype> JSON, MD (Markdown) or YAML. [default: MD]
-f --filters <filters> Filter data in the results based on a simple, key=value
-c --columns <column> 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.
Expand Down Expand Up @@ -77,6 +85,7 @@ enum FileType {
#[derive(Debug, Deserialize)]
enum OutputType {
YAML,
JSON,
MD,
}

Expand Down Expand Up @@ -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))
}
};

Expand Down
2 changes: 1 addition & 1 deletion madato_wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "madato_wasm"
version = "0.5.3"
version = "0.5.4"
authors = ["Ramon Buckland <[email protected]>"]
license = "MIT/Apache-2.0"
description = "JS API WASM Binding for madato"
Expand Down

0 comments on commit 5bec61f

Please sign in to comment.