Skip to content

Commit

Permalink
reworked with clippy suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckland committed Jul 23, 2018
1 parent f6c5eac commit 5a794b0
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 69 deletions.
21 changes: 10 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
language: rust
sudo: false

matrix:
include:
- rust: stable
- rust: beta
- rust: nightly
script:
- cargo test
- cargo test --features nightly

rust:
# - nightly-2018-07-16
- nightly

script:
- cargo test --verbose && cargo build --verbose && cargo install
script: |
cargo build --verbose &&
cargo test --verbose &&
cargo doc --verbose
branches:
only:
- staging # bors r+
- trying # bors try
- master
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "markdown_tools"
version = "0.1.0"
version = "0.1.1"
authors = ["Ramon Buckland <[email protected]>"]

[lib]
Expand All @@ -17,4 +17,4 @@ serde_yaml = "0.7"
indexmap = "1.0.1"
linked-hash-map = "0.5.1"
docopt = "1.0.0"
calamine = "0.14"
calamine = "0.14"
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ And as Markdown:
* `[ ]` Support Nested Structures in the YAML imput

### Known Issues
[ ] A Spreadsheet Cell with a Date will come out as the "magic" Excel date number :-( - https://github.com/tafia/calamine/issues/116
* A Spreadsheet Cell with a Date will come out as the "magic" Excel date number :-( - https://github.com/tafia/calamine/issues/116
* Order of columns in the YAML are not preserved. LinkedHashMap fixes it, but the serde bindings doesn't seem to work. Not sure why yet.


4 changes: 4 additions & 0 deletions doc/notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ A: assign it to an invalid type .. eg `let x:u32 = data.map().my_complex_transfo
npm install
npm run serve
```

## travis CI

- https://dev.to/cad97/great-rust-ci-1fk6
4 changes: 2 additions & 2 deletions src/bin/md_tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use markdown_tools::*;
use std::fs::File;
use std::io::prelude::*;

const USAGE: &'static str = "
const USAGE: &str = "
Markdown Tools
Usage:
Expand Down Expand Up @@ -44,7 +44,7 @@ fn read_yaml_file(filename: String) -> Result<String, String> {
file.read_to_string(&mut contents)
.expect("Unable to read the file");

Ok(mk_md_table_from_yaml(contents))
Ok(mk_md_table_from_yaml(&contents))
}

fn main() -> Result<(), String> {
Expand Down
36 changes: 22 additions & 14 deletions src/excel.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,55 @@
#![feature(slice_patterns)]

use calamine::{open_workbook_auto, DataType, Range, Reader, Sheets, Xlsx};
use calamine::{open_workbook_auto, DataType, Reader};

use mk_table_all_cols;
use types::{Table, TableRow};


fn table_formatter(table: Result<(String, Table<String, String>), (String,String)>, print_name: bool) -> String {
fn table_formatter(
table: Result<(String, Table<String, String>), (String, String)>,
print_name: bool,
) -> String {
match table {
Err((name, error)) => format!("Sheet `{}` errored: {}", name, error),
Ok((name, table_data)) => {
Err((name, error)) => format!("Sheet `{}` errored: {}", name, error),
Ok((name, table_data)) => {
if print_name {
format!("**{}**\n{}", name, mk_table_all_cols(&table_data))
} else {
mk_table_all_cols(&table_data)
}

}
}
}

pub fn spreadsheet_to_md(filename: String) -> Result<String, String> {

let results = read_excel(filename);
if results.len() <= 1 {
Ok(table_formatter(results[0].clone(), false))
} else {
Ok(results.iter().map(|table_result| table_formatter(table_result.clone(), true)).collect::<Vec<String>>().join("\n\n"))
Ok(results
.iter()
.map(|table_result| table_formatter(table_result.clone(), true))
.collect::<Vec<String>>()
.join("\n\n"))
}
}

pub fn read_excel(filename: String) -> Vec<Result<(String, Table<String, String>), (String, String)>> {
pub fn read_excel(
filename: String,
) -> Vec<Result<(String, Table<String, String>), (String, String)>> {
// opens a new workbook
let mut workbook = open_workbook_auto(filename).expect("Cannot open file");

let sheet_names = workbook.sheet_names().to_owned();

let sheets: Vec<Result<(String,Table<String, String>), (String, String)>> =
sheet_names.iter().map(|name| {
let sheets: Vec<Result<(String, Table<String, String>), (String, String)>> = sheet_names
.iter()
.map(|name| {
let maybe_sheet = workbook.worksheet_range(name);
match maybe_sheet {
None => Err((name.clone(), format!("sheet {} is empty", name))),
Some(Err(err)) => Err((name.clone(), format!("{}", err))),
Some(Ok(sheet)) => Ok((name.clone(),{
Some(Ok(sheet)) => Ok((name.clone(), {
let first_row: Vec<(usize, String)> = sheet
.rows()
.next()
Expand All @@ -67,11 +74,12 @@ pub fn read_excel(filename: String) -> Vec<Result<(String, Table<String, String>
.collect::<Vec<_>>()
})),
}
}).collect::<Vec<_>>();
})
.collect::<Vec<_>>();

sheets
}

fn md_santise(data: &DataType) -> String {
data.to_string().replace("|", "\\|").replace("\n","<br/>")
data.to_string().replace("|", "\\|").replace("\n", "<br/>")
}
86 changes: 53 additions & 33 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#![feature(use_extern_macros, wasm_custom_section, wasm_import_module, iterator_flatten, slice_patterns)]
#![feature(
use_extern_macros, wasm_custom_section, wasm_import_module, iterator_flatten, slice_patterns
)]

extern crate calamine;
extern crate indexmap;
extern crate serde_yaml;
extern crate wasm_bindgen;
extern crate linked_hash_map;

pub mod excel;
pub mod types;
Expand All @@ -15,14 +18,17 @@ use indexmap::IndexSet;
use std::cmp;
use types::*;
use wasm_bindgen::prelude::*;
use utils::*;
use std::collections::BTreeMap;

#[allow(unused_imports)]
use utils::StripMargin;

#[test]
fn can_extract_headers() {
let hdrs = vec![
treemap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")], // foo bar nop
treemap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no"), s!("aaa") => s!("ddd")], //
treemap![s!("bar") => s!("col has no foo"), s!("fff") => s!("ffsd")],
linkedhashmap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")], // foo bar nop
linkedhashmap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no"), s!("aaa") => s!("ddd")], //
linkedhashmap![s!("bar") => s!("col has no foo"), s!("fff") => s!("ffsd")],
];

let expected = indexset![s!("bar"), s!("foo"), s!("nop"), s!("aaa"), s!("fff")];
Expand Down Expand Up @@ -60,17 +66,17 @@ pub fn mk_header(heading_data: &[(String, usize)]) -> String {
format!("{}{:-^width$}|", res, "-", width = h.1)
});

return format!("{}\n{}", heading, dashed);
format!("{}\n{}", heading, dashed)
}

#[test]
fn can_mk_data() {
let tbl_md = mk_data(
&vec![(s!("foo"), 5), (s!("bar"), 8)],
&vec![
treemap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
treemap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
treemap![s!("bar") => s!("col has no foo")],
linkedhashmap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
linkedhashmap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
linkedhashmap![s!("bar") => s!("col has no foo")],
],
);

Expand Down Expand Up @@ -99,37 +105,36 @@ fn can_mk_data() {
///
/// # Arguments
///
/// `keys` - for the treemaps. keys determine cell order in a row
/// `keys` - for the linkedhashmaps. keys determine cell order in a row
/// `data` - Vector of TableRows
///
pub fn mk_data(heading_data: &[(String, usize)], data: &[TableRow<String, String>]) -> String {
let ret: Vec<String> = data
.iter()
.map(|hm| {
let m = heading_data.iter().fold(String::from("|"), |res, k| {
heading_data.iter().fold(String::from("|"), |res, k| {
let s = match hm.get(&k.0) {
Some(x) => x.to_string(),
None => "".into(),
};

format!("{}{: ^width$}|", res, s, width = k.1)
});
return m;
})
})
.collect::<Vec<String>>();

// make a new String of all the concatenated fields
return ret.join("\n");
ret.join("\n")
}

#[test]
fn can_make_table() {
let tbl_md = mk_table(
&vec![s!("foo"), s!("bar")],
&vec![
treemap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
treemap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
treemap![s!("bar") => s!("col has no foo")],
linkedhashmap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
linkedhashmap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
linkedhashmap![s!("bar") => s!("col has no foo")],
],
);

Expand Down Expand Up @@ -182,20 +187,22 @@ pub fn mk_table(headings: &[String], data: &[TableRow<String, String>]) -> Strin
#[test]
fn can_make_table_all_cols() {
let tbl_md = mk_table_all_cols(&vec![
treemap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
treemap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
treemap![s!("bar") => s!("col has no foo")],
linkedhashmap![s!("foo") => s!("ggg"), s!("bar") => s!("fred"), s!("nop") => s!("no")],
linkedhashmap![s!("foo") => s!("seventy"), s!("bar") => s!("barry"), s!("nop") => s!("no")],
linkedhashmap![s!("bar") => s!("col has no foo")],
]);

// the | below is the margin
let expected = "
|| bar | foo |nop|
||--------------|-------|---|
|| fred | ggg |no |
|| barry |seventy|no |
||col has no foo| | |"
|| foo | bar |nop|
||-------|--------------|---|
|| ggg | fred |no |
||seventy| barry |no |
|| |col has no foo| |"
.strip_margin();

println!("{}\n{}", tbl_md, expected);

assert!(tbl_md == expected);
}

Expand Down Expand Up @@ -269,9 +276,8 @@ fn can_yaml_to_md() {
/// ```
///
#[wasm_bindgen]
pub fn mk_md_table_from_yaml(yaml: String) -> String {
let deserialized_map: Table<String, String> = serde_yaml::from_str(&yaml).unwrap();
mk_table_all_cols(&deserialized_map)
pub fn mk_md_table_from_yaml(yaml: &str) -> String {
mk_table_all_cols( &load_yaml(yaml))
}

#[test]
Expand Down Expand Up @@ -306,14 +312,28 @@ fn can_yaml_to_md_with_headings() {
}

#[wasm_bindgen]
pub fn mk_md_table_from_yaml_with_headings_list(headings: String, yaml: String) -> String {
pub fn mk_md_table_from_yaml_with_headings_list(headings: &str, yaml: &str) -> String {
mk_md_table_from_yaml_with_headings(
&headings.split(",").map(String::from).collect::<Vec<_>>(),
&headings.split(',').map(String::from).collect::<Vec<_>>(),
yaml,
)
}

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

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

deserialized_map
.iter()
.map(|btree| {
btree
.iter()
.map(|(x, y)| (x.clone(), y.clone()))
.collect::<TableRow<String, String>>()
})
.collect::<Vec<_>>()

}
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use indexmap::IndexSet;
use std::collections::BTreeMap;
use linked_hash_map::LinkedHashMap;

pub type TableRow<K, V> = BTreeMap<K, V>;
pub type TableRow<K, V> = LinkedHashMap<K, V>;
pub type Table<K, V> = Vec<TableRow<K, V>>;
pub type MultiTables<K, V> = Vec<Table<K, V>>;
pub type Headers = IndexSet<String>;
5 changes: 1 addition & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

#[macro_use]

#[allow(unused_macros)]
macro_rules! s {
($s:expr) => {
Expand Down Expand Up @@ -47,7 +45,6 @@ macro_rules! indexset {
}}
}


/*
* https://gist.github.com/kardeiz/26c303957fc298212c3623c01a26f38c
*/
Expand All @@ -59,7 +56,7 @@ impl StripMargin for &'static str {
fn strip_margin(self) -> String {
let mut out = Vec::new();
for l in self.lines().filter(|x| !x.is_empty()) {
for s in l.splitn(2, '|').nth(1) {
if let Some(s) = l.splitn(2, '|').nth(1) {
out.push(s);
}
}
Expand Down

0 comments on commit 5a794b0

Please sign in to comment.