From 455d41d20742ae09d58eb5ba8cfadee1fdf8c6a2 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Sun, 25 Oct 2026 00:22:35 +0800 Subject: [PATCH] comline.toml: async, for connection protocols' async handlers and senders [generate] async = true (or per [[generate.target]]) hands the generator async_api: Rust then generates connection protocols for its AsyncConnection. Off by default; the scaffold's comline.toml lists it. Pins the async generators (codegen d88f731, comline-rust 4b21074, comline-typescript a8424c6). --- Cargo.lock | 6 ++--- Cargo.toml | 6 ++--- src/commands/generate.rs | 1 + src/commands/new.rs | 1 + src/gen_config.rs | 48 +++++++++++++++++++++++++++++++++++ tests/cli/generate.rs | 54 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 110 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index dff2b8a..c05f506 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -399,7 +399,7 @@ dependencies = [ [[package]] name = "comline-codegen" version = "0.1.0" -source = "git+https://github.com/ComlineProject/generation?rev=bbfadb25cd34f02974c69cc2f102e667a089f684#bbfadb25cd34f02974c69cc2f102e667a089f684" +source = "git+https://github.com/ComlineProject/generation?rev=d88f731f8211b80cec6a82272aa61aa5140a0775#d88f731f8211b80cec6a82272aa61aa5140a0775" dependencies = [ "comline-core", "eyre", @@ -409,7 +409,7 @@ dependencies = [ [[package]] name = "comline-codegen-rust" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-rust?rev=577bdb2910db8b3aedb6061f5436816836a08942#577bdb2910db8b3aedb6061f5436816836a08942" +source = "git+https://github.com/ComlineProject/comline-rust?rev=4b21074d56b005ed8500bcc31bcaf8aebec215e2#4b21074d56b005ed8500bcc31bcaf8aebec215e2" dependencies = [ "comline-codegen", "comline-core", @@ -419,7 +419,7 @@ dependencies = [ [[package]] name = "comline-codegen-typescript" version = "0.1.0" -source = "git+https://github.com/ComlineProject/comline-typescript?rev=c72d14efa93135f892c04c6546f690a8e65b6cc1#c72d14efa93135f892c04c6546f690a8e65b6cc1" +source = "git+https://github.com/ComlineProject/comline-typescript?rev=a8424c623170aa0fec6363513d354d0dba23c060#a8424c623170aa0fec6363513d354d0dba23c060" dependencies = [ "comline-codegen", "comline-core", diff --git a/Cargo.toml b/Cargo.toml index a16b212..8654179 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,9 +38,9 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "a3f9eef5 # `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 = "bbfadb25cd34f02974c69cc2f102e667a089f684" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "577bdb2910db8b3aedb6061f5436816836a08942", optional = true } -comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c72d14efa93135f892c04c6546f690a8e65b6cc1", optional = true } +comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "d88f731f8211b80cec6a82272aa61aa5140a0775" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "4b21074d56b005ed8500bcc31bcaf8aebec215e2", optional = true } +comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "a8424c623170aa0fec6363513d354d0dba23c060", optional = true } [features] # Which language generators are compiled in. Drop one to shed its whole diff --git a/src/commands/generate.rs b/src/commands/generate.rs index c7978ee..796c0c7 100644 --- a/src/commands/generate.rs +++ b/src/commands/generate.rs @@ -223,6 +223,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, + async_api: t.async_api, validate_on_decode: t.validate_on_decode, }; // Deliberately not `core_err`: a generator's errors are about diff --git a/src/commands/new.rs b/src/commands/new.rs index 5a20be8..cc6b67a 100644 --- a/src/commands/new.rs +++ b/src/commands/new.rs @@ -47,6 +47,7 @@ const COMLINE_TOML: &str = "\ # 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 +# async = false # protocols on a connection get async handlers and senders # # 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 87e6dc2..5b6bae1 100644 --- a/src/gen_config.rs +++ b/src/gen_config.rs @@ -118,6 +118,11 @@ pub struct Generate { /// default), `collect` (fail with every failure) or `first` (fail with /// the first). Shared by every target unless overridden. pub validate_on_decode: Option, + /// `true`: protocols on a connection get async handlers and senders + /// (Rust's `AsyncConnection`), shared by every target unless overridden. + /// A generator whose output is async anyway ignores it. Default `false`. + #[serde(rename = "async")] + pub async_api: Option, /// `[[generate.target]]` blocks. #[serde(rename = "target")] pub targets: Vec, @@ -141,6 +146,8 @@ pub struct Target { pub default_framing: Option, pub external_std: Option, pub validate_on_decode: Option, + #[serde(rename = "async")] + pub async_api: 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 @@ -222,6 +229,8 @@ pub struct ResolvedTarget { pub external_std: bool, /// See [`Generate::validate_on_decode`]. pub validate_on_decode: ValidateOnDecode, + /// See [`Generate::async_api`]. + pub async_api: bool, } /// Variables a `layout` template can reference. @@ -359,6 +368,7 @@ pub fn resolve( default_framing: None, external_std: None, validate_on_decode: None, + async_api: None, flatten: false, }] } else { @@ -538,6 +548,7 @@ pub fn resolve( flatten: t.flatten, external_std, validate_on_decode, + async_api: t.async_api.or(cfg.generate.async_api).unwrap_or(false), }); } @@ -867,6 +878,43 @@ mod tests { assert!(!resolved[0].flatten); } + #[test] + fn async_is_off_unless_asked_for_and_a_target_overrides_the_section() { + let declared = [DeclaredLang { + language: "rust".into(), + lang_version: "1.85.0".into(), + }]; + let off = resolve( + &parse("[generate]\n"), + &declared, + Path::new("/x"), + &Overrides::default(), + ) + .unwrap(); + assert!(!off[0].async_api); + + let on = resolve( + &parse("[generate]\nasync = true\n"), + &declared, + Path::new("/x"), + &Overrides::default(), + ) + .unwrap(); + assert!(on[0].async_api); + + let overridden = parse( + "[generate]\nasync = true\n\n[[generate.target]]\nlanguage = \"rust\"\nasync = false\n", + ); + let resolved = resolve( + &overridden, + &declared, + Path::new("/x"), + &Overrides::default(), + ) + .unwrap(); + assert!(!resolved[0].async_api); + } + #[test] fn external_std_defaults_to_false() { let cfg = parse("[generate]\n"); diff --git a/tests/cli/generate.rs b/tests/cli/generate.rs index e863291..690cbb7 100644 --- a/tests/cli/generate.rs +++ b/tests/cli/generate.rs @@ -778,3 +778,57 @@ fn an_ignored_feature_is_a_warning() { .stderr(predicate::str::contains("`typescript` ignores validators: the types are generated; the checks aren't run yet (core#96)")) .stderr(predicate::str::contains("used by schema 'accounts', type alias 'Handle'")); } + +/// `async = true` in `comline.toml`: a protocol on a connection is generated +/// for the async driver; without it, the blocking one. +#[test] +fn async_generates_connection_protocols_for_the_async_driver() { + let temp = tempfile::tempdir().unwrap(); + let project = fixture_project(temp.path()); + fs::write( + project.join("src/link.ids"), + "protocol Link {\n function send(n: u32) -> u32;\n}\n", + ) + .unwrap(); + // On a connection by the package default rather than `@framing`, which + // settings forbid by default. + fs::write( + project.join("comline.toml"), + "[generate]\ndefault_framing = \"datagram@2\"\n", + ) + .unwrap(); + let generate = |project: &std::path::Path| { + comline_cmd() + .current_dir(project) + .args(["generate", "--target", "rust"]) + .assert() + .success(); + fs::read_to_string(project.join("generated/rust/link.rs")).unwrap() + }; + + let blocking = generate(&project); + assert!( + blocking.contains("pub struct LinkDispatcher(pub S);"), + "{blocking}" + ); + assert!(!blocking.contains("Async"), "{blocking}"); + + fs::write( + project.join("comline.toml"), + "[generate]\ndefault_framing = \"datagram@2\"\nasync = true\n", + ) + .unwrap(); + let asynchronous = generate(&project); + assert!( + asynchronous.contains("pub struct LinkAsyncDispatcher(pub S);"), + "{asynchronous}" + ); + assert!( + asynchronous.contains("pub struct LinkAsyncSender<'p, P: AsyncPeer>(pub &'p P);"), + "{asynchronous}" + ); + assert!( + asynchronous.contains("features = [\"async\"]"), + "the code-mode note names the runtime feature" + ); +}