schema
ModuleSchemas and loading.
Contents
use utopia_config::Property;Rust
pub struct PropertyOne property of a Schema.
Also implements
Debug.use utopia_config::Schema;Rust
pub struct SchemaThe shape of a config: a name and properties, each read from one key.
Rust
use utopia_config::{Kind, Property, Schema, Type};
use utopia_validators::Text;
let schema = Schema::new("App\\Config")
.property(Property::key("host", Type::of(Kind::String), "db.host", Text::new(1024)))
.property(Property::key("port", Type::nullable(Kind::String), "db.port", Text::new(5)).optional());
assert_eq!(schema.properties().len(), 2);use utopia_config::Type;Rust
pub struct TypeA property's declared type: a Kind, and whether it also accepts null.
use utopia_config::Field;Rust
pub enum FieldWhere a property reads its value from.
use utopia_config::Kind;Rust
pub enum KindThe type a property holds.
Also implements
Debug.use utopia_config::load;Rust
pub fn load(source: &dyn Source, parser: &dyn Parser, schema: &Schema) -> Result<Instance, Error>Loads a config: reads source, parses it with parser, and fills each
property of schema from its key.
Fails with Error::Load when the source has no contents, a required
key is missing, an optional key without default is missing from a
non-nullable property, a validator refuses a value
(Invalid value for <key>: <description>), a value cannot be converted
to its property's type, or a nested config is not of the property's
schema; and with Error::Parse when the contents cannot be parsed.
Rust
use utopia_config::{Json, Kind, Property, Schema, Type, Value, Variable, load};
use utopia_validators::{Boolean, Text};
let schema = Schema::new("App\\Database")
.property(Property::key("host", Type::of(Kind::String), "db.host", Text::new(1024)))
.property(Property::key("tls", Type::of(Kind::Bool), "db.config.tls", Boolean::default()));
let json = r#"{"db": {"host": "docker.internal", "config": {"tls": true}}}"#;
let config = load(&Variable::new(json), &Json, &schema)?;
assert_eq!(config.get("host"), Some(&Value::from("docker.internal")));
assert_eq!(config.get("tls"), Some(&Value::Bool(true)));
let error = load(&Variable::new(r#"{"db": {}}"#), &Json, &schema).unwrap_err();
assert_eq!(error.to_string(), "Missing required key: db.host");