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 @@ -14,5 +14,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 = "a70cafcdd2015686ff1dbfba59b2b56ff70b730d" }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "7f2ddd3ed97c412389e0a0bf06420774b1a659d3" }
comline-codegen-rust = { git = "https://github.com/ComlineProject/comline-rust", rev = "7a9914d5aa08d74e72949df65b8c8bf5e235cc19" }
comline-codegen-typescript = { git = "https://github.com/ComlineProject/comline-typescript", rev = "86fc4eb586529ef92041f8dd3c46ff9340103411" }
2 changes: 1 addition & 1 deletion conformance/tests/golden/constant/rust.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

2 changes: 1 addition & 1 deletion conformance/tests/golden/enum_basic/rust.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum State {
Expand Down
6 changes: 3 additions & 3 deletions conformance/tests/golden/optional_and_arrays/rust.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bag {
pub required: String,
pub maybe: u32,
pub maybe: Option<u32>,
pub many: Vec<String>,
pub maybe_many: Vec<u32>,
pub maybe_many: Option<Vec<u32>>,
}

2 changes: 1 addition & 1 deletion conformance/tests/golden/primitives/rust.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Scalars {
Expand Down
147 changes: 143 additions & 4 deletions conformance/tests/golden/protocol/rust.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,148 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};
use comline_runtime::client::Client;
use comline_runtime::contract::{
BufMut, CallError, Dispatch, Envelope, Kind, RuntimeError, WireFormat,
};
use comline_runtime::transport::Transport;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NotFound {
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceLookupParams {
pub id: i32,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceNotifyParams {
pub code: u16,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ServiceLookupError {
NotFound(NotFound),
}

#[derive(Debug, Clone, PartialEq)]
pub enum ServiceCountError {
}

#[derive(Debug, Clone, PartialEq)]
pub enum ServiceNotifyError {
}

#[derive(Debug, Clone, PartialEq)]
pub enum ServiceError {
NotFound(NotFound),
}

impl From<ServiceLookupError> for ServiceError {
fn from(e: ServiceLookupError) -> Self {
match e {
ServiceLookupError::NotFound(x) => ServiceError::NotFound(x),
}
}
}

pub trait Service {
fn lookup(id: i32) -> Record;
fn count() -> u64;
fn notify(code: u16);
fn lookup(&self, id: i32) -> Result<Record, ServiceLookupError>;
fn count(&self) -> Result<u64, ServiceCountError>;
fn notify(&self, code: u16) -> Result<(), ServiceNotifyError>;
}

pub const SERVICE_CALLS: &[&str] = &["lookup", "count", "notify"];

pub struct ServiceDispatcher<S>(pub S);

impl<S: Service> Dispatch for ServiceDispatcher<S> {
fn dispatch<W: WireFormat>(
&self,
call: Kind,
params: &[u8],
fmt: &W,
out: &mut dyn BufMut,
) -> 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) => {
let mut body = Vec::new();
fmt.encode(&reply, &mut body)?;
Envelope::encode_ok(&body, out);
}
Err(ServiceLookupError::NotFound(e)) => {
let mut body = Vec::new();
fmt.encode(&e, &mut body)?;
Envelope::encode_err(0u16, &body, out);
}
}
Ok(())
}
1 => {
let _: () = fmt.decode(params)?;
match self.0.count() {
Ok(reply) => {
let mut body = Vec::new();
fmt.encode(&reply, &mut body)?;
Envelope::encode_ok(&body, out);
}
Err(never) => match never {},
}
Ok(())
}
2 => {
let p: ServiceNotifyParams = fmt.decode(params)?;
match self.0.notify(p.code) {
Ok(reply) => {
let mut body = Vec::new();
fmt.encode(&reply, &mut body)?;
Envelope::encode_ok(&body, out);
}
Err(never) => match never {},
}
Ok(())
}
_ => Err(RuntimeError::UnknownCall),
}
}
}

pub struct ServiceClient<T, W>(pub Client<T, W>);

impl<T: Transport, W: WireFormat> ServiceClient<T, W> {
pub fn new(client: Client<T, W>) -> Self {
Self(client)
}

pub fn lookup(&mut self, id: i32) -> Result<Record, CallError<ServiceLookupError>> {
let (reply, fmt) = self.0.call(0u16, &ServiceLookupParams { id })?;
match reply {
Envelope::Ok(payload) => fmt.decode(payload).map_err(CallError::Runtime),
Envelope::Err { id: 0u16, body } => {
let e: NotFound = fmt.decode(body)?;
Err(CallError::App(ServiceLookupError::NotFound(e)))
}
Envelope::Err { id, .. } => Err(CallError::Runtime(RuntimeError::Remote { id })),
}
}

pub fn count(&mut self) -> Result<u64, CallError<ServiceCountError>> {
let (reply, fmt) = self.0.call(1u16, &())?;
match reply {
Envelope::Ok(payload) => fmt.decode(payload).map_err(CallError::Runtime),
Envelope::Err { id, .. } => Err(CallError::Runtime(RuntimeError::Remote { id })),
}
}

pub fn notify(&mut self, code: u16) -> Result<(), CallError<ServiceNotifyError>> {
let (reply, fmt) = self.0.call(2u16, &ServiceNotifyParams { code })?;
match reply {
Envelope::Ok(payload) => fmt.decode(payload).map_err(CallError::Runtime),
Envelope::Err { id, .. } => Err(CallError::Runtime(RuntimeError::Remote { id })),
}
}
}

4 changes: 2 additions & 2 deletions conformance/tests/golden/type_refs/rust.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Generated by Comline
use serde::{Serialize, Deserialize};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Inner {
Expand All @@ -9,7 +9,7 @@ pub struct Inner {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Outer {
pub one: Inner,
pub some: Inner,
pub some: Option<Inner>,
pub list: Vec<Inner>,
}

Loading