use reliakit::json::{parse_with_limits, JsonLimits, JsonValue};
use reliakit::primitives::{Hostname, Port};
fn main() {
let input = r#"{ "host": "api.internal", "port": 8080 }"#;
let value = match parse_with_limits(input.as_bytes(), JsonLimits::conservative()) {
Ok(value) => value,
Err(err) => {
eprintln!("rejected: {err}");
return;
}
};
let JsonValue::Object(object) = &value else {
eprintln!("expected a JSON object");
return;
};
let host = object
.get("host")
.and_then(JsonValue::as_str)
.and_then(|s| Hostname::new(s).ok());
let port = object
.get("port")
.and_then(JsonValue::as_number)
.and_then(|n| n.to_u64().ok())
.and_then(|n| u16::try_from(n).ok())
.and_then(|p| Port::new(p).ok());
match (host, port) {
(Some(host), Some(port)) => println!("typed config: {host}:{port}"),
_ => println!("config failed typed validation"),
}
}