Crate markdown_tables

Crate markdown_tables 

Source
Expand description

A library for formatting data as markdown tables.

This crate provides a simple way to format structured data as markdown tables with automatic column width calculation and special character escaping.

§Example

use markdown_tables::{MarkdownTableRow, as_table};

#[derive(Debug)]
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()]
    }
}

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(),
    },
];

let table = as_table(&people);
assert_eq!(table, "| Name  | Age | City        |\n|-------|-----|-------------|\n| Alice | 30  | New York    |\n| Bob   | 25  | Los Angeles |\n");

§Features

  • Automatic column width calculation based on content
  • Pipe character (|) escaping for data containing pipes
  • Support for Unicode characters (though alignment may not be perfect for wide characters)
  • Empty table handling (returns empty string)

Traits§

MarkdownTableRow
A trait for types that can be formatted as a row in a markdown table.

Functions§

as_table
Formats the input as a markdown-style table.