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
4 changes: 2 additions & 2 deletions conformance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ comline-codegen = { path = "../codegen" }
[dev-dependencies]
eyre = "0.6.8"
serde_json = "1.0"
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "2d2c8853727929a7b350b3017c9ff86cb2ec8911" }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "c44b39d68035bd968cec0cd75afb6c6230c4ab76" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "dd1c29e6560a0ffd7893c59715b31d43b88c585f" }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "d377fbf07cf6c0f004e9d5dd4fbd6fa15ea9d0bc" }
19 changes: 12 additions & 7 deletions conformance/tests/golden/connections/rust/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ use comline_runtime::transport::Transport;
/// two ends of a connection must agree on it, and the handshake checks.
pub const IR_HASH: u64 = 0xb97e34b203ec1425;

/// Digest of every schema in the package — what a connection's handshake
/// checks, so both ends agree on each protocol's number.
pub const CONNECTION_IR_HASH: u64 = 0xb97e34b203ec1425;

/// The package's wire format (`wire.format`): what this file's helpers
/// encode with, and what the peer has to speak too.
pub type Format = comline_runtime::format::Packed;
#[doc(hidden)]
pub use crate::connection::{CONNECTION_IR_HASH, Format};

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatSendParams {
pub n: u32,
Expand All @@ -33,8 +29,10 @@ pub trait Chat {
fn send(&self, peer: &mut impl Peer, n: u32) -> Result<u32, Fault<ChatSendError>>;
}

#[doc(hidden)]
pub const CHAT_CALLS: &[&str] = &["send"];

#[doc(hidden)]
pub struct ChatDispatcher<S>(pub S);

impl<S: Chat> Dispatch for ChatDispatcher<S> {
Expand Down Expand Up @@ -89,6 +87,7 @@ impl<S: Chat> Dispatch for ChatDispatcher<S> {

impl<S: Chat> ChatDispatcher<S> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 0;

/// Provide this protocol on `conn`: the peer's calls to it come here.
Expand All @@ -109,6 +108,7 @@ pub struct ChatSender<'p, P: Peer>(pub &'p mut P);

impl<'p, P: Peer> ChatSender<'p, P> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 0;

pub fn new(peer: &'p mut P) -> Self {
Expand All @@ -125,6 +125,7 @@ impl<'p, P: Peer> ChatSender<'p, P> {
}
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventsShownParams {
pub n: u32,
Expand All @@ -139,8 +140,10 @@ pub trait Events {
fn ping(&self, peer: &mut impl Peer);
}

#[doc(hidden)]
pub const EVENTS_CALLS: &[&str] = &["shown", "ping"];

#[doc(hidden)]
pub struct EventsDispatcher<S>(pub S);

impl<S: Events> Dispatch for EventsDispatcher<S> {
Expand Down Expand Up @@ -203,6 +206,7 @@ impl<S: Events> Dispatch for EventsDispatcher<S> {

impl<S: Events> EventsDispatcher<S> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 1;

/// Provide this protocol on `conn`: the peer's calls to it come here.
Expand All @@ -223,6 +227,7 @@ pub struct EventsSender<'p, P: Peer>(pub &'p mut P);

impl<'p, P: Peer> EventsSender<'p, P> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 1;

pub fn new(peer: &'p mut P) -> Self {
Expand Down
28 changes: 23 additions & 5 deletions conformance/tests/golden/connections/rust/connection.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
// Generated by Comline
// For Rust 1.85.0 or later; verified up to 1.99.
//! The package's protocols share one connection: `datagram@2` framing, the `comline@1` handshake. To use it:
//!
//! - Implement the trait of each protocol the server provides: `conformance::Chat`.
//! - Implement the trait of each protocol the client provides: `conformance::Events`.
//! - Serve with [`serve`]`(transport, Services { chat: .. })`, which serves every call until the peer leaves. [`accept`] hands the connection back first, for a server that calls the client or serves one call at a time.
//! - Dial with [`connect`]`(transport, ClientServices { events: .. })`.
//! - Call the server from the dialling side: `ChatSender::new(&mut conn).send(..)`.
//! - Call the client from the accepting side, on the connection [`accept`] returns or the peer a handler is given: `EventsSender::new(&mut conn).shown(..)`.
//!
//! [`Connection<T>`](Connection) names the connection over a transport `T`, written `Connection<Tcp>`; [`Format`] is the package's wire format.

use comline_runtime::contract::RuntimeError;
use comline_runtime::transport::Transport;

/// The package's wire format (`wire.format`).
pub type Format = comline_runtime::format::Packed;

/// Digest of every schema in the package — what a connection's handshake
/// checks, so both ends agree on each protocol's number.
pub const CONNECTION_IR_HASH: u64 = 0xb97e34b203ec1425;

/// The package's connection over the transport `T`: what [`accept`] and [`connect`]
/// return, written `Connection<Tcp>`: [`Format`] over the default framing.
pub type Connection<T> = comline_runtime::connection::Connection<T, Format>;

/// What the accepting side provides: one field per protocol, so leaving one out
/// doesn't compile.
pub struct Services<Chat> {
Expand All @@ -15,7 +33,7 @@ pub struct Services<Chat> {

impl<Chat: crate::conformance::Chat + 'static> Services<Chat> {
/// Provide every protocol on `conn`: the peer's calls to them come here.
pub fn provide_on<T: comline_runtime::transport::Transport>(self, conn: &mut comline_runtime::connection::Connection<T, Format>) {
pub fn provide_on<T: Transport>(self, conn: &mut Connection<T>) {
crate::conformance::ChatDispatcher(self.chat).provide_on(conn);
}
}
Expand All @@ -28,7 +46,7 @@ pub struct ClientServices<Events> {

impl<Events: crate::conformance::Events + 'static> ClientServices<Events> {
/// Provide every protocol on `conn`: the peer's calls to them come here.
pub fn provide_on<T: comline_runtime::transport::Transport>(self, conn: &mut comline_runtime::connection::Connection<T, Format>) {
pub fn provide_on<T: Transport>(self, conn: &mut Connection<T>) {
crate::conformance::EventsDispatcher(self.events).provide_on(conn);
}
}
Expand All @@ -37,21 +55,21 @@ impl<Events: crate::conformance::Events + 'static> ClientServices<Events> {
/// every protocol in `services` on it. The connection comes back not yet
/// served, for a server that calls the client first, or serves one call at
/// a time.
pub fn accept<T: comline_runtime::transport::Transport, Chat: crate::conformance::Chat + 'static>(transport: T, services: Services<Chat>) -> Result<comline_runtime::connection::Connection<T, Format>, comline_runtime::contract::RuntimeError> {
pub fn accept<T: Transport, Chat: crate::conformance::Chat + 'static>(transport: T, services: Services<Chat>) -> Result<Connection<T>, RuntimeError> {
let mut conn = comline_runtime::connection::Connection::accept(transport, Format::default(), CONNECTION_IR_HASH)?;
services.provide_on(&mut conn);
Ok(conn)
}

/// [`accept`], then serve every call until the peer leaves.
pub fn serve<T: comline_runtime::transport::Transport, Chat: crate::conformance::Chat + 'static>(transport: T, services: Services<Chat>) -> Result<(), comline_runtime::contract::RuntimeError> {
pub fn serve<T: Transport, Chat: crate::conformance::Chat + 'static>(transport: T, services: Services<Chat>) -> Result<(), RuntimeError> {
accept(transport, services)?.serve()
}

/// Dial over `transport`: run the handshake, then provide every protocol in
/// `services`, which the server calls. Call the server's protocols through
/// their senders.
pub fn connect<T: comline_runtime::transport::Transport, Events: crate::conformance::Events + 'static>(transport: T, services: ClientServices<Events>) -> Result<comline_runtime::connection::Connection<T, Format>, comline_runtime::contract::RuntimeError> {
pub fn connect<T: Transport, Events: crate::conformance::Events + 'static>(transport: T, services: ClientServices<Events>) -> Result<Connection<T>, RuntimeError> {
let mut conn = comline_runtime::connection::Connection::connect(transport, Format::default(), CONNECTION_IR_HASH)?;
services.provide_on(&mut conn);
Ok(conn)
Expand Down
19 changes: 8 additions & 11 deletions conformance/tests/golden/connections/typescript/conformance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import {
RuntimeError,
resolveKind,
PackedCodec,
type Codec,
type Connection,
type Dispatch,
Expand All @@ -18,26 +17,21 @@ import {
* two ends of a connection must agree on it. */
export const IR_HASH = 0xb97e34b203ec1425n;

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0xb97e34b203ec1425n;

/** The package's wire format (`wire.format`): what this file's helpers
* encode with, and what the peer has to speak too. */
export const Format: Codec = new PackedCodec();

export interface ChatSendParams {
n: number;
}

/** @internal */
export const ChatSendParamsLayout: Layout = { struct: [["n", "u32"]] };

export interface Chat {
send(peer: Peer, params: ChatSendParams): Promise<number>;
}

/** @internal */
export const CHAT_CALLS = ["send"] as const;

/** @internal */
export class ChatDispatcher implements Dispatch {
/** This protocol's number on a connection. */
static readonly PROTOCOL = 0;
Expand Down Expand Up @@ -74,7 +68,7 @@ export class ChatDispatcher implements Dispatch {
}
}

/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.accept(transport, Format, CONNECTION_IR_HASH)`, or let the package's `serve` / `connect` do it. */
/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.accept(transport, Format, CONNECTION_IR_HASH)` (`Format` and `CONNECTION_IR_HASH` are in `./wire.js`), or let the package's `serve` / `connect` do it. */
provideOn(conn: Connection): void {
conn.provide(ChatDispatcher.PROTOCOL, this);
}
Expand Down Expand Up @@ -103,15 +97,18 @@ export interface EventsShownParams {
n: number;
}

/** @internal */
export const EventsShownParamsLayout: Layout = { struct: [["n", "u32"]] };

export interface Events {
shown(peer: Peer, params: EventsShownParams): Promise<boolean>;
ping(peer: Peer): Promise<void>;
}

/** @internal */
export const EVENTS_CALLS = ["shown", "ping"] as const;

/** @internal */
export class EventsDispatcher implements Dispatch {
/** This protocol's number on a connection. */
static readonly PROTOCOL = 1;
Expand Down Expand Up @@ -156,7 +153,7 @@ export class EventsDispatcher implements Dispatch {
}
}

/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.connect(transport, Format, CONNECTION_IR_HASH)`, or let the package's `serve` / `connect` do it. */
/** Provide this protocol on `conn`: the peer's calls to it come here. Open `conn` with `Connection.connect(transport, Format, CONNECTION_IR_HASH)` (`Format` and `CONNECTION_IR_HASH` are in `./wire.js`), or let the package's `serve` / `connect` do it. */
provideOn(conn: Connection): void {
conn.provide(EventsDispatcher.PROTOCOL, this);
}
Expand Down
33 changes: 23 additions & 10 deletions conformance/tests/golden/connections/typescript/connection.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
// Generated by Comline
// For TypeScript 5.0 or later; verified up to 5.9.

/**
* Where to start:
*
* - Implement what you provide: `Chat`, `Events` (in `./conformance.js`).
* - Serve with `serve(transport, { chat })`; `accept(transport, { chat })` takes the same and hands back the connection.
* - Dial with `connect(transport, { events })`.
* - Call the other end with `new ChatSender(conn).send(…)`, on the connection `connect` returns or the peer a handler is given.
*
* What is marked internal is wire plumbing: exported for hand-written use, and not what
* a user calls.
*/

import {
Connection,
PackedCodec,
type Codec,
type Transport,
} from "@comline/runtime";
import * as ns_conformance from "./conformance.js";
import { CONNECTION_IR_HASH, Format } from "./wire.js";

/** The package's wire format (`wire.format`). */
export const Format: Codec = new PackedCodec();

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0xb97e34b203ec1425n;
/** The package's wire format and the digest of every schema in it, which every file of the package shares. */
export { CONNECTION_IR_HASH, Format } from "./wire.js";

/** What the accepting side provides: one field per protocol, so leaving one out doesn't type-check. */
export interface Services {
chat: ns_conformance.Chat;
}

/** Provide every protocol in `services` on `conn`: the peer's calls to them come here. */
/**
* Provide every protocol in `services` on `conn`: the peer's calls to them come here.
* @internal
*/
export function provideServices(conn: Connection, services: Services): void {
new ns_conformance.ChatDispatcher(services.chat).provideOn(conn);
}
Expand All @@ -31,7 +41,10 @@ export interface ClientServices {
events: ns_conformance.Events;
}

/** Provide every protocol in `services` on `conn`: the peer's calls to them come here. */
/**
* Provide every protocol in `services` on `conn`: the peer's calls to them come here.
* @internal
*/
export function provideClientServices(conn: Connection, services: ClientServices): void {
new ns_conformance.EventsDispatcher(services.events).provideOn(conn);
}
Expand Down
14 changes: 14 additions & 0 deletions conformance/tests/golden/connections/typescript/wire.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated by Comline
// For TypeScript 5.0 or later; verified up to 5.9.

import {
PackedCodec,
type Codec,
} from "@comline/runtime";

/** The package's wire format (`wire.format`). */
export const Format: Codec = new PackedCodec();

/** Digest of every schema in the package — what a connection's handshake
* checks, so both ends agree on each protocol's number. */
export const CONNECTION_IR_HASH = 0xb97e34b203ec1425n;
19 changes: 12 additions & 7 deletions conformance/tests/golden/connections_async/rust/conformance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ use comline_runtime::contract::{ConnectionFraming, ProtocolNumber};
/// two ends of a connection must agree on it, and the handshake checks.
pub const IR_HASH: u64 = 0xb97e34b203ec1425;

/// Digest of every schema in the package — what a connection's handshake
/// checks, so both ends agree on each protocol's number.
pub const CONNECTION_IR_HASH: u64 = 0xb97e34b203ec1425;

/// The package's wire format (`wire.format`): what this file's helpers
/// encode with, and what the peer has to speak too.
pub type Format = comline_runtime::format::Packed;
#[doc(hidden)]
pub use crate::connection::{CONNECTION_IR_HASH, Format};

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatSendParams {
pub n: u32,
Expand All @@ -33,8 +29,10 @@ pub trait Chat: Send + Sync + 'static {
fn send(&self, peer: &impl AsyncPeer, n: u32) -> impl ::core::future::Future<Output = Result<u32, Fault<ChatSendError>>> + Send;
}

#[doc(hidden)]
pub const CHAT_CALLS: &[&str] = &["send"];

#[doc(hidden)]
pub struct ChatAsyncDispatcher<S>(pub S);

impl<S: Chat> AsyncDispatch for ChatAsyncDispatcher<S> {
Expand Down Expand Up @@ -71,6 +69,7 @@ impl<S: Chat> AsyncDispatch for ChatAsyncDispatcher<S> {

impl<S: Chat> ChatAsyncDispatcher<S> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 0;

/// Provide this protocol on `conn`: the peer's calls to it come here.
Expand All @@ -91,6 +90,7 @@ pub struct ChatAsyncSender<'p, P: AsyncPeer>(pub &'p P);

impl<'p, P: AsyncPeer> ChatAsyncSender<'p, P> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 0;

pub fn new(peer: &'p P) -> Self {
Expand All @@ -108,6 +108,7 @@ impl<'p, P: AsyncPeer> ChatAsyncSender<'p, P> {
}
}

#[doc(hidden)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EventsShownParams {
pub n: u32,
Expand All @@ -122,8 +123,10 @@ pub trait Events: Send + Sync + 'static {
fn ping(&self, peer: &impl AsyncPeer) -> impl ::core::future::Future<Output = ()> + Send;
}

#[doc(hidden)]
pub const EVENTS_CALLS: &[&str] = &["shown", "ping"];

#[doc(hidden)]
pub struct EventsAsyncDispatcher<S>(pub S);

impl<S: Events> AsyncDispatch for EventsAsyncDispatcher<S> {
Expand Down Expand Up @@ -168,6 +171,7 @@ impl<S: Events> AsyncDispatch for EventsAsyncDispatcher<S> {

impl<S: Events> EventsAsyncDispatcher<S> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 1;

/// Provide this protocol on `conn`: the peer's calls to it come here.
Expand All @@ -188,6 +192,7 @@ pub struct EventsAsyncSender<'p, P: AsyncPeer>(pub &'p P);

impl<'p, P: AsyncPeer> EventsAsyncSender<'p, P> {
/// This protocol's number on a connection.
#[doc(hidden)]
pub const PROTOCOL: ProtocolNumber = 1;

pub fn new(peer: &'p P) -> Self {
Expand Down
Loading
Loading