use decodal::{Data, EmptyLoader, Engine, HostValue};

fn main() -> decodal::Result<()> {
    let mut engine = Engine::new(EmptyLoader);

    engine.bind_global(
        "Service",
        HostValue::object([
            ("name", HostValue::string_type()),
            ("port", HostValue::int_type().gt(443).default_int(8443)?),
            ("enabled", HostValue::bool_type().default_bool(true)?),
        ]),
    )?;

    let module = engine.add_root_source(
        "embedded-main",
        "embedded-main",
        r#"
        Service & {
          name = "api";
          port = 9443;
        }
        "#,
    )?;

    let value = engine.eval_module(module)?;
    let data = engine.materialize(&value)?;

    if let Data::Object(fields) = data {
        assert_eq!(fields[0].value, Data::String(String::from("api")));
        assert_eq!(fields[1].value, Data::Int(9443));
        assert_eq!(fields[2].value, Data::Bool(true));
    } else {
        panic!("expected object");
    }

    Ok(())
}
