Skip to main content

Module settings

Module settings 

Source
Expand description

Reading the settings an application was configured with.

One function, get, and it answers with the type you asked for:

#[derive(Deserialize)]
struct Billing {
    region: String,
    retries: u8,
}

let billing: Billing = simple::settings::get("billing", &["region", "retries"])?;

assert_eq!(billing.region, "eu-west");
assert_eq!(billing.retries, 3);

§Why the answer is your own type

A settings read answers with a map of setting name to value, and a Value holding that map is a faithful description of the wire. It is also the shape an action then has to walk by hand: index a key, ask whether it is a string, decide what to do when it is not — at every use site, in every action, for a set of keys the action already knew when it wrote the call.

Naming a type moves all of that into one line. The keys asked for and the fields read back are declared together, retries arrives as a u8 because that is what it was declared as, and a value that does not fit the declaration is reported once, at the call, rather than at the first use. This is the same bargain crate::graphql makes for a query result, and it is made the same way — serde, and one type parameter — so an action author learns it once.

§Asking for the map itself

Some settings are chosen at runtime, and then the keys are not known when the call is written. Value is a type like any other, so asking for it hands back the map exactly as it arrived:

let chosen = "region";
let settings: Value = simple::settings::get("billing", &[chosen])?;

assert_eq!(settings[chosen], json!("eu-west"));

Both readings are the same call and the same wire. Which one you get is decided by the type on the left, and nothing else.

§What is settled before anything is sent

A read names an application and at least one key, so get establishes both at the call and reports a call that names neither without a round trip — exactly as crate::graphql::query establishes that it was handed a document. The refusal carries INVALID_TOOL_INPUT, and the host is left alone.

Functions§

get
Read the settings an application was configured with.