Add Config::META to access the configuration definition at runtime

This commit is contained in:
Lukas Kalbertodt
2021-07-24 18:48:13 +02:00
parent e3e90430f4
commit b71eaa0464
4 changed files with 192 additions and 2 deletions

View File

@@ -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
View 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),
}