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
6 changes: 3 additions & 3 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/gen_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// `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<bool>,
/// `[[generate.target]]` blocks.
#[serde(rename = "target")]
pub targets: Vec<Target>,
Expand All @@ -141,6 +146,8 @@ pub struct Target {
pub default_framing: Option<String>,
pub external_std: Option<bool>,
pub validate_on_decode: Option<String>,
#[serde(rename = "async")]
pub async_api: Option<bool>,
/// `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 @@ -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.
Expand Down Expand Up @@ -359,6 +368,7 @@ pub fn resolve(
default_framing: None,
external_std: None,
validate_on_decode: None,
async_api: None,
flatten: false,
}]
} else {
Expand Down Expand Up @@ -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),
});
}

Expand Down Expand Up @@ -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");
Expand Down
54 changes: 54 additions & 0 deletions tests/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(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<S>(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"
);
}
Loading