From 6435c819d5e7f9c2b5cf6d231ca926f2934d51fc Mon Sep 17 00:00:00 2001 From: Roux Date: Sat, 29 Apr 2023 18:14:52 -0400 Subject: [PATCH] Init workspaces --- Cargo.lock | 12 +- Cargo.toml | 13 +- crates/maple-rpc/Cargo.toml | 13 + crates/maple-rpc/src/build.rs | 33 ++ crates/maple-rpc/src/lib.rs | 2 + crates/maple-rpc/src/schema/.gitignore | 1 + crates/maple-rpc/src/schema/protocol.capnp | 33 ++ src/build.rs | 10 - src/main.rs | 16 +- src/schema/protocol.capnp | 12 - src/schema/protocol_capnp.rs | 481 --------------------- 11 files changed, 114 insertions(+), 512 deletions(-) create mode 100644 crates/maple-rpc/Cargo.toml create mode 100644 crates/maple-rpc/src/build.rs create mode 100644 crates/maple-rpc/src/lib.rs create mode 100644 crates/maple-rpc/src/schema/.gitignore create mode 100644 crates/maple-rpc/src/schema/protocol.capnp delete mode 100644 src/build.rs delete mode 100644 src/schema/protocol.capnp delete mode 100644 src/schema/protocol_capnp.rs diff --git a/Cargo.lock b/Cargo.lock index f01f6b6..89b10bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,9 +10,9 @@ checksum = "18eca1b9aca7e3623dda158213b1b79c1e5c4293fbbf0ead6b9ed28a1763bbff" [[package]] name = "capnpc" -version = "0.16.3" +version = "0.16.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b45a0c0b3fec59ba52fc3bb90ba34cb6444e20691faa07be1dabb80660e4ad0" +checksum = "1bee1608054cd103343a6ba6d02488483b8a5060cdc926d16d81c1d2e2e4ecc4" dependencies = [ "capnp", ] @@ -20,6 +20,14 @@ dependencies = [ [[package]] name = "maple-chat" version = "0.1.0" +dependencies = [ + "capnp", + "maple-rpc", +] + +[[package]] +name = "maple-rpc" +version = "0.1.0" dependencies = [ "capnp", "capnpc", diff --git a/Cargo.toml b/Cargo.toml index f327d1c..8ff8777 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,14 +1,17 @@ +[workspace] +resolver = "2" +members = ["crates/maple-rpc"] + +[workspace.dependencies] +maple-rpc = {path = "crates/maple-rpc"} + [package] name = "maple-chat" version = "0.1.0" edition = "2021" -build = "src/build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] capnp = "0.16" - -[build-dependencies] -capnpc = "0.16" - +maple-rpc = {workspace = true} diff --git a/crates/maple-rpc/Cargo.toml b/crates/maple-rpc/Cargo.toml new file mode 100644 index 0000000..102604a --- /dev/null +++ b/crates/maple-rpc/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "maple-rpc" +version = "0.1.0" +edition = "2021" +build = "src/build.rs" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +capnp = "0.16" + +[build-dependencies] +capnpc = "0.16" diff --git a/crates/maple-rpc/src/build.rs b/crates/maple-rpc/src/build.rs new file mode 100644 index 0000000..b232ab1 --- /dev/null +++ b/crates/maple-rpc/src/build.rs @@ -0,0 +1,33 @@ +//Maple Chat, A simple chat app made with cap'n proto +//Copyright (C) 2023 Roux Pupo +// +//This program is free software: you can redistribute it and/or modify +//it under the terms of the GNU Affero General Public License as published by +//the Free Software Foundation, either version 3 of the License, or +//(at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//GNU Affero General Public License for more details. +// +//You should have received a copy of the GNU Affero General Public License +//along with this program. If not, see . + +use capnpc; +use std::process::Command; + +fn main() { + capnpc::CompilerCommand::new() + .output_path("src/schema") + .src_prefix("src/schema") + .file("src/schema/protocol.capnp") + .run() + .unwrap(); + let result = Command::new("rustfmt") + .arg("src/schema/protocol.capnp") + .output() + .expect("rustfmt failure"); + println!("{}", String::from_utf8_lossy(&result.stdout)); + eprintln!("{}", String::from_utf8_lossy(&result.stderr)); +} diff --git a/crates/maple-rpc/src/lib.rs b/crates/maple-rpc/src/lib.rs new file mode 100644 index 0000000..d8098ac --- /dev/null +++ b/crates/maple-rpc/src/lib.rs @@ -0,0 +1,2 @@ +#[path = "./schema/protocol_capnp.rs"] +pub mod protocol_capnp; \ No newline at end of file diff --git a/crates/maple-rpc/src/schema/.gitignore b/crates/maple-rpc/src/schema/.gitignore new file mode 100644 index 0000000..6f5f3d1 --- /dev/null +++ b/crates/maple-rpc/src/schema/.gitignore @@ -0,0 +1 @@ +*.rs diff --git a/crates/maple-rpc/src/schema/protocol.capnp b/crates/maple-rpc/src/schema/protocol.capnp new file mode 100644 index 0000000..0fcebc4 --- /dev/null +++ b/crates/maple-rpc/src/schema/protocol.capnp @@ -0,0 +1,33 @@ +# Maple Chat, A simple chat app made with cap'n proto +# Copyright (C) 2023 Roux Pupo +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +@0xd93f69a809989ffc; + +using UserId = UInt32; +using MessageId = UInt32; + +struct User { + name @0 :Text; + age @1 :UInt8; + userId @2 :UserId; +} + +struct Message { + sender @0: User; + reciever @1: List(UserId); + content @2: Text; + time @3: Int64; + messageId @4: MessageId; +} diff --git a/src/build.rs b/src/build.rs deleted file mode 100644 index c1d72aa..0000000 --- a/src/build.rs +++ /dev/null @@ -1,10 +0,0 @@ -extern crate capnpc; - -fn main() { - capnpc::CompilerCommand::new() - .output_path("src/schema/") - .src_prefix("src/schema/") - .file("src/schema/protocol.capnp") - .run() - .unwrap(); -} diff --git a/src/main.rs b/src/main.rs index 54ad54a..570537a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,3 @@ -use capnp::any_pointer::Builder; //Maple Chat, A simple chat app made with cap'n proto //Copyright (C) 2023 Roux Pupo // @@ -14,8 +13,21 @@ use capnp::any_pointer::Builder; // //You should have received a copy of the GNU Affero General Public License //along with this program. If not, see . -use capnp::*; +use capnp::message::{Builder, ReaderOptions}; +use capnp::serialize; +use maple_rpc::protocol_capnp::*; fn main() { + let mut message = Builder::new_default(); + let mut person = message.init_root::(); + person.set_name("Mocha"); + person.set_age(19); + + let data = serialize::write_message_to_words(&message); + + let reader = serialize::read_message(data.as_slice(), ReaderOptions::new()).unwrap(); + let retrieved = reader.get_root::().unwrap(); + println!("{:?}", retrieved.get_name()); } + diff --git a/src/schema/protocol.capnp b/src/schema/protocol.capnp deleted file mode 100644 index 2fb5f6d..0000000 --- a/src/schema/protocol.capnp +++ /dev/null @@ -1,12 +0,0 @@ -@0xd93f69a809989ffc; -struct User { - name @0 :Text; - age @1 :UInt8; -} - -struct Message { - sender @0: User; - reciever @1: List(User); - content @2: Text; - time @3: Int64; -} diff --git a/src/schema/protocol_capnp.rs b/src/schema/protocol_capnp.rs deleted file mode 100644 index 4b6cf1d..0000000 --- a/src/schema/protocol_capnp.rs +++ /dev/null @@ -1,481 +0,0 @@ -// @generated by the capnpc-rust plugin to the Cap'n Proto schema compiler. -// DO NOT EDIT. -// source: protocol.capnp - -pub mod user { - #[derive(Copy, Clone)] - pub struct Owned(()); - impl ::capnp::traits::Owned for Owned { - type Reader<'a> = Reader<'a>; - type Builder<'a> = Builder<'a>; - } - impl ::capnp::traits::OwnedStruct for Owned { - type Reader<'a> = Reader<'a>; - type Builder<'a> = Builder<'a>; - } - impl ::capnp::traits::Pipelined for Owned { - type Pipeline = Pipeline; - } - - #[derive(Clone, Copy)] - pub struct Reader<'a> { - reader: ::capnp::private::layout::StructReader<'a>, - } - - impl<'a> ::capnp::traits::HasTypeId for Reader<'a> { - const TYPE_ID: u64 = _private::TYPE_ID; - } - impl<'a> ::core::convert::From<::capnp::private::layout::StructReader<'a>> for Reader<'a> { - fn from(reader: ::capnp::private::layout::StructReader<'a>) -> Self { - Self { reader } - } - } - - impl<'a> ::capnp::traits::FromPointerReader<'a> for Reader<'a> { - fn get_from_pointer( - reader: &::capnp::private::layout::PointerReader<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result { - ::core::result::Result::Ok(reader.get_struct(default)?.into()) - } - } - - impl<'a> ::capnp::traits::IntoInternalStructReader<'a> for Reader<'a> { - fn into_internal_struct_reader(self) -> ::capnp::private::layout::StructReader<'a> { - self.reader - } - } - - impl<'a> ::capnp::traits::Imbue<'a> for Reader<'a> { - fn imbue(&mut self, cap_table: &'a ::capnp::private::layout::CapTable) { - self.reader - .imbue(::capnp::private::layout::CapTableReader::Plain(cap_table)) - } - } - - impl<'a> Reader<'a> { - pub fn reborrow(&self) -> Reader<'_> { - Self { ..*self } - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.reader.total_size() - } - #[inline] - pub fn get_name(self) -> ::capnp::Result<::capnp::text::Reader<'a>> { - ::capnp::traits::FromPointerReader::get_from_pointer( - &self.reader.get_pointer_field(0), - ::core::option::Option::None, - ) - } - #[inline] - pub fn has_name(&self) -> bool { - !self.reader.get_pointer_field(0).is_null() - } - #[inline] - pub fn get_age(self) -> u8 { - self.reader.get_data_field::(0) - } - } - - pub struct Builder<'a> { - builder: ::capnp::private::layout::StructBuilder<'a>, - } - impl<'a> ::capnp::traits::HasStructSize for Builder<'a> { - const STRUCT_SIZE: ::capnp::private::layout::StructSize = - ::capnp::private::layout::StructSize { - data: 1, - pointers: 1, - }; - } - impl<'a> ::capnp::traits::HasTypeId for Builder<'a> { - const TYPE_ID: u64 = _private::TYPE_ID; - } - impl<'a> ::core::convert::From<::capnp::private::layout::StructBuilder<'a>> for Builder<'a> { - fn from(builder: ::capnp::private::layout::StructBuilder<'a>) -> Self { - Self { builder } - } - } - - impl<'a> ::capnp::traits::ImbueMut<'a> for Builder<'a> { - fn imbue_mut(&mut self, cap_table: &'a mut ::capnp::private::layout::CapTable) { - self.builder - .imbue(::capnp::private::layout::CapTableBuilder::Plain(cap_table)) - } - } - - impl<'a> ::capnp::traits::FromPointerBuilder<'a> for Builder<'a> { - fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self { - builder - .init_struct(::STRUCT_SIZE) - .into() - } - fn get_from_pointer( - builder: ::capnp::private::layout::PointerBuilder<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result { - ::core::result::Result::Ok( - builder - .get_struct( - ::STRUCT_SIZE, - default, - )? - .into(), - ) - } - } - - impl<'a> ::capnp::traits::SetPointerBuilder for Reader<'a> { - fn set_pointer_builder( - mut pointer: ::capnp::private::layout::PointerBuilder<'_>, - value: Self, - canonicalize: bool, - ) -> ::capnp::Result<()> { - pointer.set_struct(&value.reader, canonicalize) - } - } - - impl<'a> Builder<'a> { - pub fn into_reader(self) -> Reader<'a> { - self.builder.into_reader().into() - } - pub fn reborrow(&mut self) -> Builder<'_> { - Builder { - builder: self.builder.reborrow(), - } - } - pub fn reborrow_as_reader(&self) -> Reader<'_> { - self.builder.as_reader().into() - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.builder.as_reader().total_size() - } - #[inline] - pub fn get_name(self) -> ::capnp::Result<::capnp::text::Builder<'a>> { - ::capnp::traits::FromPointerBuilder::get_from_pointer( - self.builder.get_pointer_field(0), - ::core::option::Option::None, - ) - } - #[inline] - pub fn set_name(&mut self, value: ::capnp::text::Reader<'_>) { - self.builder.reborrow().get_pointer_field(0).set_text(value); - } - #[inline] - pub fn init_name(self, size: u32) -> ::capnp::text::Builder<'a> { - self.builder.get_pointer_field(0).init_text(size) - } - #[inline] - pub fn has_name(&self) -> bool { - !self.builder.is_pointer_field_null(0) - } - #[inline] - pub fn get_age(self) -> u8 { - self.builder.get_data_field::(0) - } - #[inline] - pub fn set_age(&mut self, value: u8) { - self.builder.set_data_field::(0, value); - } - } - - pub struct Pipeline { - _typeless: ::capnp::any_pointer::Pipeline, - } - impl ::capnp::capability::FromTypelessPipeline for Pipeline { - fn new(typeless: ::capnp::any_pointer::Pipeline) -> Self { - Self { - _typeless: typeless, - } - } - } - impl Pipeline {} - mod _private { - pub const TYPE_ID: u64 = 0xaa1d_7a63_00c7_5ded; - } -} - -pub mod message { - #[derive(Copy, Clone)] - pub struct Owned(()); - impl ::capnp::traits::Owned for Owned { - type Reader<'a> = Reader<'a>; - type Builder<'a> = Builder<'a>; - } - impl ::capnp::traits::OwnedStruct for Owned { - type Reader<'a> = Reader<'a>; - type Builder<'a> = Builder<'a>; - } - impl ::capnp::traits::Pipelined for Owned { - type Pipeline = Pipeline; - } - - #[derive(Clone, Copy)] - pub struct Reader<'a> { - reader: ::capnp::private::layout::StructReader<'a>, - } - - impl<'a> ::capnp::traits::HasTypeId for Reader<'a> { - const TYPE_ID: u64 = _private::TYPE_ID; - } - impl<'a> ::core::convert::From<::capnp::private::layout::StructReader<'a>> for Reader<'a> { - fn from(reader: ::capnp::private::layout::StructReader<'a>) -> Self { - Self { reader } - } - } - - impl<'a> ::capnp::traits::FromPointerReader<'a> for Reader<'a> { - fn get_from_pointer( - reader: &::capnp::private::layout::PointerReader<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result { - ::core::result::Result::Ok(reader.get_struct(default)?.into()) - } - } - - impl<'a> ::capnp::traits::IntoInternalStructReader<'a> for Reader<'a> { - fn into_internal_struct_reader(self) -> ::capnp::private::layout::StructReader<'a> { - self.reader - } - } - - impl<'a> ::capnp::traits::Imbue<'a> for Reader<'a> { - fn imbue(&mut self, cap_table: &'a ::capnp::private::layout::CapTable) { - self.reader - .imbue(::capnp::private::layout::CapTableReader::Plain(cap_table)) - } - } - - impl<'a> Reader<'a> { - pub fn reborrow(&self) -> Reader<'_> { - Self { ..*self } - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.reader.total_size() - } - #[inline] - pub fn get_sender(self) -> ::capnp::Result> { - ::capnp::traits::FromPointerReader::get_from_pointer( - &self.reader.get_pointer_field(0), - ::core::option::Option::None, - ) - } - #[inline] - pub fn has_sender(&self) -> bool { - !self.reader.get_pointer_field(0).is_null() - } - #[inline] - pub fn get_reciever( - self, - ) -> ::capnp::Result<::capnp::struct_list::Reader<'a, crate::protocol_capnp::user::Owned>> - { - ::capnp::traits::FromPointerReader::get_from_pointer( - &self.reader.get_pointer_field(1), - ::core::option::Option::None, - ) - } - #[inline] - pub fn has_reciever(&self) -> bool { - !self.reader.get_pointer_field(1).is_null() - } - #[inline] - pub fn get_content(self) -> ::capnp::Result<::capnp::text::Reader<'a>> { - ::capnp::traits::FromPointerReader::get_from_pointer( - &self.reader.get_pointer_field(2), - ::core::option::Option::None, - ) - } - #[inline] - pub fn has_content(&self) -> bool { - !self.reader.get_pointer_field(2).is_null() - } - #[inline] - pub fn get_time(self) -> i64 { - self.reader.get_data_field::(0) - } - } - - pub struct Builder<'a> { - builder: ::capnp::private::layout::StructBuilder<'a>, - } - impl<'a> ::capnp::traits::HasStructSize for Builder<'a> { - const STRUCT_SIZE: ::capnp::private::layout::StructSize = - ::capnp::private::layout::StructSize { - data: 1, - pointers: 3, - }; - } - impl<'a> ::capnp::traits::HasTypeId for Builder<'a> { - const TYPE_ID: u64 = _private::TYPE_ID; - } - impl<'a> ::core::convert::From<::capnp::private::layout::StructBuilder<'a>> for Builder<'a> { - fn from(builder: ::capnp::private::layout::StructBuilder<'a>) -> Self { - Self { builder } - } - } - - impl<'a> ::capnp::traits::ImbueMut<'a> for Builder<'a> { - fn imbue_mut(&mut self, cap_table: &'a mut ::capnp::private::layout::CapTable) { - self.builder - .imbue(::capnp::private::layout::CapTableBuilder::Plain(cap_table)) - } - } - - impl<'a> ::capnp::traits::FromPointerBuilder<'a> for Builder<'a> { - fn init_pointer(builder: ::capnp::private::layout::PointerBuilder<'a>, _size: u32) -> Self { - builder - .init_struct(::STRUCT_SIZE) - .into() - } - fn get_from_pointer( - builder: ::capnp::private::layout::PointerBuilder<'a>, - default: ::core::option::Option<&'a [capnp::Word]>, - ) -> ::capnp::Result { - ::core::result::Result::Ok( - builder - .get_struct( - ::STRUCT_SIZE, - default, - )? - .into(), - ) - } - } - - impl<'a> ::capnp::traits::SetPointerBuilder for Reader<'a> { - fn set_pointer_builder( - mut pointer: ::capnp::private::layout::PointerBuilder<'_>, - value: Self, - canonicalize: bool, - ) -> ::capnp::Result<()> { - pointer.set_struct(&value.reader, canonicalize) - } - } - - impl<'a> Builder<'a> { - pub fn into_reader(self) -> Reader<'a> { - self.builder.into_reader().into() - } - pub fn reborrow(&mut self) -> Builder<'_> { - Builder { - builder: self.builder.reborrow(), - } - } - pub fn reborrow_as_reader(&self) -> Reader<'_> { - self.builder.as_reader().into() - } - - pub fn total_size(&self) -> ::capnp::Result<::capnp::MessageSize> { - self.builder.as_reader().total_size() - } - #[inline] - pub fn get_sender(self) -> ::capnp::Result> { - ::capnp::traits::FromPointerBuilder::get_from_pointer( - self.builder.get_pointer_field(0), - ::core::option::Option::None, - ) - } - #[inline] - pub fn set_sender( - &mut self, - value: crate::protocol_capnp::user::Reader<'_>, - ) -> ::capnp::Result<()> { - ::capnp::traits::SetPointerBuilder::set_pointer_builder( - self.builder.reborrow().get_pointer_field(0), - value, - false, - ) - } - #[inline] - pub fn init_sender(self) -> crate::protocol_capnp::user::Builder<'a> { - ::capnp::traits::FromPointerBuilder::init_pointer(self.builder.get_pointer_field(0), 0) - } - #[inline] - pub fn has_sender(&self) -> bool { - !self.builder.is_pointer_field_null(0) - } - #[inline] - pub fn get_reciever( - self, - ) -> ::capnp::Result<::capnp::struct_list::Builder<'a, crate::protocol_capnp::user::Owned>> - { - ::capnp::traits::FromPointerBuilder::get_from_pointer( - self.builder.get_pointer_field(1), - ::core::option::Option::None, - ) - } - #[inline] - pub fn set_reciever( - &mut self, - value: ::capnp::struct_list::Reader<'a, crate::protocol_capnp::user::Owned>, - ) -> ::capnp::Result<()> { - ::capnp::traits::SetPointerBuilder::set_pointer_builder( - self.builder.reborrow().get_pointer_field(1), - value, - false, - ) - } - #[inline] - pub fn init_reciever( - self, - size: u32, - ) -> ::capnp::struct_list::Builder<'a, crate::protocol_capnp::user::Owned> { - ::capnp::traits::FromPointerBuilder::init_pointer( - self.builder.get_pointer_field(1), - size, - ) - } - #[inline] - pub fn has_reciever(&self) -> bool { - !self.builder.is_pointer_field_null(1) - } - #[inline] - pub fn get_content(self) -> ::capnp::Result<::capnp::text::Builder<'a>> { - ::capnp::traits::FromPointerBuilder::get_from_pointer( - self.builder.get_pointer_field(2), - ::core::option::Option::None, - ) - } - #[inline] - pub fn set_content(&mut self, value: ::capnp::text::Reader<'_>) { - self.builder.reborrow().get_pointer_field(2).set_text(value); - } - #[inline] - pub fn init_content(self, size: u32) -> ::capnp::text::Builder<'a> { - self.builder.get_pointer_field(2).init_text(size) - } - #[inline] - pub fn has_content(&self) -> bool { - !self.builder.is_pointer_field_null(2) - } - #[inline] - pub fn get_time(self) -> i64 { - self.builder.get_data_field::(0) - } - #[inline] - pub fn set_time(&mut self, value: i64) { - self.builder.set_data_field::(0, value); - } - } - - pub struct Pipeline { - _typeless: ::capnp::any_pointer::Pipeline, - } - impl ::capnp::capability::FromTypelessPipeline for Pipeline { - fn new(typeless: ::capnp::any_pointer::Pipeline) -> Self { - Self { - _typeless: typeless, - } - } - } - impl Pipeline { - pub fn get_sender(&self) -> crate::protocol_capnp::user::Pipeline { - ::capnp::capability::FromTypelessPipeline::new(self._typeless.get_pointer_field(0)) - } - } - mod _private { - pub const TYPE_ID: u64 = 0xa8c3_12bc_6f70_3fa1; - } -}