Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Print what would be written, without writing anything
#[arg(long)]
dry_run: bool,
Expand Down
1 change: 1 addition & 0 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 73 additions & 4 deletions src/gen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<bool>,
/// 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<String>,
/// `[[generate.target]]` blocks.
#[serde(rename = "target")]
pub targets: Vec<Target>,
Expand All @@ -130,6 +135,7 @@ pub struct Target {
pub package_versions: Option<VersionSpec>,
pub default_framing: Option<String>,
pub external_std: Option<bool>,
pub validate_on_decode: Option<String>,
/// `lib` mode only: write the crate straight into `out` instead of
/// `<out>/<language>/`. Target-only (no `[generate]` default, no flag/env)
/// — a shared `out` across several `lib` targets needs the per-language
Expand Down Expand Up @@ -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<bool>,
/// `--validate-on-decode`.
pub validate_on_decode: Option<&'a str>,
}

/// A fully-resolved target: every field decided, `out` made absolute.
Expand All @@ -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.
Expand Down Expand Up @@ -343,6 +353,7 @@ pub fn resolve(
package_versions: None,
default_framing: None,
external_std: None,
validate_on_decode: None,
flatten: false,
}]
} else {
Expand Down Expand Up @@ -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 <lang>` \
when more than one target is configured ({} here)",
"`--out` / `--layout` / `--mode` / `--external-std` / `--validate-on-decode` \
need `--target <lang>` when more than one target is configured ({} here)",
selected.len()
));
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -485,6 +515,7 @@ pub fn resolve(
default_framing,
flatten: t.flatten,
external_std,
validate_on_decode,
});
}

Expand Down Expand Up @@ -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`"
);
}
}
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fn run(cli: Cli) -> Result<()> {
mode,
external_std,
no_external_std,
validate_on_decode,
dry_run,
list,
watch,
Expand All @@ -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)
}
Expand Down
97 changes: 97 additions & 0 deletions tests/cli/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Account, Fault<AccountsRenameError>> {
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),
);
}
3 changes: 3 additions & 0 deletions tests/cli/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/validated_project/comline.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[generate]
validate_on_decode = "collect"
8 changes: 8 additions & 0 deletions tests/fixtures/validated_project/config.idp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
congregation validated_e2e
specification_version = 1

code_generation = {
languages = {
rust#1.70.0 = {}
}
}
Loading
Loading