diff --git a/conformance/Cargo.toml b/conformance/Cargo.toml index f0d8fd8..bc38ab9 100644 --- a/conformance/Cargo.toml +++ b/conformance/Cargo.toml @@ -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" } diff --git a/conformance/tests/golden/constant/rust.rs b/conformance/tests/golden/constant/rust.rs index becdc6a..3aee84f 100644 --- a/conformance/tests/golden/constant/rust.rs +++ b/conformance/tests/golden/constant/rust.rs @@ -1,3 +1,3 @@ // Generated by Comline -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; diff --git a/conformance/tests/golden/enum_basic/rust.rs b/conformance/tests/golden/enum_basic/rust.rs index c9630c1..efcee0a 100644 --- a/conformance/tests/golden/enum_basic/rust.rs +++ b/conformance/tests/golden/enum_basic/rust.rs @@ -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 { diff --git a/conformance/tests/golden/optional_and_arrays/rust.rs b/conformance/tests/golden/optional_and_arrays/rust.rs index 542c0ae..fb2f833 100644 --- a/conformance/tests/golden/optional_and_arrays/rust.rs +++ b/conformance/tests/golden/optional_and_arrays/rust.rs @@ -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, pub many: Vec, - pub maybe_many: Vec, + pub maybe_many: Option>, } diff --git a/conformance/tests/golden/primitives/rust.rs b/conformance/tests/golden/primitives/rust.rs index 6618305..61cd4a4 100644 --- a/conformance/tests/golden/primitives/rust.rs +++ b/conformance/tests/golden/primitives/rust.rs @@ -1,5 +1,5 @@ // Generated by Comline -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Scalars { diff --git a/conformance/tests/golden/protocol/rust.rs b/conformance/tests/golden/protocol/rust.rs index 7c42830..3162b55 100644 --- a/conformance/tests/golden/protocol/rust.rs +++ b/conformance/tests/golden/protocol/rust.rs @@ -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 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; + fn count(&self) -> Result; + fn notify(&self, code: u16) -> Result<(), ServiceNotifyError>; +} + +pub const SERVICE_CALLS: &[&str] = &["lookup", "count", "notify"]; + +pub struct ServiceDispatcher(pub S); + +impl Dispatch for ServiceDispatcher { + fn dispatch( + &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(pub Client); + +impl ServiceClient { + pub fn new(client: Client) -> Self { + Self(client) + } + + pub fn lookup(&mut self, id: i32) -> Result> { + 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> { + 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> { + 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 })), + } + } } diff --git a/conformance/tests/golden/type_refs/rust.rs b/conformance/tests/golden/type_refs/rust.rs index 9a6cdc9..9e15571 100644 --- a/conformance/tests/golden/type_refs/rust.rs +++ b/conformance/tests/golden/type_refs/rust.rs @@ -1,5 +1,5 @@ // Generated by Comline -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Inner { @@ -9,7 +9,7 @@ pub struct Inner { #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Outer { pub one: Inner, - pub some: Inner, + pub some: Option, pub list: Vec, }