| Crates.io | markdown-tables |
| lib.rs | markdown-tables |
| version | 0.1.0 |
| created_at | 2025-05-23 08:18:56.730279+00 |
| updated_at | 2025-05-23 08:18:56.730279+00 |
| description | A library for generating markdown-formatted tables |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1686024 |
| size | 21,351 |
A simple Rust library for formatting data as markdown tables.
This library provides a trait-based approach to formatting structured data as markdown tables. It handles column alignment, width calculation, and special character escaping automatically.
To use this library, implement the MarkdownTableRow trait for your data type:
use markdown_tables::{MarkdownTableRow, as_table};
struct Person {
name: String,
age: u32,
city: String,
}
impl MarkdownTableRow for Person {
fn column_names() -> Vec<&'static str> {
vec!["Name", "Age", "City"]
}
fn column_values(&self) -> Vec<String> {
vec![
self.name.clone(),
self.age.to_string(),
self.city.clone()
]
}
}
// Create some data
let people = vec![
Person {
name: "Alice".to_string(),
age: 30,
city: "New York".to_string(),
},
Person {
name: "Bob".to_string(),
age: 25,
city: "Los Angeles".to_string(),
},
];
// Format as a table
let table = as_table(&people);
println!("{}", table);
This will output:
| Name | Age | City |
|-------|-----|-------------|
| Alice | 30 | New York |
| Bob | 25 | Los Angeles |
|) in data are automatically escapedMarkdownTableRow traitTypes that implement this trait can be formatted as rows in a markdown table.
column_names() - Returns the names of the columns (used for the header row)column_values(&self) - Returns the values for this rowas_table<T: MarkdownTableRow>(table: &[T]) -> StringFormats a slice of items as a markdown table. Returns an empty string if the slice is empty.
#[derive(Debug)]
struct Product {
id: u32,
name: String,
price: f64,
}
impl MarkdownTableRow for Product {
fn column_names() -> Vec<&'static str> {
vec!["ID", "Product", "Price"]
}
fn column_values(&self) -> Vec<String> {
vec![
self.id.to_string(),
self.name.clone(),
format!("${:.2}", self.price),
]
}
}
Pipe characters in data are automatically escaped:
let data = vec![
Person {
name: "Alice | Bob".to_string(),
age: 30,
city: "New York | NY".to_string(),
},
];
// Output will have escaped pipes: Alice \| Bob