commit 149e5e64210330b034d7c76ecfaa5f556c0ba977
parent 5a487387cb7cb9e2905bcc3e12bb2b15f2e7b770
Author: MTRNord <mtrnord1@gmail.com>
Date: Sat, 16 Apr 2022 22:00:03 +0200
Move to cbor
Diffstat:
8 files changed, 149 insertions(+), 89 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -1,3 +1,4 @@
/target
-/data
+/data.cbor
+/data.capnpdata
/json_data
\ No newline at end of file
diff --git a/Cargo.lock b/Cargo.lock
@@ -56,21 +56,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
-name = "capnp"
-version = "0.14.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "21d5d7da973146f1720672faa44f1523cc8f923636190ca1a931c7bc8834de68"
-
-[[package]]
-name = "capnpc"
-version = "0.14.7"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c7ed9b80f792ac01a8b328ccbc509c2bd756fb5dec18af0163e7963dde23c0b5"
-dependencies = [
- "capnp",
-]
-
-[[package]]
name = "cc"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -165,6 +150,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4"
[[package]]
+name = "half"
+version = "1.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
+
+[[package]]
name = "hashbrown"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -220,6 +211,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
+name = "minicbor"
+version = "0.15.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2b419e66bd98ccf5824dd4b8f4141f7431d1d07733c3e13c946278204a437d2b"
+dependencies = [
+ "half",
+ "minicbor-derive",
+]
+
+[[package]]
+name = "minicbor-derive"
+version = "0.9.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "03c32aff53852dc6dd3817559d603734e4f7a65411702953238aafdcb3a7fd47"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -308,10 +320,9 @@ dependencies = [
name = "replayer-mx"
version = "0.1.0"
dependencies = [
- "capnp",
- "capnpc",
"clap",
"color-eyre",
+ "minicbor",
]
[[package]]
diff --git a/Cargo.toml b/Cargo.toml
@@ -6,9 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-capnp = "0.14"
+minicbor = { version = "0.15", features = ["derive", "partial-skip-support", "partial-derive-support", "std", "alloc", "half"] }
color-eyre = "0.6"
-clap = { version = "3", features = ["derive"] }
-
-[build-dependencies]
-capnpc = "0.14"
-\ No newline at end of file
+clap = { version = "3", features = ["derive"] }
+\ No newline at end of file
diff --git a/build.rs b/build.rs
@@ -1,8 +0,0 @@
-fn main() {
- capnpc::CompilerCommand::new()
- .src_prefix("schema")
- .import_path("schema")
- .file("schema/matrix.capnp")
- .run()
- .expect("schema compiler command");
-}
diff --git a/src/events/common.rs b/src/events/common.rs
@@ -0,0 +1,19 @@
+use minicbor::{Decode, Encode};
+use std::borrow::Cow;
+
+#[derive(Encode, Decode)]
+pub(crate) struct Unsigned<'a> {
+ #[b(0)]
+ pub(crate) age: u64,
+ #[b(1)]
+ pub(crate) transaction_id: Cow<'a, str>,
+ #[b(2)]
+ pub(crate) prev_content: Option<Todo>,
+ #[b(3)]
+ pub(crate) redacted_because: Option<Cow<'a, str>>,
+}
+
+// Placeholder
+
+#[derive(Encode, Decode)]
+pub(crate) struct Todo;
diff --git a/src/events/message.rs b/src/events/message.rs
@@ -0,0 +1,53 @@
+use std::borrow::Cow;
+
+use minicbor::{Decode, Encode};
+
+use crate::events::common::Unsigned;
+
+#[derive(Encode, Decode)]
+pub(crate) struct Event<'a, Content> {
+ #[b(0)]
+ pub(crate) event_type: EventType,
+ #[b(1)]
+ pub(crate) content: Content,
+ #[b(2)]
+ pub(crate) room_id: Cow<'a, str>,
+ #[b(3)]
+ pub(crate) event_id: Cow<'a, str>,
+ #[b(4)]
+ pub(crate) sender: Cow<'a, str>,
+ #[b(5)]
+ pub(crate) origin_server_ts: u64,
+ #[b(6)]
+ pub(crate) unsigned: Unsigned<'a>,
+}
+
+#[derive(Encode, Decode)]
+pub(crate) struct TextContent<'a> {
+ #[b(0)]
+ pub(crate) msg_type: MessageType,
+ #[b(1)]
+ pub(crate) body: Cow<'a, str>,
+ #[b(2)]
+ pub(crate) format: Option<FormatType>,
+ #[b(3)]
+ pub(crate) formatted_body: Option<Cow<'a, str>>,
+}
+
+#[derive(Encode, Decode)]
+pub(crate) enum FormatType {
+ #[n(0)]
+ Html,
+}
+
+#[derive(Encode, Decode)]
+pub(crate) enum MessageType {
+ #[n(0)]
+ Text,
+}
+
+#[derive(Encode, Decode)]
+pub(crate) enum EventType {
+ #[n(0)]
+ RoomMessage,
+}
diff --git a/src/events/mod.rs b/src/events/mod.rs
@@ -0,0 +1,2 @@
+pub(crate) mod common;
+pub(crate) mod message;
diff --git a/src/main.rs b/src/main.rs
@@ -1,39 +1,17 @@
-use crate::matrix_capnp::message_event;
-use capnp::serialize_packed;
use clap::Parser;
use color_eyre::eyre::{eyre, Result};
-use std::{fs::File, io::BufReader};
+use minicbor::display;
+use std::{
+ fs::File,
+ io::{BufReader, Read},
+};
-pub mod matrix_capnp {
- include!(concat!(env!("OUT_DIR"), "/matrix_capnp.rs"));
-}
-
-pub mod matrix {
- use crate::matrix_capnp::message_event;
- use capnp::message::{Builder, HeapAllocator};
-
- pub fn build_event() -> Builder<HeapAllocator> {
- let mut message = Builder::new_default();
- {
- let mut event = message.init_root::<message_event::Builder>();
+use crate::events::{
+ common::Unsigned,
+ message::{Event, EventType, MessageType, TextContent},
+};
- {
- event.set_event_id("$ip5l5n9ckymoybrwh-jl8BDYl8HCv2nO1d4NCFp4ek0");
- event.set_origin_server_ts(1650133746117);
- event.set_sender("@mtrnord:nordgedanken.dev");
- event.set_room_id("!KwXDovBFhYakswlOwN:nordgedanken.dev");
- let mut unsigned = event.reborrow().init_unsigned();
- unsigned.set_age(405);
- unsigned.set_transaction_id("m1650132888916.13");
- let content = event.init_content();
- let mut text = content.init_text();
- text.set_body("This is a test");
- }
- }
-
- message
- }
-}
+mod events;
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
@@ -53,34 +31,41 @@ fn main() -> Result<()> {
}
if args.read {
- let mut data_file = File::open("./data")?;
- let mut buf_reader = BufReader::new(&mut data_file);
- let message_reader = serialize_packed::read_message(
- &mut buf_reader,
- ::capnp::message::ReaderOptions::new(),
- )?;
-
- let event = message_reader.get_root::<message_event::Reader>()?;
-
- println!("event_id: {}", event.get_event_id()?);
- println!("origin_server_ts: {}", event.get_origin_server_ts());
- match event.get_content().which() {
- Ok(message_event::content::Text(content)) => {
- println!("text body: {}", content.get_body()?);
- if content.has_formatted_body() {
- println!("text formatted_body: {}", content.get_formatted_body()?)
- }
- }
- Err(err) => println!("content: {:?}", err),
- _ => println!("content type not yet supported"),
- }
+ todo!();
} else if args.write {
- let event = matrix::build_event();
- let mut data_file = File::create("./data")?;
+ let data: Event<TextContent> = Event {
+ event_type: EventType::RoomMessage,
+ content: TextContent {
+ msg_type: MessageType::Text,
+ body: "This is a test".into(),
+ format: None,
+ formatted_body: None,
+ },
+ room_id: "!KwXDovBFhYakswlOwN:nordgedanken.dev".into(),
+ event_id: "$ip5l5n9ckymoybrwh-jl8BDYl8HCv2nO1d4NCFp4ek0".into(),
+ sender: "@nordgedanken:nordgedanken.dev".into(),
+ origin_server_ts: 1650133746117,
+ unsigned: Unsigned {
+ age: 405,
+ transaction_id: "m1650132888916.13".into(),
+ prev_content: None,
+ redacted_because: None,
+ },
+ };
+ {
+ let mut data_file = File::create("data.cbor")?;
+ minicbor::encode(&data, &mut data_file)?;
+ }
+ {
+ let file = File::open("data.cbor")?;
+ let mut reader = BufReader::new(file);
+ let mut buffer = Vec::new();
- serialize_packed::write_message(&mut data_file, &event)?;
+ // Read file into vector.
+ reader.read_to_end(&mut buffer)?;
- println!("Wrote event to ./data");
+ println!("{}", display(&buffer));
+ }
}
Ok(())