conferencier 0.1.0

Minimal, typed TOML configuration for Tokio applications
Documentation
#![cfg_attr(feature = "derive", doc = include_str!("../guide/overview.md"))]
#![doc(test(attr(deny(warnings))))]
#![cfg_attr(
    not(feature = "derive"),
    doc = r#"A minimal, easy-to-use TOML configuration library for Tokio applications.

The `derive` feature is disabled in this build. Store access and manual trait
implementations remain available:

- [`ConfigStore`] is a cloneable handle to one in-memory TOML document. Its clones
  share data; in-memory operations are synchronous and only file I/O is async.
- [`ConfigScope`] describes typed configuration and runtime state, shared through
  [`SharedScope<T>`]. Loading and saving explicitly copy data to and from a store.
- [`ConfigTable`] converts plain values to and from nested TOML tables.
- [`table_path`] formats diagnostic paths for manual implementations.
- [`Error`] and [`Result`] carry file, TOML and field-conversion errors;
  [`IoOperation`] identifies the phase of a file operation that failed.
- [`toml`] and [`Datetime`] re-export the TOML types used by the public API.

The library enables only Tokio's `sync`, `rt` and `fs` features; enable runtime
features such as `macros` and `rt-multi-thread` in your own dependency. Invalid
input handled by the library returns an error rather than panicking, except for
caller-supplied code, file operations polled without a Tokio runtime, and stack
overflows from deeply nested values built in code or parsed with `toml/unbounded`.
See the [0.1.x migration guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/migration/index.html)
and [runtime requirements](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#reading-a-file-before-the-runtime-starts)
and [nesting limits](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/concepts/index.html#nesting-limits).

Enable the default `derive` feature for `#[derive(ConfigScope)]`,
`#[derive(ConfigTable)]` and the full
[attribute reference](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/derive/index.html)."#
)]
mod error;
#[cfg(feature = "derive")]
pub mod guide;
mod scope;
mod store;
mod table;
pub mod table_path;
mod value_conversion;

pub use crate::error::{Error, IoOperation, Result};
pub use crate::scope::ConfigScope;
pub use crate::scope::SharedScope;
pub use crate::store::ConfigStore;
pub use crate::table::ConfigTable;

/// The [`toml`] crate used by this library; its types appear in the public API.
///
/// Prefer `conferencier::toml::{Table, Value}` in manual table implementations
/// and raw store access so the types match this crate's TOML dependency.
/// Conferencier does not preserve the input document's comments or formatting.
pub use toml;
/// TOML date, time or datetime value, usable directly in derived scopes and tables.
///
/// This re-exports [`toml::value::Datetime`]. Its `Time::second` and
/// `Time::nanosecond` are optional, because TOML 1.1 permits times with omitted
/// seconds; see the [guide](https://docs.rs/conferencier/%5E0.1.0/conferencier/guide/derive/index.html#toml-compatibility).
pub use toml::value::Datetime;

// The derive macros share the traits' names but live in the macro namespace, so
// `use conferencier::ConfigScope;` imports both (likewise for `ConfigTable`).
#[cfg(feature = "derive")]
pub use conferencier_derive::{ConfigScope, ConfigTable};

#[doc(hidden)]
pub mod __private {
    pub use std::sync::Arc;
    pub use tokio::sync::RwLock;

    use crate::scope::SharedScope;
    pub use crate::table_path::{child_path, element_path, section_path};
    pub use crate::value_conversion::{
        array, boolean, boolean_vec, datetime, datetime_vec, float, float_vec, integer,
        integer_vec, string, string_vec, table, table_element,
    };

    #[diagnostic::on_unimplemented(
        message = "`#[confer(section = ...)]` requires `#[derive(ConfigScope)]` on the same struct",
        label = "`section` applies only to a derived ConfigScope"
    )]
    pub trait DerivedConfigScope {}

    /// Returns a snapshot of `section`: empty when absent, an error when not a table.
    pub fn read_section(store: &crate::ConfigStore, section: &str) -> crate::Result<toml::Table> {
        store.read_section(section)
    }

    /// Rejects the first key not present in the struct's loaded field names.
    pub fn deny_unknown_keys(
        table_path: &str,
        table: &toml::Table,
        known: &[&str],
    ) -> crate::Result<()> {
        match table
            .keys()
            .filter(|key| !known.contains(&key.as_str()))
            .min()
        {
            Some(key) => Err(crate::Error::unknown_key_error(table_path, key.clone())),
            None => Ok(()),
        }
    }

    /// Wraps `value` in the shared scope type used by the derive implementation.
    pub fn new_shared_scope<T>(value: T) -> SharedScope<T> {
        Arc::new(RwLock::new(value))
    }
}