Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Tabled::kv method to construct KV layout #480

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 141 additions & 0 deletions tabled/src/tables/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,147 @@ impl Table {
}
}

/// Creates a Key-Value [`Table`] instance, from a list of [`Tabled`] values.
///
/// # Examples
///
/// ```
/// use tabled::{Table, Tabled};
/// use testing_table::assert_table;
///
/// #[derive(Tabled)]
/// #[tabled(rename_all = "PascalCase")]
/// struct Swim {
/// event: String,
/// time: String,
/// #[tabled(rename = "Pool Length")]
/// pool: u8,
/// }
///
/// const POOL_25: u8 = 25;
/// const POOL_50: u8 = 50;
///
/// let list = vec![
/// Swim { event: String::from("Men 100 Freestyle"), time: String::from("47.77"), pool: POOL_25 },
/// Swim { event: String::from("Men 400 Freestyle"), time: String::from("03:59.16"), pool: POOL_25 },
/// Swim { event: String::from("Men 800 Freestyle"), time: String::from("08:06.70"), pool: POOL_25 },
/// Swim { event: String::from("Men 4x100 Medley Relay"), time: String::from("03:27.28"), pool: POOL_50 },
/// ];
///
/// let table = Table::kv(list);
///
/// assert_table!(
/// table,
/// "+-------------+------------------------+"
/// "| Event | Men 100 Freestyle |"
/// "+-------------+------------------------+"
/// "| Time | 47.77 |"
/// "+-------------+------------------------+"
/// "| Pool Length | 25 |"
/// "+-------------+------------------------+"
/// "| Event | Men 400 Freestyle |"
/// "+-------------+------------------------+"
/// "| Time | 03:59.16 |"
/// "+-------------+------------------------+"
/// "| Pool Length | 25 |"
/// "+-------------+------------------------+"
/// "| Event | Men 800 Freestyle |"
/// "+-------------+------------------------+"
/// "| Time | 08:06.70 |"
/// "+-------------+------------------------+"
/// "| Pool Length | 25 |"
/// "+-------------+------------------------+"
/// "| Event | Men 4x100 Medley Relay |"
/// "+-------------+------------------------+"
/// "| Time | 03:27.28 |"
/// "+-------------+------------------------+"
/// "| Pool Length | 50 |"
/// "+-------------+------------------------+"
/// );
/// ```
///
/// A more complex example with a subtle style.
///
/// ```
/// use tabled::{Table, Tabled, settings::Style};
/// use tabled::settings::{style::HorizontalLine, Theme};
/// use testing_table::assert_table;
///
/// #[derive(Tabled)]
/// #[tabled(rename_all = "PascalCase")]
/// struct Swim {
/// event: String,
/// time: String,
/// #[tabled(rename = "Pool Length")]
/// pool: u8,
/// }
///
/// const POOL_25: u8 = 25;
/// const POOL_50: u8 = 50;
///
/// let list = vec![
/// Swim { event: String::from("Men 100 Freestyle"), time: String::from("47.77"), pool: POOL_25 },
/// Swim { event: String::from("Men 400 Freestyle"), time: String::from("03:59.16"), pool: POOL_25 },
/// Swim { event: String::from("Men 800 Freestyle"), time: String::from("08:06.70"), pool: POOL_25 },
/// Swim { event: String::from("Men 4x100 Medley Relay"), time: String::from("03:27.28"), pool: POOL_50 },
/// ];
///
/// let mut table = Table::kv(list);
///
/// let mut style = Theme::from_style(Style::rounded().remove_horizontals());
/// for entry in 1 .. table.count_rows() / Swim::LENGTH {
/// style.insert_horizontal_line(entry * Swim::LENGTH, HorizontalLine::inherit(Style::modern()));
/// }
///
/// table.with(style);
///
/// assert_table!(
/// table,
/// "╭─────────────┬────────────────────────╮"
/// "│ Event │ Men 100 Freestyle │"
/// "│ Time │ 47.77 │"
/// "│ Pool Length │ 25 │"
/// "├─────────────┼────────────────────────┤"
/// "│ Event │ Men 400 Freestyle │"
/// "│ Time │ 03:59.16 │"
/// "│ Pool Length │ 25 │"
/// "├─────────────┼────────────────────────┤"
/// "│ Event │ Men 800 Freestyle │"
/// "│ Time │ 08:06.70 │"
/// "│ Pool Length │ 25 │"
/// "├─────────────┼────────────────────────┤"
/// "│ Event │ Men 4x100 Medley Relay │"
/// "│ Time │ 03:27.28 │"
/// "│ Pool Length │ 50 │"
/// "╰─────────────┴────────────────────────╯"
/// );
/// ```
pub fn kv<I, T>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
T: Tabled,
{
let headers = T::headers();

let mut records = Vec::new();
for row in iter.into_iter() {
for (text, name) in row.fields().into_iter().zip(headers.iter()) {
let key = Text::new(name.clone().into_owned());
let value = Text::new(text.into_owned());

records.push(vec![key, value]);
}
}

let records = VecRecords::new(records);

Self {
records,
config: ColoredConfig::new(configure_grid()),
dimension: CompleteDimensionVecRecords::default(),
}
}

/// Creates a builder from a data set given.
///
/// # Example
Expand Down
Loading
Loading