Trait confql_data_resolver::ResolveValue[][src]

pub trait ResolveValue {
    fn merge_properties(
        _value: &mut Value,
        _data_path: &DataPath<'_>
    ) -> Result<(), DataResolverError> { ... }
fn resolve_value(
        data_path: DataPath<'_>
    ) -> Result<Value, DataResolverError> { ... }
fn resolve_vec_base(_data_path: &DataPath<'_>) -> Value { ... } }
Expand description

This trait, when implemented on a type, attaches methods for retrieving a serde_yaml::Value representation of that type. For primitives, a default impl will do. For structs, you will mostly specify the merge_properties function in a very straightforward way, i.e.

use confql_data_resolver::{DataPath, DataResolverError, Merge, ResolveValue};
use serde_yaml;

struct MyObj {
    id: i32,
    name: String,
}

impl ResolveValue for MyObj {
    fn merge_properties(
        value: &mut serde_yaml::Value,
        data_path: &DataPath,
    ) -> Result<(), DataResolverError> {
        if let Ok(id) = i32::resolve_value(data_path.join("id")) {
            value.merge_at("id", id)?;
        }
        if let Ok(name) = String::resolve_value(data_path.join("name")) {
            value.merge_at("name", name)?;
        }
        Ok(())
    }
}

In fact, that’s what a procedural macro in the codebase does for you.

Provided methods

Implement this for structs as described in ResolveValue.

Resolve data from the given DataPath. The default implementation should be sufficient in most cases.

Resolve a starting value before data acquisition from actual file content. Null (default impl) is a good starting value in most cases, because it accepts any merge. Explicitly implement this in cases like impl<T: ResolveValue> ResolveValue for Vec<T> where the initial value might not be null (i.e. in the Vec case, some fields may be predefined by the file stem of your DataPath.

Implementations on Foreign Types

Implementors