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

TypedSettings.pudu

Pudu48 lines1.7 KB

GitHub ↗
1/** @Examples.TypedSettings.Program — derived binding, validation, and redaction */2module TypedSettings34import Std.App.Secret as Secret5import Std.Io as Io6import PuduLangEnvironment.Binding as Binding7import PuduLangEnvironment as Environment8import PuduLangEnvironment.Loader as Loader9import PuduLangEnvironment.Options as Options10import PuduLangEnvironment.Settings as Settings11import PuduLangEnvironment.Source as Source12import PuduLangValidator.Rule as Rule13import PuduLangValidator.Rules.Number as NumberRule14import PuduLangValidator.Validator as Validator1516/** @Examples.TypedSettings.Server — what the server reads at start-up */17type Server = {18  port: Int,19  @env("DB_URL") @secret database: Secret.Secret,20  @default("info") logLevel: Str,21  workers: Option[Int]22} derives Binding.Bind, Binding.Redacted2324/// Ports from 1024 to 65535.25fn rules() -> Validator.Validator[Server] {26  Validator.add(Validator.create(), Rule.build(NumberRule.inclusiveBetween(Rule.ruleFor("port", |given: Server| given.port), 1024, 65535)))27}2829/// Binds the settings from text sources and prints them with the secret hidden.30fn main() -> Int {31  let text = "PORT=8443\nDB_URL=postgres://app:s3cret@db/app\nWORKERS=4\n"32  let options = Options.defaults().withSources(&[Source.Text("inline", text)])33  let bound = match Loader.load(&options) {34    case Ok(variables) => Settings.bind(&variables, &rules())35    case Err(problem) => Err(problem)36  }37  match bound {38    case Ok(server) => {39      let _shown = Io.writeLine(server.redacted())40      if server.port == 8443 && server.workers == Some(4) && server.logLevel == "info" { 0 } else { 1 }41    }42    case Err(problem) => {43      let _said = Io.writeErrorLine(Environment.describe(&problem))44      145    }46  }47}48