diff --git a/Cargo.lock b/Cargo.lock index 274d15b..7abd7a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,12 @@ dependencies = [ "memchr", ] +[[package]] +name = "ascii_table" +version = "4.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb3cd3541590f14025990035bb02b205870f8ca3f5297956dd2c6b32e6470fa1" + [[package]] name = "memchr" version = "2.7.5" @@ -50,5 +56,6 @@ checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" name = "wikipedia-infobox-analyzer" version = "0.1.0" dependencies = [ + "ascii_table", "regex", ] diff --git a/Cargo.toml b/Cargo.toml index 1d7c3c5..08d66ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ categories = ["command-line-utilities"] [dependencies] regex = "1.11.1" +ascii_table = "4.0.7" diff --git a/src/lib.rs b/src/lib.rs index 6e6f20f..36ddd41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #[doc = include_str!("../README.md")] pub mod wikipedia_infobox_analyzer { + use ascii_table::{Align, AsciiTable}; use regex::Regex; /// Looks for a used template that does lists wikidata properties listing @@ -21,6 +22,19 @@ pub mod wikipedia_infobox_analyzer { return properties; } + + /// Creates an ascii infobox out of the properties + pub fn format_infobox_from_used_properties(name: String, properties: Vec) -> String { + let mut ascii_table = AsciiTable::default(); + ascii_table.set_max_width(26); + ascii_table + .column(0) + .set_header(name) + .set_align(Align::Left); + + let data: Vec> = properties.iter().map(|v| vec![v]).collect(); + return ascii_table.format(data); + } } #[cfg(test)] @@ -53,4 +67,24 @@ mod tests { ] ); } + + #[test] + fn test_format_infobox_from_used_properties() { + assert_eq!( + format_infobox_from_used_properties( + "Earth".to_string(), + vec!["P31".to_string(), "P361".to_string(), "P571".to_string()] + ), + vec![ + "┌───────┐\n", + "│ Earth │\n", // item identifier: Q2 + "├───────┤\n", + "│ P31 │\n", // property: instance of + "│ P361 │\n", // property: part of + "│ P571 │\n", // property: inception + "└───────┘\n" + ] + .concat() + ); + } }