Skip to main content

ephemeris_documents/toml/
decor.rs

1//! Reading and editing the decor of the first entry of a table.
2
3use toml_edit::{Decor, Item, Table};
4
5/// The text of the prefix of `decor`, when it has one.
6pub(super) fn prefix_str(decor: &Decor) -> Option<&str> {
7    decor.prefix()?.as_str()
8}
9
10/// The decor that prints before the first entry of `table`. A table has none
11/// of its own there, so this is the decor of a child table, of the first
12/// element of an array of tables, or of the first key.
13pub(super) fn first_decor(table: &Table) -> Option<&Decor> {
14    let (key, item) = table.iter().next()?;
15    match item {
16        Item::Table(child) => Some(child.decor()),
17        Item::ArrayOfTables(children) => Some(children.iter().next()?.decor()),
18        Item::None | Item::Value(_) => Some(table.get_key_value(key)?.0.leaf_decor()),
19    }
20}
21
22/// Edit the decor that [`first_decor`] gives. `None` when there is none.
23pub(super) fn with_first_decor_mut<R>(
24    table: &mut Table,
25    edit: impl FnOnce(&mut Decor) -> R,
26) -> Option<R> {
27    let (mut key, item) = table.iter_mut().next()?;
28    match item {
29        Item::Table(child) => Some(edit(child.decor_mut())),
30        Item::ArrayOfTables(children) => Some(edit(children.iter_mut().next()?.decor_mut())),
31        Item::None | Item::Value(_) => Some(edit(key.leaf_decor_mut())),
32    }
33}