Skip to content

Commit

Permalink
feat(mdbook): Add insert_dotted_table()
Browse files Browse the repository at this point in the history
  • Loading branch information
sgoudham committed Aug 15, 2022
1 parent 48cd282 commit 37620fe
Showing 1 changed file with 107 additions and 3 deletions.
110 changes: 107 additions & 3 deletions src/toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,32 @@ use toml_edit::{Array, Document, Item, Table, Value};
pub trait DocumentExt {
fn get_or_insert_into_output_html_mut(&mut self, entry: &str) -> Result<&mut Array, String>;
fn get_or_insert_into_preprocessor_mut(&mut self, entry: &str) -> Result<&mut Item, &str>;
fn insert_dotted_table(&mut self, name: &str) -> Result<&mut Table, String>;
}

impl DocumentExt for Document {
fn insert_dotted_table(&mut self, name: &str) -> Result<&mut Table, String> {
let table_vec = name.split('.').collect::<Vec<&str>>();
if table_vec.len() == 1 || name == "." {
return Err(format!("No dotted table found in '{}'", name));
}

let mut table = self
.entry(table_vec.first().unwrap())
.or_insert(Item::Table(Table::new()))
.as_table_mut()
.unwrap();
for key in table_vec.iter().skip(1) {
table = table
.entry(key)
.or_insert(Item::Table(Table::new()))
.as_table_mut()
.unwrap()
}

Ok(table)
}

fn get_or_insert_into_output_html_mut(&mut self, entry: &str) -> Result<&mut Array, String> {
let empty_table = Item::Table(Table::default());
let empty_array = Item::Value(Value::Array(Array::default()));
Expand All @@ -22,12 +45,13 @@ impl DocumentExt for Document {
.as_value_mut()?
.as_array_mut()
})
.ok_or(format!("Could not insert 'output.html.{entry}'"))
.ok_or(format!("Could not insert 'output.html.{}'", entry))
}

fn get_or_insert_into_preprocessor_mut(&mut self, entry: &str) -> Result<&mut Item, &str> {
let empty_table = Item::Table(Table::default());
let table = self.entry("preprocessor")
let table = self
.entry("preprocessor")
.or_insert(empty_table.clone())
.as_table_mut()
.ok_or("'No table 'preprocessor' found")?
Expand All @@ -49,7 +73,7 @@ impl TableExt for Table {
}

fn additional_css(&self) -> Result<&Array, &str> {
self.get("additional-css")
self.get("additional-cground-colorss")
.and_then(|item| item.as_array())
.ok_or("'additional-css' not found")
}
Expand Down Expand Up @@ -87,3 +111,83 @@ impl ArrayExt for Array {
.and_then(|str| str.as_str())
}
}

#[cfg(test)]
mod test {

mod get_or_insert_dotted_table_mut {
use toml_edit::{Document, Formatted, Item, Table, Value};

use crate::toml::DocumentExt;

fn empty_table() -> Item {
Item::Table(Table::new())
}

fn formatted_string(value: &str) -> Item {
Item::Value(Value::String(Formatted::new(value.into())))
}

#[test]
fn with_only_dot() {
let mut document = Document::new();

let only_dot = document.insert_dotted_table(".");

assert!(only_dot.is_err())
}

#[test]
fn with_no_dots() {
let mut document = Document::new();

let winston_no_dots = document.insert_dotted_table("winston");

assert!(winston_no_dots.is_err())
}

#[test]
fn with_empty_document() {
let mut document = Document::new();

let output_html_winston = document.insert_dotted_table("output.html.winston");

assert!(output_html_winston.is_ok());
assert!(document
.get("output")
.unwrap()
.as_table()
.unwrap()
.get("html")
.unwrap()
.as_table()
.unwrap()
.get("winston")
.unwrap()
.as_table()
.is_some());
}

#[test]
fn with_dotted_table() {
let mut document = Document::new();
document
.entry("output")
.or_insert(empty_table())
.as_table_mut()
.unwrap()
.entry("html")
.or_insert(empty_table())
.as_table_mut()
.unwrap()
.insert("test", formatted_string("winston"));

let output_html_table = document.insert_dotted_table("output.html");

assert_eq!(
output_html_table.unwrap().get("test").unwrap().as_str(),
Some("winston")
)
}
}
}

0 comments on commit 37620fe

Please sign in to comment.