Config

Struct Config 

Source
#[non_exhaustive]
pub struct Config { pub book: BookConfig, pub build: BuildConfig, pub rust: RustConfig, /* private fields */ }
Expand description

The overall configuration object for MDBook, essentially an in-memory representation of book.toml.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§book: BookConfig

Metadata about the book.

§build: BuildConfig

Information about the build environment.

§rust: RustConfig

Information about Rust language support.

Implementations§

Source§

impl Config

Source

pub fn from_disk<P>(config_file: P) -> Result<Config, Error>
where P: AsRef<Path>,

Load the configuration file from disk.

Source

pub fn update_from_env(&mut self) -> Result<(), Error>

Updates the Config from the available environment variables.

Variables starting with MDBOOK_ are used for configuration. The key is created by removing the MDBOOK_ prefix and turning the resulting string into kebab-case. Double underscores (__) separate nested keys, while a single underscore (_) is replaced with a dash (-).

For example:

  • MDBOOK_book -> book
  • MDBOOK_BOOK -> book
  • MDBOOK_BOOK__TITLE -> book.title
  • MDBOOK_BOOK__TEXT_DIRECTION -> book.text-direction

So by setting the MDBOOK_BOOK__TITLE environment variable you can override the book’s title without needing to touch your book.toml.

Note: To facilitate setting more complex config items, the value of an environment variable is first parsed as JSON, falling back to a string if the parse fails.

This means, if you so desired, you could override all book metadata when building the book with something like

$ export MDBOOK_BOOK='{"title": "My Awesome Book", "authors": ["Michael-F-Bryan"]}'
$ mdbook build

The latter case may be useful in situations where mdbook is invoked from a script or CI, where it sometimes isn’t possible to update the book.toml before building.

Source

pub fn get<'de, T>(&self, name: &str) -> Result<Option<T>, Error>
where T: Deserialize<'de>,

Get a value from the configuration.

This fetches a value from the book configuration. The key can have dotted indices to access nested items (e.g. output.html.playground will fetch the “playground” out of the html output table).

This can only access the output and preprocessor tables.

Returns Ok(None) if the field is not set.

Returns Err if it fails to deserialize.

Source

pub fn contains_key(&self, name: &str) -> bool

Returns whether the config contains the given dotted key name.

The key can have dotted indices to access nested items (e.g. preprocessor.foo.bar will check if that key is set in the config).

This can only access the output and preprocessor tables.

Source

pub fn preprocessors<'de, T>(&self) -> Result<BTreeMap<String, T>, Error>
where T: Deserialize<'de>,

Returns the configuration for all preprocessors.

Source

pub fn outputs<'de, T>(&self) -> Result<BTreeMap<String, T>, Error>
where T: Deserialize<'de>,

Returns the configuration for all renderers.

Source

pub fn set<S, I>(&mut self, index: I, value: S) -> Result<(), Error>
where S: Serialize, I: AsRef<str>,

Set a config key, clobbering any existing values along the way.

The key can have dotted indices for nested items (e.g. output.html.playground will set the “playground” in the html output table).

§Errors

This will fail if:

  • The value cannot be represented as TOML.
  • The value is not a correct type.
  • The key is an unknown configuration option.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Config
where Config: Default,

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<Config, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl FromStr for Config

Source§

fn from_str(src: &str) -> Result<Config, Error>

Load a Config from some string.

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Config) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Config

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for Config

Auto Trait Implementations§

§

impl Freeze for Config

§

impl RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl UnwindSafe for Config

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,