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
37 changes: 37 additions & 0 deletions FEATURES.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generator features

<!-- Generated by `comline-conformance` (`support_is_proven`) - don't edit by hand. Regenerate with `CONFORMANCE_BLESS=1 cargo test`. -->

What the rust generator implements, and the conformance fixtures that prove it. A claim is checked, not trusted: a supported feature has a fixture that generates and matches its golden, an ignored one generates exactly what it would without the feature, and an unsupported one is refused with an error naming every use.

| Feature | rust ≥ 1.85.0 |
|---|---|
| structs <br><sub>`structs`</sub> | ✅ supported <br><sub>proven by `primitives`, `optional_and_arrays`, `type_refs` +17 more</sub> |
| enums <br><sub>`enums`</sub> | ✅ supported <br><sub>proven by `enum_basic`</sub> |
| optional fields <br><sub>`optional-fields`</sub> | ✅ supported <br><sub>proven by `optional_and_arrays`, `type_refs`, `shapes` +4 more</sub> |
| arrays <br><sub>`arrays`</sub> | ✅ supported <br><sub>proven by `optional_and_arrays`, `type_refs`, `shapes` +5 more</sub> |
| fixed-length arrays <br><sub>`fixed-arrays`</sub> | ✅ supported <br><sub>proven by `shapes`</sub> |
| fixed arrays over 32 elements, nested <br><sub>`large-fixed-arrays-nested`</sub> | ❌ unsupported: a fixed array over 32 elements renders only as a struct or error field's own, non-optional type (serde's derive stops at 32) <br><sub>proven by `large_fixed_arrays_nested`</sub> |
| unions <br><sub>`unions`</sub> | ❌ unsupported: Rust has no anonymous sum type to render one as <br><sub>proven by `unions`</sub> |
| type aliases <br><sub>`type-aliases`</sub> | ✅ supported <br><sub>proven by `validators`, `validators_on_decode`, `bounds`</sub> |
| constants <br><sub>`constants`</sub> | ✅ supported <br><sub>proven by `constant`, `shapes`, `validators` +1 more</sub> |
| protocols <br><sub>`protocols`</sub> | ✅ supported <br><sub>proven by `protocol`, `error_shapes`, `validators` +15 more</sub> |
| errors <br><sub>`errors`</sub> | ✅ supported <br><sub>proven by `protocol`, `error_shapes`, `lockstep` +2 more</sub> |
| functions that throw <br><sub>`throws`</sub> | ✅ supported <br><sub>proven by `protocol`, `error_shapes`, `lockstep` +2 more</sub> |
| validators <br><sub>`validators`</sub> | ✅ supported <br><sub>proven by `validators`, `validators_on_decode`</sub> |
| imports between schemas <br><sub>`imports`</sub> | ✅ supported <br><sub>proven by `cross_schema`, `import_alias`, `import_glob` +6 more</sub> |
| nested namespaces <br><sub>`nested-namespaces`</sub> | ✅ supported <br><sub>proven by `nested_namespace`, `external_std`, `validators` +4 more</sub> |
| `lib` mode <br><sub>`lib-mode`</sub> | ✅ supported <br><sub>proven by `nested_namespace`, `external_std`</sub> |
| `external_std` <br><sub>`external-std`</sub> | ✅ supported <br><sub>proven by `external_std`</sub> |
| `validate_on_decode` <br><sub>`validate-on-decode`</sub> | ✅ supported <br><sub>proven by `validators_on_decode`</sub> |
| JSON-RPC framing <br><sub>`framing-jsonrpc`</sub> | ✅ supported <br><sub>proven by `framing_jsonrpc`, `connections_jsonrpc`</sub> |
| no handshake <br><sub>`handshake-none`</sub> | ✅ supported <br><sub>proven by `handshake_none`</sub> |
| `datagram@3` framing <br><sub>`framing-datagram3`</sub> | ✅ supported <br><sub>proven by `framing_datagram3`, `handshake_comline2`, `connections_default` +3 more</sub> |
| `comline@2` handshake <br><sub>`handshake-comline2`</sub> | ✅ supported <br><sub>proven by `handshake_comline2`, `connections_default`, `dependency_protocol` +2 more</sub> |
| connections <br><sub>`connections`</sub> | ✅ supported <br><sub>proven by `protocol`, `error_shapes`, `validators` +13 more</sub> |
| `async` <br><sub>`async`</sub> | ✅ supported <br><sub>proven by `connections_async`</sub> |
| the `packed@1` format <br><sub>`format-packed`</sub> | ✅ supported <br><sub>proven by `protocol`, `error_shapes`, `validators` +11 more</sub> |
| the `json@1` format <br><sub>`format-json`</sub> | ✅ supported <br><sub>proven by `format_json`</sub> |
| the `msgpack@1` format <br><sub>`format-msgpack`</sub> | ✅ supported <br><sub>proven by `format_msgpack`</sub> |
| bounds <br><sub>`bounds`</sub> | ✅ supported <br><sub>proven by `bounds`</sub> |
| single characters <br><sub>`chars`</sub> | ✅ supported <br><sub>proven by `chars`</sub> |
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ rewrite, not a port.
## Features

What this generator implements, feature by feature. Each claim is checked by
the conformance suite in [`ComlineProject/generation`](https://github.com/ComlineProject/generation):
`codegen/tests/conformance.rs`, which runs the conformance corpus of
[`ComlineProject/generation`](https://github.com/ComlineProject/generation)
over this generator, against the goldens in `codegen/tests/golden/`:
a supported feature has a fixture that generates and matches its golden output,
an ignored one generates exactly what it would without the feature, and an
unsupported one is refused with an error naming every use. The full matrix,
with the fixtures that prove each claim, is in
[`FEATURES.md`](https://github.com/ComlineProject/generation/blob/master/FEATURES.md).
with the fixtures that prove each claim, is in [`FEATURES.md`](FEATURES.md)
(also as [`features.json`](features.json)), which that run writes.

<!-- features:start -->
<!-- Generated from `SUPPORT` in `codegen/src/lib.rs` - don't edit by hand. -->
Expand Down
11 changes: 6 additions & 5 deletions codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "GPL-3.0-only"
# repository structure — no crates.io publishing at this stage). Pinned to the
# same generation rev `comline-typescript` uses, so a CLI build has one
# `comline-codegen`. Local iteration: add a `[patch]` onto a sibling checkout.
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "e900919375429591d73c906380f1d354d636ab22" }
comline-codegen = { git = "https://github.com/ComlineProject/generation", rev = "31e808ff05632acbfa4f48d208b8d5a2fbca3b0e" }
comline-core = { git = "https://github.com/ComlineProject/core", rev = "69039e7ab05953760bb7a6818f61e98c14b43922", default-features = false }

eyre = "0.6.8"
Expand All @@ -21,9 +21,10 @@ name = "comline-gen-rust"
path = "src/bin/comline-gen-rust.rs"

[dev-dependencies]
# The conformance corpus: every fixture is a whole package. Same generation
# rev as `comline-codegen` above, so the corpus and the generator share one
# `comline-codegen`.
comline-conformance = { git = "https://github.com/ComlineProject/generation", rev = "e900919375429591d73c906380f1d354d636ab22" }
# The conformance corpus and the harness that runs this generator over it
# (`tests/conformance.rs`, against `tests/golden/`): every fixture is a whole
# package. Same generation rev as `comline-codegen` above, so the corpus and
# the generator share one `comline-codegen`.
comline-conformance = { git = "https://github.com/ComlineProject/generation", rev = "31e808ff05632acbfa4f48d208b8d5a2fbca3b0e" }
# The tests compile schemas, so they need core's compiler.
comline-core = { git = "https://github.com/ComlineProject/core", rev = "69039e7ab05953760bb7a6818f61e98c14b43922" }
46 changes: 46 additions & 0 deletions codegen/tests/conformance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//! The conformance corpus (`comline-conformance`, in ComlineProject/generation)
//! run over this generator: its output for every fixture, at each of its
//! version boundaries, against the goldens under `tests/golden/`; the
//! generator protocol round trip; and the proof of every claim in `SUPPORT`,
//! with the report `FEATURES.md` and `features.json` at the repository root.
//! Regenerate the goldens and the report with:
//!
//! ```text
//! CONFORMANCE_BLESS=1 cargo test -p comline-codegen-rust --test conformance
//! ```
//!
//! A blessed golden is a snapshot of what the generator does now: a diff in
//! one is a change to what it emits, to be read as one.

use std::path::Path;

use comline_conformance::harness::{self, Generator};

const GENERATOR: Generator = Generator {
language: "rust",
generate: comline_codegen_rust::generate_rust,
register: comline_codegen_rust::register,
support: comline_codegen_rust::SUPPORT,
versions: comline_codegen_rust::VERSIONS,
};

#[test]
fn the_protocol_generates_what_the_generator_does() {
harness::the_protocol_generates_what_the_generator_does(&GENERATOR);
}

#[test]
fn corpus_matches_golden() {
harness::corpus_matches_golden(
&GENERATOR,
&Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/golden"),
);
}

#[test]
fn support_is_proven() {
harness::support_is_proven(
&GENERATOR,
&Path::new(env!("CARGO_MANIFEST_DIR")).join(".."),
);
}
188 changes: 188 additions & 0 deletions codegen/tests/golden/bounds/rust/conformance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// This file uses `comline_runtime` - add it to your Cargo.toml:
// comline-runtime = { git = "https://github.com/ComlineProject/runtime", rev = "3b9cc9dc53a83fb2830d91b267bd5f477f0a1e51" }
// Generated by Comline
// For Rust 1.85.0 or later; verified up to 1.99.
use serde::{Deserialize, Serialize};

pub type Name = String;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Customer {
#[serde(
deserialize_with = "comline_runtime::bounds::decode::string::<_, 32>",
serialize_with = "comline_runtime::bounds::encode::string::<_, 32>"
)]
pub name: Name,
#[serde(with = "__comline_bounds::chars_2")]
pub country: String,
#[serde(default)]
#[serde(with = "__comline_bounds::option_of_chars_max_254")]
pub email: Option<String>,
#[serde(with = "__comline_bounds::items_max_8_of_chars_max_16")]
pub tags: Vec<String>,
#[serde(
deserialize_with = "comline_runtime::bounds::decode::vec::<_, _, 10>",
serialize_with = "comline_runtime::bounds::encode::vec::<_, _, 10>"
)]
pub scores: Vec<u32>,
}

#[derive(Debug, Clone)]
pub struct Node {
pub label: String,
pub children: Vec<Node>,
}

/// [`Node`]'s fields as serde reads and writes them; its own impls
/// count how deep it nests, at most 16 levels (`@max_depth`).
#[doc(hidden)]
#[allow(dead_code, non_camel_case_types)]
#[derive(Serialize, Deserialize)]
#[serde(remote = "Node")]
struct __NodeDepth {
#[serde(
deserialize_with = "comline_runtime::bounds::decode::string::<_, 64>",
serialize_with = "comline_runtime::bounds::encode::string::<_, 64>"
)]
pub label: String,
#[serde(
deserialize_with = "comline_runtime::bounds::decode::vec::<_, _, 4>",
serialize_with = "comline_runtime::bounds::encode::vec::<_, _, 4>"
)]
pub children: Vec<Node>,
}

impl __NodeDepth {
fn depth<R>(f: impl FnOnce(&mut comline_runtime::bounds::Depth) -> R) -> R {
::std::thread_local! {
static DEPTH: ::core::cell::Cell<comline_runtime::bounds::Depth> =
const { ::core::cell::Cell::new(comline_runtime::bounds::Depth::new(16)) };
}
DEPTH.with(|cell| {
let mut depth = cell.get();
let r = f(&mut depth);
cell.set(depth);
r
})
}
}

impl Serialize for Node {
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
__NodeDepth::depth(|d| d.enter()).map_err(comline_runtime::bounds::ser_error)?;
let r = __NodeDepth::serialize(self, s);
__NodeDepth::depth(|d| d.leave());
r
}
}

impl<'de> Deserialize<'de> for Node {
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
__NodeDepth::depth(|d| d.enter()).map_err(comline_runtime::bounds::de_error)?;
let r = __NodeDepth::deserialize(d);
__NodeDepth::depth(|d| d.leave());
r
}
}

/// How this file's bounded values decode and encode (core#98): each
/// module is a `#[serde(with)]` for one shape of bounds.
#[doc(hidden)]
#[allow(dead_code, unused_imports)]
mod __comline_bounds {
/// A string of exactly `.0` characters: more is refused before
/// it's allocated, as a maximum is.
#[derive(Clone, Copy)]
pub struct Exact(pub u32);

impl<'de> serde::de::DeserializeSeed<'de> for Exact {
type Value = String;

fn deserialize<D: serde::Deserializer<'de>>(self, d: D) -> Result<String, D::Error> {
let s: String = serde::de::DeserializeSeed::deserialize(comline_runtime::bounds::CharsSeed::<String>::new(self.0), d)?;
if s.chars().count() != self.0 as usize {
return Err(serde::de::Error::custom(::core::format_args!("a string of exactly {} characters", self.0)));
}
Ok(s)
}
}

/// An optional value, decoded through the seed `.0` when present.
#[derive(Clone, Copy)]
pub struct Opt<S>(pub S);

impl<'de, S: serde::de::DeserializeSeed<'de>> serde::de::DeserializeSeed<'de> for Opt<S> {
type Value = Option<S::Value>;

fn deserialize<D: serde::Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
d.deserialize_option(self)
}
}

impl<'de, S: serde::de::DeserializeSeed<'de>> serde::de::Visitor<'de> for Opt<S> {
type Value = Option<S::Value>;

fn expecting(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
f.write_str("an optional value")
}

fn visit_none<E: serde::de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}

fn visit_unit<E: serde::de::Error>(self) -> Result<Self::Value, E> {
Ok(None)
}

fn visit_some<D: serde::Deserializer<'de>>(self, d: D) -> Result<Self::Value, D::Error> {
self.0.deserialize(d).map(Some)
}
}

pub mod chars_2 {
use super::*;

pub fn deserialize<'de, D: serde::Deserializer<'de>>(d: D) -> Result<String, D::Error> {
serde::de::DeserializeSeed::deserialize(Exact(2), d)
}

pub fn serialize<S: serde::Serializer>(value: &String, s: S) -> Result<S::Ok, S::Error> {
if value.chars().count() != 2 {
return Err(serde::ser::Error::custom("a string of exactly 2 characters"));
}
serde::Serialize::serialize(value, s)
}
}

pub mod items_max_8_of_chars_max_16 {
use super::*;

pub fn deserialize<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Vec<String>, D::Error> {
serde::de::DeserializeSeed::deserialize(comline_runtime::bounds::ItemsSeed::<_, Vec<String>>::new(comline_runtime::bounds::CharsSeed::<String>::new(16), 8), d)
}

pub fn serialize<S: serde::Serializer>(value: &Vec<String>, s: S) -> Result<S::Ok, S::Error> {
comline_runtime::bounds::check_items(value.len(), 8).map_err(comline_runtime::bounds::ser_error)?;
for item12 in value.iter() {
comline_runtime::bounds::check_chars(item12, 16).map_err(comline_runtime::bounds::ser_error)?;
}
serde::Serialize::serialize(value, s)
}
}

pub mod option_of_chars_max_254 {
use super::*;

pub fn deserialize<'de, D: serde::Deserializer<'de>>(d: D) -> Result<Option<String>, D::Error> {
serde::de::DeserializeSeed::deserialize(Opt(comline_runtime::bounds::CharsSeed::<String>::new(254)), d)
}

pub fn serialize<S: serde::Serializer>(value: &Option<String>, s: S) -> Result<S::Ok, S::Error> {
if let Some(inner12) = value {
comline_runtime::bounds::check_chars(inner12, 254).map_err(comline_runtime::bounds::ser_error)?;
}
serde::Serialize::serialize(value, s)
}
}
}

9 changes: 9 additions & 0 deletions codegen/tests/golden/chars/rust/conformance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated by Comline
// For Rust 1.85.0 or later; verified up to 1.99.
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Grade {
pub letter: char,
}

Loading
Loading