diff --git a/Cargo.lock b/Cargo.lock index 3aa57fb..98a536f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -397,7 +397,7 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=a7d62ef4270f7cda5eb894f9180ced3fde28093e#a7d62ef4270f7cda5eb894f9180ced3fde28093e" +source = "git+https://github.com/ComlineProject/generation?rev=16b94d482e887f4894b458bad9e6cf66d1dbe8c3#16b94d482e887f4894b458bad9e6cf66d1dbe8c3" dependencies = [ "comline-core", "eyre", @@ -406,7 +406,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=833bade81ceaf4c5a08a5a7a27bbc646d8897793#833bade81ceaf4c5a08a5a7a27bbc646d8897793" +source = "git+https://github.com/ComlineProject/comline-rust?rev=d0c50133dbf63d841c6a5631914603668621b0fe#d0c50133dbf63d841c6a5631914603668621b0fe" dependencies = [ "comline-codegen", "comline-core", @@ -416,7 +416,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=e9c297d0ddf6c52e7170ac6884430eb879015ec6#e9c297d0ddf6c52e7170ac6884430eb879015ec6" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=9f2b7ecd70ea881c157ec69e889d16145db2feea#9f2b7ecd70ea881c157ec69e889d16145db2feea" dependencies = [ "comline-codegen", "comline-core", @@ -426,7 +426,7 @@ dependencies = [ [[package]] name = "comline-core" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=b04836d22537af00e1e7017d9a8a232ea691860b#b04836d22537af00e1e7017d9a8a232ea691860b" +source = "git+https://github.com/ComlineProject/core?rev=8243d98ffb04feef0428ec8d9fdee99148c32a4e#8243d98ffb04feef0428ec8d9fdee99148c32a4e" dependencies = [ "ariadne", "blake3", @@ -453,7 +453,7 @@ dependencies = [ [[package]] name = "comline-core-stdlib" version = "0.1.0" -source = "git+https://github.com/ComlineProject/core?rev=b04836d22537af00e1e7017d9a8a232ea691860b#b04836d22537af00e1e7017d9a8a232ea691860b" +source = "git+https://github.com/ComlineProject/core?rev=8243d98ffb04feef0428ec8d9fdee99148c32a4e#8243d98ffb04feef0428ec8d9fdee99148c32a4e" [[package]] name = "concolor" diff --git a/Cargo.toml b/Cargo.toml index 857a3b4..c506688 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,15 +29,15 @@ toml = "0.8" # needs real I/O (git fetch, filesystem), so it's off by default for # `comline-core`'s wasm32 consumers (the playground editor, the language # server); the CLI is the one consumer that actually runs `comline build`. -comline-core = { git = "https://github.com/ComlineProject/core", rev = "b04836d22537af00e1e7017d9a8a232ea691860b", features = ["deps"] } +comline-core = { git = "https://github.com/ComlineProject/core", rev = "8243d98ffb04feef0428ec8d9fdee99148c32a4e", features = ["deps"] } # All three pinned to revs that agree on one `comline-codegen`, and bumped # together: comline-rust and comline-typescript's codegen crates consume # `comline-codegen`'s types, so the rev below must be byte-identical to the # one *they* pin - Cargo only unifies a git dependency across identical rev # strings, and two copies of `GenRequest` are two incompatible types. -comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "a7d62ef4270f7cda5eb894f9180ced3fde28093e" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "833bade81ceaf4c5a08a5a7a27bbc646d8897793", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "e9c297d0ddf6c52e7170ac6884430eb879015ec6", optional = true } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "16b94d482e887f4894b458bad9e6cf66d1dbe8c3" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "d0c50133dbf63d841c6a5631914603668621b0fe", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "9f2b7ecd70ea881c157ec69e889d16145db2feea", optional = true } [features] # Which language generators are compiled in. Drop one to shed its whole diff --git a/src/cli.rs b/src/cli.rs index a900f13..7cf0f9c 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -119,6 +119,11 @@ Examples: #[arg(long)] no_external_std: bool, + /// Run `@validators` checks while decoding: `off`, `collect` (fail + /// with every failure) or `first` (fail with the first) + #[arg(long, value_name = "WHEN")] + validate_on_decode: Option, + /// Print what would be written, without writing anything #[arg(long)] dry_run: bool, diff --git a/src/commands/generate.rs b/src/commands/generate.rs index 36df8d7..8e044f1 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -221,6 +221,7 @@ fn generate_once(work_dir: &Path, overrides: &Overrides, dry_run: bool, list: bo lang_version: t.lang_version.clone(), default_framing: t.default_framing.clone(), external_std: t.external_std, + validate_on_decode: t.validate_on_decode, }; // Deliberately not `core_err`: a generator's errors are about // schema/package *structure* (e.g. a namespace that's both a diff --git a/src/commands/new.rs b/src/commands/new.rs index 4e779f0..21728c9 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -45,6 +45,7 @@ const COMLINE_TOML: &str = "\ # external_std = false # reference `comline_std` rather than inlining std; defaults on in `lib` # package_versions = \"latest\" # latest | all | [\"0.3.0\", \"0.4.0\"] # default_framing = \"jsonrpc\" # wire framing for protocols with no `@framing` (else datagram) +# validate_on_decode = \"off\" # run `@validators` while decoding: off | collect | first # # One block per declared language you want to configure. A block sets that # language's output; it does not decide which languages are generated, and diff --git a/src/gen_config.rs b/src/gen_config.rs index b374c2f..85ad065 100644 --- a/src/gen_config.rs +++ b/src/gen_config.rs @@ -11,6 +11,7 @@ use std::path::{Path, PathBuf}; +use comline_codegen::validation::ValidateOnDecode; use comline_core::utils::templating::recurse_render; use miette::{miette, IntoDiagnostic, Result, WrapErr}; use serde::{Deserialize, Serialize}; @@ -109,6 +110,10 @@ pub struct Generate { /// it, shared by every target unless overridden. Only a generator that /// knows how (today, Rust's) acts on it. Default `false`. pub external_std: Option, + /// Whether decoding a value runs its `@validators` checks: `off` (the + /// default), `collect` (fail with every failure) or `first` (fail with + /// the first). Shared by every target unless overridden. + pub validate_on_decode: Option, /// `[[generate.target]]` blocks. #[serde(rename = "target")] pub targets: Vec, @@ -130,6 +135,7 @@ pub struct Target { pub package_versions: Option, pub default_framing: Option, pub external_std: Option, + pub validate_on_decode: Option, /// `lib` mode only: write the crate straight into `out` instead of /// `//`. Target-only (no `[generate]` default, no flag/env) /// — a shared `out` across several `lib` targets needs the per-language @@ -190,6 +196,8 @@ pub struct Overrides<'a> { /// the mode (see [`default_external_std`]), so either direction can be /// the one a given run needs to override. pub external_std: Option, + /// `--validate-on-decode`. + pub validate_on_decode: Option<&'a str>, } /// A fully-resolved target: every field decided, `out` made absolute. @@ -207,6 +215,8 @@ pub struct ResolvedTarget { pub flatten: bool, /// See [`Generate::external_std`]. pub external_std: bool, + /// See [`Generate::validate_on_decode`]. + pub validate_on_decode: ValidateOnDecode, } /// Variables a `layout` template can reference. @@ -343,6 +353,7 @@ pub fn resolve( package_versions: None, default_framing: None, external_std: None, + validate_on_decode: None, flatten: false, }] } else { @@ -374,12 +385,15 @@ pub fn resolve( }; } - let field_override = - ov.out.is_some() || ov.layout.is_some() || ov.mode.is_some() || ov.external_std.is_some(); + let field_override = ov.out.is_some() + || ov.layout.is_some() + || ov.mode.is_some() + || ov.external_std.is_some() + || ov.validate_on_decode.is_some(); if field_override && selected.len() > 1 && ov.target.is_none() { return Err(miette!( - "`--out` / `--layout` / `--mode` / `--external-std` need `--target ` \ - when more than one target is configured ({} here)", + "`--out` / `--layout` / `--mode` / `--external-std` / `--validate-on-decode` \ + need `--target ` when more than one target is configured ({} here)", selected.len() )); } @@ -391,6 +405,7 @@ pub fn resolve( let env_layout = env_var("COMLINE_GENERATE_LAYOUT"); let env_mode = env_var("COMLINE_GENERATE_MODE"); let env_external_std = env_var_bool("COMLINE_GENERATE_EXTERNAL_STD"); + let env_validate_on_decode = env_var("COMLINE_GENERATE_VALIDATE_ON_DECODE"); let mut resolved = Vec::with_capacity(selected.len()); for t in selected { @@ -443,6 +458,21 @@ pub fn resolve( cfg.generate.external_std, default_external_std(&mode), ); + let validate_on_decode = pick( + ov.validate_on_decode, + &env_validate_on_decode, + &t.validate_on_decode, + &cfg.generate.validate_on_decode, + ValidateOnDecode::Off.name(), + ); + let validate_on_decode = ValidateOnDecode::parse(&validate_on_decode).ok_or_else(|| { + miette!( + "`validate_on_decode = \"{validate_on_decode}\"` for target `{}` - expected one of \ + {}", + t.language, + ValidateOnDecode::NAMES.map(|n| format!("`{n}`")).join(", ") + ) + })?; let lang_version = t .lang_version @@ -485,6 +515,7 @@ pub fn resolve( default_framing, flatten: t.flatten, external_std, + validate_on_decode, }); } @@ -818,4 +849,42 @@ mod tests { let resolved = resolve(&cfg, &declared, Path::new("/x"), &ov).unwrap(); assert!(resolved[0].external_std); } + + #[test] + fn validate_on_decode_defaults_to_off() { + let cfg = parse("[[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n"); + let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap(); + assert_eq!(resolved[0].validate_on_decode, ValidateOnDecode::Off); + } + + #[test] + fn validate_on_decode_target_wins_over_section_and_the_flag_over_both() { + let cfg = parse( + "[generate]\nvalidate_on_decode = \"collect\"\n\n\ + [[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n\ + validate_on_decode = \"first\"\n", + ); + let resolved = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap(); + assert_eq!(resolved[0].validate_on_decode, ValidateOnDecode::First); + + let ov = Overrides { + validate_on_decode: Some("off"), + ..Default::default() + }; + let resolved = resolve(&cfg, &[], Path::new("/x"), &ov).unwrap(); + assert_eq!(resolved[0].validate_on_decode, ValidateOnDecode::Off); + } + + #[test] + fn an_unknown_validate_on_decode_names_the_accepted_values() { + let cfg = parse( + "[generate]\nvalidate_on_decode = \"always\"\n\n\ + [[generate.target]]\nlanguage = \"rust\"\nlang_version = \"1.70.0\"\n", + ); + let error = resolve(&cfg, &[], Path::new("/x"), &Overrides::default()).unwrap_err(); + assert_eq!( + error.to_string(), + "`validate_on_decode = \"always\"` for target `rust` - expected one of `off`, `collect`, `first`" + ); + } } diff --git a/src/main.rs b/src/main.rs index 63be049..ba3fc96 100644 --- a/src/main.rs +++ b/src/main.rs @@ -123,6 +123,7 @@ fn run(cli: Cli) -> Result<()> { mode, external_std, no_external_std, + validate_on_decode, dry_run, list, watch, @@ -138,6 +139,7 @@ fn run(cli: Cli) -> Result<()> { external_std: external_std .then_some(true) .or(no_external_std.then_some(false)), + validate_on_decode: validate_on_decode.as_deref(), }; commands::generate::run(&work_dir, &overrides, dry_run, list, watch) } diff --git a/tests/cli/end_to_end.rs b/tests/cli/end_to_end.rs index 44e76dd..2a0a523 100644 --- a/tests/cli/end_to_end.rs +++ b/tests/cli/end_to_end.rs @@ -150,3 +150,100 @@ fn a_generated_protocol_crate_runs_a_real_round_trip() { String::from_utf8_lossy(&out.stderr), ); } + +/// Driven against the generated `validated_e2e` crate (see +/// `tests/fixtures/validated_project`, which sets +/// `validate_on_decode = "collect"`). +const VALIDATED_DRIVER: &str = r#" +use std::thread; + +use comline_runtime::contract::{CallError, Fault, RuntimeError}; +use comline_runtime::format::MsgPack; +use comline_runtime::transport::duplex; +use comline_runtime::validation::Validate; +use validated_e2e::accounts::{Account, Accounts, AccountsClient, AccountsDispatcher, AccountsRenameError}; + +struct Svc; + +impl Accounts for Svc { + fn rename(&self, to: String) -> Result> { + Ok(Account { handle: to, nickname: None, friends: vec![] }) + } +} + +#[test] +fn std_validators_run_through_an_alias() { + let bad = Account { + handle: "a".into(), + nickname: Some("far_too_long".into()), + friends: vec!["ok".into(), "x".into()], + }; + let failures: Vec<(String, String)> = + bad.validate().unwrap_err().0.into_iter().map(|e| (e.field, e.message)).collect(); + assert_eq!( + failures, + [ + ("handle".to_string(), "handle must be at least 2 characters".to_string()), + ("nickname".to_string(), "nickname must be at most 8 characters".to_string()), + ("friends[1]".to_string(), "friends[1] must be at least 2 characters".to_string()), + ] + ); +} + +#[test] +fn a_call_with_arguments_failing_their_checks_is_refused() { + let (client_side, provider_side) = duplex(); + let provider = thread::spawn(move || { + let mut provider_side = provider_side; + AccountsDispatcher(Svc).serve(&mut provider_side, MsgPack).unwrap(); + }); + let mut client = AccountsClient::connect(client_side, MsgPack).expect("handshake"); + + assert_eq!(client.rename("ada".into()).unwrap().handle, "ada"); + match client.rename("x".into()).unwrap_err() { + CallError::Runtime(RuntimeError::Provider) => {} + other => panic!("expected the call refused, got {other:?}"), + } + // and the connection survives it + assert_eq!(client.rename("bob".into()).unwrap().handle, "bob"); + + drop(client); + provider.join().unwrap(); +} +"#; + +/// `@validators` through `comline generate` itself: std's `StringBounds` +/// reached by `use`, carried by a `type` alias, run by the generated +/// `validate()` - and, with `validate_on_decode = "collect"` in +/// `comline.toml`, a call whose arguments fail is refused by the provider. +#[test] +fn generated_validators_run_end_to_end() { + let temp = tempfile::tempdir().unwrap(); + let project = copy_fixture("validated_project", temp.path()); + + comline_cmd() + .current_dir(&project) + .args([ + "generate", "--target", "rust", "--mode", "lib", "--out", "gen", + ]) + .assert() + .success(); + + let crate_dir = project.join("gen/rust"); + fs::create_dir_all(crate_dir.join("tests")).unwrap(); + fs::write(crate_dir.join("tests/validation.rs"), VALIDATED_DRIVER).unwrap(); + + let out = Command::new(env!("CARGO")) + .args(["test", "--quiet"]) + .current_dir(&crate_dir) + .env("CARGO_TARGET_DIR", crate_dir.join("target")) + .output() + .expect("run cargo test on the generated crate"); + + assert!( + out.status.success(), + "the generated crate's validation test failed\n--- stdout ---\n{}\n--- stderr ---\n{}", + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr), + ); +} diff --git a/tests/cli/util.rs b/tests/cli/util.rs index c192d20..fb279de 100644 --- a/tests/cli/util.rs +++ b/tests/cli/util.rs @@ -32,6 +32,9 @@ pub fn copy_fixture(name: &str, temp: &Path) -> PathBuf { .join(name); fs::create_dir_all(&dest).unwrap(); fs::copy(src.join("config.idp"), dest.join("config.idp")).unwrap(); + if src.join("comline.toml").exists() { + fs::copy(src.join("comline.toml"), dest.join("comline.toml")).unwrap(); + } copy_dir(&src.join("src"), &dest.join("src")); dest } diff --git a/tests/fixtures/validated_project/comline.toml b/tests/fixtures/validated_project/comline.toml new file mode 100644 index 0000000..e10aca8 --- /dev/null +++ b/tests/fixtures/validated_project/comline.toml @@ -0,0 +1,2 @@ +[generate] +validate_on_decode = "collect" diff --git a/tests/fixtures/validated_project/config.idp b/tests/fixtures/validated_project/config.idp new file mode 100644 index 0000000..93b2a92 --- /dev/null +++ b/tests/fixtures/validated_project/config.idp @@ -0,0 +1,8 @@ +congregation validated_e2e +specification_version = 1 + +code_generation = { + languages = { + rust#1.70.0 = {} + } +} diff --git a/tests/fixtures/validated_project/src/accounts.ids b/tests/fixtures/validated_project/src/accounts.ids new file mode 100644 index 0000000..386ea41 --- /dev/null +++ b/tests/fixtures/validated_project/src/accounts.ids @@ -0,0 +1,14 @@ +use std::validators::StringBounds + +@validators = [StringBounds(min_chars = 2, max_chars = 8)] +type Handle = string + +struct Account { + handle: Handle + optional nickname: Handle + friends: Handle[] +} + +protocol Accounts { + function rename(to: Handle) -> Account; +}