-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_tabled.rs
executable file
·152 lines (138 loc) · 5.69 KB
/
lib_tabled.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/env -S cargo +nightly -Zscript
---
package.edition = "2024"
[dependencies]
indoc = "2.0.5"
json_to_table = "0.9.0"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134"
tabled = "0.17.0"
---
//! # Cargo-Script: zlib-tabled.rs
//!
//! - Effort to effect ratio isn't drawing me.
//! - Lots of bespoke syntax.
//! - BUT: *nested* tables are promising.
//! - may be worth revisiting sometime for that.
use std::{collections::BTreeMap, error::Error, ops::AddAssign, result::Result};
use indoc::indoc;
use json_to_table::json_to_table;
use serde_json::json;
use tabled::{Table, Tabled,
builder::Builder,
settings::{Style, merge::Merge}};
const LYRICS: &str = indoc!(r#"
… So, so you think you can tell heaven from hell?
Blue skies from pain?
Can you tell a green field from a cold steel rail?
A smile from a veil?
Do you think you can tell?
"#);
fn main() -> Result<(), Box<dyn Error>> {
let word_fmap = {
let mut fmap: BTreeMap<&str, usize> = BTreeMap::new();
let string = LYRICS;
for line in string.lines() {
for word in line.split_whitespace() {
fmap.entry(word).or_default().add_assign(1);
}
}
fmap
};
let word_info_list = {
let mut list = Vec::new();
for (word, count) in word_fmap.iter() {
let mut char_fmap: BTreeMap<char, usize> = BTreeMap::new();
for c in word.chars() {
char_fmap.entry(c).or_default().add_assign(1);
}
let most_common_char = char_fmap
.into_iter()
.max_by_key(|&(_, count)| count)
.map(|(c, _)| c)
.unwrap_or_default();
list.push(WordInfo {
word,
frequency: *count,
length: word.len(),
most_common_char,
});
}
list
};
println!("--------------------------------------------------------------------------");
let mut table_from_fmap = Builder::from(word_fmap).build();
table_from_fmap.with(Style::modern_rounded().remove_horizontal());
println!("{}\n", table_from_fmap);
let mut word_info_table = Table::new(&word_info_list);
word_info_table.with(Style::psql());
println!("{}\n", word_info_table);
let data = [['A', 'B', 'B'], ['A', 'W', 'E'], ['Z', 'Z', 'Z']];
let mut table_for_merge = Table::new(data);
table_for_merge.with(Merge::horizontal()).with(Merge::vertical());
println!("{}\n", table_for_merge);
let mut data_ = [['Q', 'A', 'Z'], ['A', 'A', 'A'], ['A', 'A', 'A'], ['A', 'Z', 'Z']];
for bttm in data_.iter_mut().flat_map(|row| row.iter_mut()) {
if *bttm != 'A' {
*bttm = '.';
}
}
let table_no_merge = Table::new(data_);
println!("horizontal\n{}\n", table_no_merge);
let data_h = [['Q', 'A', 'Z'], ['A', 'A', 'A'], ['A', 'A', 'A'], ['A', 'Z', 'Z']];
let mut table_hor_merge = Table::new(data_h);
table_hor_merge.with(Merge::horizontal());
println!("horizontal\n{}\n", table_hor_merge);
let data_v = [['Q', 'A', 'Z'], ['A', 'A', 'A'], ['A', 'A', 'A'], ['A', 'Z', 'Z']];
let mut table_vert_merge = Table::new(data_v);
table_vert_merge.with(Merge::vertical());
println!("vertical\n{}\n", table_vert_merge);
let data_m = [['Q', 'A', 'Z'], ['A', 'A', 'A'], ['A', 'A', 'A'], ['A', 'Z', 'Z']];
let mut table_attempt_mix_merge = Table::new(data_m);
table_attempt_mix_merge
.with(Merge::vertical())
.with(Merge::horizontal())
.with(Merge::vertical());
println!("multiple, to no effect\n{}\n", table_attempt_mix_merge);
let combined = &[
table_no_merge.clone().to_string(),
table_hor_merge.clone().to_string(),
table_vert_merge.clone().to_string(),
table_attempt_mix_merge.clone().to_string(),
];
let table_from_combined = Table::new(combined);
println!("{}\n", table_from_combined);
let combined_macro = tabled::col![
tabled::row!["raw", "hor_merge", "vert_merge", "attempt_mix_merge"],
tabled::row![table_no_merge, table_hor_merge, table_vert_merge, table_attempt_mix_merge,]
];
println!("{}\n", combined_macro);
/////////////////// JSON ///////////////////
let serde_json_value = json!(
[{"name": "Aleix Melon",
"id": "E00245",
"role": ["Dev", "DBA"],
"age": 23,
"doj": "11-12-2019",
"married": false,
"address": {
"street": "32, Laham St.",
"city": "Innsbruck",
"country": "Austria"
},
"referred-by": "E0012"
},]
);
let json_table = json_to_table(&serde_json_value).to_string();
println!("JSON:!\n{}\n", serde_json_value);
println!("JSON to Table!\n{}\n", json_table);
Ok(())
}
/// Data struct
#[derive(Tabled)]
struct WordInfo<'a> {
word: &'a str,
frequency: usize,
length: usize,
most_common_char: char,
}