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