Struct crowbook::BookOptions
[−]
[src]
pub struct BookOptions { /* fields omitted */ }
Contains the options of a book.
This structure offers some facilities to check the content of an option. It also verifies, when setting an option, that it corresponds to certain values (e.g. if you expect an int, you can't set this option to "foo").
Example
use crowbook::BookOptions; let mut options = BookOptions::new(); // By default, `lang` is set to "en" assert_eq!(options.get_str("lang").unwrap(), "en"); // We can change it to "fr" options.set("lang", "fr").unwrap(); assert_eq!(options.get_str("lang").unwrap(), "fr"); // `epub.version` must be an int, we can't set it to a string let res = options.set("epub.version", "foo"); assert!(res.is_err());
Methods
impl BookOptions
[src]
fn new() -> BookOptions
Creates a new BookOptions struct from the default compiled string
fn set(&mut self, key: &str, value: &str) -> Result<Option<BookOption>>
Sets an option
Arguments
key
: the identifier of the option, e.g.: "author"value
: the value of the option as a string
Returns
- an error either if
key
is not a valid option or if the value is not of the right type. - an option containing None if key was not set, and Some(previous_value) if key was already present.
Examples
use crowbook::Book; let mut book = Book::new(); // Set author book.options.set("author", "Joan Doe").unwrap(); // Set numbering to chapters and subsections book.options.set("rendering.num_depth", "2").unwrap(); // Try to set invalid key "autor" let result = book.options.set("autor", "John Smith"); assert!(result.is_err()); // error: "author" was mispelled "autor" let result = book.options.set("rendering.num_depth", "foo"); assert!(result.is_err()); // error: numbering must be an int
fn get_str(&self, key: &str) -> Result<&str>
Gets a string option.
Returns
- A string if
key
is valid and corresponds to a string - An error either if
key
is not valid or is not a string.
Example
use crowbook::BookOptions; let options = BookOptions::new(); assert!(options.get_str("author").is_ok()); assert!(options.get_str("rendering.inline_toc").is_err());
fn get_str_vec(&self, key: &str) -> Result<&[String]>
Get a stringvec option
fn get_path(&self, key: &str) -> Result<String>
Get a path option.
Adds the correct path correction before it.
fn get_relative_path(&self, key: &str) -> Result<&str>
Get a path option
Don't add book's root path before it.
fn get_bool(&self, key: &str) -> Result<bool>
Gets a bool option
Example
let options = BookOptions::new(); assert!(options.get_bool("epub.toc.extras").is_ok());
fn get_char(&self, key: &str) -> Result<char>
Gets a char option
fn get_i32(&self, key: &str) -> Result<i32>
Gets an int option
Example
let options = BookOptions::new(); assert!(options.get_i32("rendering.num_depth").is_ok());
fn get_f32(&self, key: &str) -> Result<f32>
Gets a float option