Add json output
This commit is contained in:
parent
51edacca78
commit
3ae97afde2
45
Cargo.lock
generated
45
Cargo.lock
generated
@ -107,6 +107,8 @@ name = "enc-check"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"tabled",
|
||||
]
|
||||
|
||||
@ -122,6 +124,12 @@ version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "papergrid"
|
||||
version = "0.10.0"
|
||||
@ -175,6 +183,43 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf9e0fcba69a370eed61bcf2b728575f726b50b55cba78064753d708ddc7549e"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.188"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4eca7ac642d82aa35b60049a6eccb4be6be75e599bd2e9adb5f875a737654af2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.38",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.107"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b420ce6e3d8bd882e9b243c6eed35dbc9a6110c9769e74b584e0d68d1f20c65"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.10.0"
|
||||
|
||||
@ -11,4 +11,6 @@ keywords = ["encodings", "utf-8", "utf8", "unicode"]
|
||||
|
||||
[dependencies]
|
||||
clap = { version = "4.4.6", features = ["derive"] }
|
||||
serde = { version = "1.0.188", features = ["derive"] }
|
||||
serde_json = "1.0.107"
|
||||
tabled = "0.14.0"
|
||||
|
||||
96
src/main.rs
96
src/main.rs
@ -1,4 +1,5 @@
|
||||
use clap::{Parser, Args};
|
||||
use serde::Serialize;
|
||||
use tabled::{
|
||||
builder::Builder,
|
||||
settings::{Modify, object::Rows, Alignment, Style}
|
||||
@ -6,7 +7,7 @@ use tabled::{
|
||||
|
||||
struct StringDetail {
|
||||
characters: Vec<CharacterDetail>,
|
||||
len: usize,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
struct CharacterDetail {
|
||||
@ -46,17 +47,17 @@ impl StringDetail{
|
||||
}
|
||||
|
||||
fn default() -> Self {
|
||||
Self { characters: Vec::new(), len: 0 }
|
||||
Self { characters: Vec::new(), length: 0 }
|
||||
}
|
||||
|
||||
fn push(&mut self, character:Option<char>, byte:u8){
|
||||
self.characters
|
||||
.push(CharacterDetail {
|
||||
byte_index: self.len,
|
||||
byte_index: self.length,
|
||||
character,
|
||||
byte,
|
||||
});
|
||||
self.len += 1;
|
||||
self.length += 1;
|
||||
}
|
||||
|
||||
fn push_utf16(&mut self, character:Option<char>, byte: u16){
|
||||
@ -64,28 +65,27 @@ impl StringDetail{
|
||||
self.push(character, bytes[0]);
|
||||
self.push(None, bytes[1]);
|
||||
}
|
||||
|
||||
fn print_table(&self) {
|
||||
let mut table_builder = Builder::default();
|
||||
table_builder.set_header(StringDetail::table_header());
|
||||
for i in self.table_rows() {
|
||||
table_builder.push_record(i);
|
||||
}
|
||||
|
||||
let table = table_builder.build()
|
||||
.with(Style::sharp())
|
||||
.with(Modify::new(Rows::new(1..)).with(Alignment::left()))
|
||||
.to_string();
|
||||
print!("{}", table);
|
||||
#[derive(Serialize)]
|
||||
struct StringTable {
|
||||
characters: Vec<StringTableRow>,
|
||||
length: usize,
|
||||
}
|
||||
|
||||
fn table_rows(&self) -> Vec<Vec<String>> {
|
||||
self.characters.iter()
|
||||
.map(StringDetail::to_table_row)
|
||||
.collect::<Vec<Vec<_>>>()
|
||||
#[derive(Serialize)]
|
||||
struct StringTableRow {
|
||||
unicode: String,
|
||||
unicode_hex: String,
|
||||
character: String,
|
||||
byte: String,
|
||||
hex: String,
|
||||
dec: String,
|
||||
bin: String,
|
||||
}
|
||||
|
||||
fn to_table_row(char_detail: &CharacterDetail) -> Vec<String> {
|
||||
impl StringTableRow {
|
||||
fn from(char_detail: &CharacterDetail) -> Self {
|
||||
let empty = "";
|
||||
let mut character = String::from(empty);
|
||||
let mut unicode = String::from(empty);
|
||||
@ -104,17 +104,18 @@ impl StringDetail{
|
||||
let dec = format!("{}", char_detail.byte);
|
||||
let bin = format!("{:08b}", char_detail.byte);
|
||||
|
||||
vec![
|
||||
StringTableRow {
|
||||
unicode,
|
||||
unicode_hex,
|
||||
character,
|
||||
byte,
|
||||
hex,
|
||||
dec,
|
||||
bin]
|
||||
bin
|
||||
}
|
||||
}
|
||||
|
||||
fn table_header() -> Vec<String> {
|
||||
fn header() -> Vec<String> {
|
||||
vec![
|
||||
String::from("U+dec"),
|
||||
String::from("U+hex"),
|
||||
@ -126,6 +127,49 @@ impl StringDetail{
|
||||
]
|
||||
}
|
||||
|
||||
fn to_table_row(self) -> Vec<String> {
|
||||
vec![
|
||||
self.unicode,
|
||||
self.unicode_hex,
|
||||
self.character,
|
||||
self.byte,
|
||||
self.hex,
|
||||
self.dec,
|
||||
self.bin,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
impl StringTable {
|
||||
fn from (string_details: &StringDetail) -> Self {
|
||||
let characters = string_details.characters.iter()
|
||||
.map(StringTableRow::from)
|
||||
.collect::<Vec<StringTableRow>>();
|
||||
|
||||
StringTable {
|
||||
characters,
|
||||
length: string_details.length,
|
||||
}
|
||||
}
|
||||
|
||||
fn as_table(self) -> String {
|
||||
let mut table_builder = Builder::default();
|
||||
table_builder.set_header(StringTableRow::header());
|
||||
for i in self.characters {
|
||||
table_builder.push_record(i.to_table_row());
|
||||
}
|
||||
|
||||
let table = table_builder.build()
|
||||
.with(Style::sharp())
|
||||
.with(Modify::new(Rows::new(1..)).with(Alignment::left()))
|
||||
.to_string();
|
||||
|
||||
format!("{}", table)
|
||||
}
|
||||
|
||||
fn as_json(self) -> String {
|
||||
format!("{}", serde_json::to_string(&self).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Parser)]
|
||||
@ -162,8 +206,10 @@ fn main() {
|
||||
false => StringDetail::parse_utf16(&cli.name)
|
||||
};
|
||||
|
||||
let char_table = StringTable::from(&details);
|
||||
|
||||
match cli.json {
|
||||
false => details.print_table(),
|
||||
true => panic!("Output as json is not yet implemented")
|
||||
false => println!("{}", char_table.as_table()),
|
||||
true => println!("{}", char_table.as_json()),
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user