From 37356e1277711020c21c2a87adfc6452cf694db0 Mon Sep 17 00:00:00 2001 From: Kinflou Date: Wed, 2 Sep 2026 05:11:18 +0800 Subject: [PATCH] chore(conformance): re-bless for framing codegen + handshake helpers Bumps comline-codegen-rust to comline-rust main and re-blesses. The protocol/rust.rs golden catches up across three merged codegen PRs it hadn't tracked: - #6 handshake: `pub const IR_HASH`, `Dispatcher::serve` / `Client::connect` helpers, Handshake / Server / FRAMING_DATAGRAM imports. - #8 framing: `fn calls()` on the dispatcher, `reply: &mut Reply` (was `out: &mut dyn BufMut`), `reply.ok()` / `reply.err()`, `Ok(value)` (was `Ok(reply)`), `Call::new(i, "name")` in the client stub. typescript golden unchanged. --- conformance/Cargo.toml | 2 +- conformance/tests/golden/protocol/rust.rs | 50 +++++++++++++++++------ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/conformance/Cargo.toml b/conformance/Cargo.toml index e56d2a3..0119384 100644 --- a/conformance/Cargo.toml +++ b/conformance/Cargo.toml @@ -15,5 +15,5 @@ comline-core = { git = "https://github.com/ComlineProject/core", rev = "47ac5f10 [dev-dependencies] comline-codegen = { path = "../codegen" } -comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "1191e76068eca75ea7c4148d58ab127bb005e81a" } +comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "ca7e9151b66d0f9d5050f140242eb454de41a4c4" } comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "86fc4eb586529ef92041f8dd3c46ff9340103411" } diff --git a/conformance/tests/golden/protocol/rust.rs b/conformance/tests/golden/protocol/rust.rs index 510a4a1..9fe1f6e 100644 --- a/conformance/tests/golden/protocol/rust.rs +++ b/conformance/tests/golden/protocol/rust.rs @@ -2,10 +2,16 @@ use serde::{Deserialize, Serialize}; use comline_runtime::client::Client; use comline_runtime::contract::{ - BufMut, CallError, Dispatch, Envelope, Kind, RuntimeError, WireFormat, + Call, CallError, Dispatch, Envelope, Handshake, Kind, Reply, RuntimeError, + WireFormat, FRAMING_DATAGRAM, }; +use comline_runtime::serve::Server; use comline_runtime::transport::Transport; +/// Fingerprint of the frozen IR this file was generated from — the two +/// ends of a connection [`Handshake`] must agree on it. +pub const IR_HASH: u64 = 0x30ddab86c2b70657; + #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct NotFound { } @@ -53,26 +59,30 @@ pub const SERVICE_CALLS: &[&str] = &["lookup", "count", "notify"]; pub struct ServiceDispatcher(pub S); impl Dispatch for ServiceDispatcher { + fn calls(&self) -> &'static [&'static str] { + SERVICE_CALLS + } + fn dispatch( &self, call: Kind, params: &[u8], fmt: &W, - out: &mut dyn BufMut, + reply: &mut Reply, ) -> Result<(), RuntimeError> { match call.resolve(SERVICE_CALLS).ok_or(RuntimeError::UnknownCall)? { 0 => { let p: ServiceLookupParams = fmt.decode(params)?; match self.0.lookup(p.id) { - Ok(reply) => { + Ok(value) => { let mut body = Vec::new(); - fmt.encode(&reply, &mut body)?; - Envelope::encode_ok(&body, out); + fmt.encode(&value, &mut body)?; + reply.ok(&body); } Err(ServiceLookupError::NotFound(e)) => { let mut body = Vec::new(); fmt.encode(&e, &mut body)?; - Envelope::encode_err(0u16, &body, out); + reply.err(0u16, &body); } } Ok(()) @@ -80,10 +90,10 @@ impl Dispatch for ServiceDispatcher { 1 => { let _: () = fmt.decode(params)?; match self.0.count() { - Ok(reply) => { + Ok(value) => { let mut body = Vec::new(); - fmt.encode(&reply, &mut body)?; - Envelope::encode_ok(&body, out); + fmt.encode(&value, &mut body)?; + reply.ok(&body); } Err(never) => match never {}, } @@ -99,6 +109,16 @@ impl Dispatch for ServiceDispatcher { } } +impl ServiceDispatcher { + /// Serve this protocol over `transport`, running the connection + /// handshake (`IR_HASH` + `format`'s name) against the peer first. + pub fn serve(self, transport: &mut T, format: W) + -> Result<(), RuntimeError> { + let hs = Handshake::new(IR_HASH, format.name(), FRAMING_DATAGRAM, 0); + Server::new(self, format).serve_handshaked(transport, hs) + } +} + pub struct ServiceClient(pub Client); impl ServiceClient { @@ -106,8 +126,14 @@ impl ServiceClient { Self(client) } + /// Bind + run the connection handshake against the provider. + pub fn connect(transport: T, format: W) -> Result { + let hs = Handshake::new(IR_HASH, format.name(), FRAMING_DATAGRAM, 0); + Ok(Self(Client::connect(transport, format, hs)?)) + } + pub fn lookup(&mut self, id: i32) -> Result> { - let (reply, fmt) = self.0.call(0u16, &ServiceLookupParams { id })?; + let (reply, fmt) = self.0.call(Call::new(0, "lookup"), &ServiceLookupParams { id })?; match reply { Envelope::Ok(payload) => fmt.decode(payload).map_err(CallError::Runtime), Envelope::Err { id: 0u16, body } => { @@ -119,7 +145,7 @@ impl ServiceClient { } pub fn count(&mut self) -> Result> { - let (reply, fmt) = self.0.call(1u16, &())?; + let (reply, fmt) = self.0.call(Call::new(1, "count"), &())?; match reply { Envelope::Ok(payload) => fmt.decode(payload).map_err(CallError::Runtime), Envelope::Err { id, .. } => Err(CallError::Runtime(RuntimeError::Remote { id })), @@ -127,7 +153,7 @@ impl ServiceClient { } pub fn notify(&mut self, code: u16) -> Result<(), RuntimeError> { - self.0.notify(2u16, &ServiceNotifyParams { code }) + self.0.notify(Call::new(2, "notify"), &ServiceNotifyParams { code }) } }