Format an infobox out of those properties

Raymon Zutekouw 2 days ago
parent e9cfb0293b
commit 18b98f86a9
Signed by: raymon
GPG Key ID: 0E62222846283925
  1. 7
      Cargo.lock
  2. 1
      Cargo.toml
  3. 34
      src/lib.rs

7
Cargo.lock generated

@ -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",
]

@ -16,3 +16,4 @@ categories = ["command-line-utilities"]
[dependencies]
regex = "1.11.1"
ascii_table = "4.0.7"

@ -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>) -> 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<Vec<&String>> = 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()
);
}
}

Loading…
Cancel
Save