Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-environment

Environment files for Pudu: load .env variables safely with layered files, discovery, expansion, typed reads, and secrets kept out of every message

0.1.0Apache-2.01

InstallClose

Settings.pudu

Pudu30 lines1.5 KB

GitHub ↗
1/** @Environment.Settings.Module — typed settings bound from variables */2module PuduLangEnvironment.Settings34import PuduLangEnvironment.Binding as Binding5import PuduLangEnvironment.Constants.Messages as Messages6import PuduLangEnvironment as Environment7import PuduLangEnvironment.Utils.Template as Template8import PuduLangEnvironment.Variables as Variables9import PuduLangValidator.Result as Validated10import PuduLangValidator.Validator as Validator1112/// The settings record bound from the variables by its derived `Bind`, once the validator accepts13/// it; a refusal names each missing variable, the first malformed one, or each failed property.14export fn bind[T: Binding.Bind](variables: &Variables.Variables, validator: &Validator.Validator[T]) -> Result[T, Environment.Problem] {15  check(T.bind(variables) ?, validator)16}1718/// The settings once the validator accepts them. Failures of `Error` severity refuse them as19/// `Invalid`, naming each property and code but never the validator's message, which may quote a20/// value; warnings and notes do not refuse.21export fn check[T](settings: T, validator: &Validator.Validator[T]) -> Result[T, Environment.Problem] {22  let outcome = Validator.validate(validator, settings)23  let refusing = outcome.errors.filter(|failure: Validated.Failure| failure.severity == Validated.Error)24  if refusing.isEmpty() {25    Ok(settings)26  } else {27    Err(Environment.Invalid(refusing.map(|failure: Validated.Failure| Template.fill(Messages.FAILED_PROPERTY, &[failure.propertyName, failure.errorCode]))))28  }29}30