mirror of
https://github.com/OMGeeky/confique.git
synced 2026-01-04 10:40:26 +01:00
Add Config::META to access the configuration definition at runtime
This commit is contained in:
@@ -5,6 +5,7 @@ pub mod internal;
|
||||
|
||||
mod error;
|
||||
mod file;
|
||||
pub mod meta;
|
||||
|
||||
|
||||
pub use serde;
|
||||
@@ -40,6 +41,12 @@ pub trait Config: Sized {
|
||||
/// values defined.
|
||||
type Partial: Partial;
|
||||
|
||||
/// A description of this configuration.
|
||||
///
|
||||
/// This is a runtime representation from the struct definition of your
|
||||
/// configuration type.
|
||||
const META: meta::Meta;
|
||||
|
||||
/// Tries to create `Self` from a potentially partial object.
|
||||
///
|
||||
/// If any required values are not defined in `partial`, an [`Error`] is
|
||||
|
||||
64
src/meta.rs
Normal file
64
src/meta.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
//! Types for [`Config::META`][super::Config::META]. Represent information about
|
||||
//! a configuration type.
|
||||
|
||||
// TODO: having all these fields public make me uncomfortable. For now it's
|
||||
// fine, but before reaching 1.0 I need to figure out how to allow future
|
||||
// additions without breaking stuff.
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Meta {
|
||||
/// The type (struct) name.
|
||||
pub name: &'static str,
|
||||
|
||||
/// Doc comments.
|
||||
pub doc: &'static [&'static str],
|
||||
|
||||
pub fields: &'static [Field],
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct Field {
|
||||
pub name: &'static str,
|
||||
pub kind: FieldKind,
|
||||
pub doc: &'static [&'static str],
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum FieldKind {
|
||||
Leaf {
|
||||
default: Option<Expr>,
|
||||
},
|
||||
Child {
|
||||
meta: &'static Meta,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Expr {
|
||||
Str(&'static str),
|
||||
Float(Float),
|
||||
Integer(Integer),
|
||||
Bool(bool),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum Float {
|
||||
F32(f32),
|
||||
F64(f64),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum Integer {
|
||||
U8(u8),
|
||||
U16(u16),
|
||||
U32(u32),
|
||||
U64(u64),
|
||||
U128(u128),
|
||||
Usize(usize),
|
||||
I8(i8),
|
||||
I16(i16),
|
||||
I32(i32),
|
||||
I64(i64),
|
||||
I128(i128),
|
||||
Isize(isize),
|
||||
}
|
||||
Reference in New Issue
Block a user