Project: config validator
設定値の検証は、失敗理由を Result の Err に入れて返します。呼び出し側は最初に validation を通し、以後は検証済みの値だけを使います。
TESTstdionormalize_newlines
#entry main
#indent 4
#target std
#import "core/result" as *
#import "std/test" as *
#import "core/field" as *
#import "core/math" as *
struct ServerConfig:
port %i32
workers %i32
fn validate_config %fn ServerConfig Result ServerConfig str \config:
if:
lt get config "port" 1
then:
Result::Err "port too small"
else:
if:
lt get config "workers" 1
then:
Result::Err "workers too small"
else:
Result::Ok config
fn expect_valid %fn ServerConfig Result unit str \config:
match validate_config config:
Result::Ok _ok:
Result::Ok unit
Result::Err msg:
Result::Err msg
fn expect_invalid %fn ServerConfig fn str Result unit str \config\expected:
match validate_config config:
Result::Ok _ok:
Result::Err "expected invalid config"
Result::Err msg:
check_str_eq expected msg
fn main %impure fn void i32 \void:
let checks:
checks_new
|> checks_push expect_valid ServerConfig 8080 4
|> checks_push expect_invalid ServerConfig 0 4 "port too small"
|> checks_push expect_invalid ServerConfig 8080 0 "workers too small"
let shown checks_print_report checks
checks_exit_code shownServerConfig のような small struct は、validation を通した後に次の層へ渡すと API 境界が読みやすくなります。