replayer-mx

git clone git://archive.git.mtrnord.blog/MTRNord/replayer-mx.git
Log | Files | Refs

commit 2dfe1dadaa85c0b3bb4ef60edea64e1fb2404a5a
parent 8642dadb5331c2e74bc19a62e1bc382a286115e4
Author: MTRNord <mtrnord1@gmail.com>
Date:   Sat, 16 Apr 2022 22:32:56 +0200

Remove generic to make it easier to decode

Diffstat:
Msrc/events/common.rs | 2+-
Msrc/events/message.rs | 73++++++++++++++++++++++++++++++++++++++++++++++---------------------------
Msrc/main.rs | 19++++++++++++-------
3 files changed, 59 insertions(+), 35 deletions(-)

diff --git a/src/events/common.rs b/src/events/common.rs @@ -1,7 +1,7 @@ use minicbor::{Decode, Encode}; use std::borrow::Cow; -#[derive(Encode, Decode)] +#[derive(Encode, Decode, Debug)] pub(crate) struct Unsigned<'a, Content> { #[b(0)] pub(crate) age: u64, diff --git a/src/events/message.rs b/src/events/message.rs @@ -4,52 +4,71 @@ use minicbor::{Decode, Encode}; use crate::events::common::Unsigned; -#[derive(Encode, Decode)] -pub(crate) struct Event<'a, Content> { +#[derive(Encode, Decode, Debug)] +pub(crate) struct Event<'a> { #[b(0)] - pub(crate) event_type: EventType, + pub(crate) content: Content<'a>, #[b(1)] - pub(crate) content: Content, - #[b(2)] pub(crate) room_id: Cow<'a, str>, - #[b(3)] + #[b(2)] pub(crate) event_id: Cow<'a, str>, - #[b(4)] + #[b(3)] pub(crate) sender: Cow<'a, str>, - #[b(5)] + #[b(4)] pub(crate) origin_server_ts: u64, + #[b(5)] + pub(crate) unsigned: Unsigned<'a, Content<'a>>, #[b(6)] - pub(crate) unsigned: Unsigned<'a, Content>, - #[b(7)] pub(crate) state_key: Option<Cow<'a, str>>, } -#[derive(Encode, Decode)] -pub(crate) struct TextContent<'a> { +#[derive(Encode, Decode, Debug)] +pub(crate) enum RoomMessageContent<'a> { #[b(0)] - pub(crate) msg_type: MessageType, + Text(#[b(0)] TextContent<'a>), #[b(1)] + Audio(#[b(0)] AudioContent<'a>), +} + +/// Use for text, emotes and notices +#[derive(Encode, Decode, Debug)] +pub(crate) struct TextContent<'a> { + #[b(0)] pub(crate) body: Cow<'a, str>, - #[b(2)] + #[b(1)] pub(crate) format: Option<FormatType>, - #[b(3)] + #[b(2)] pub(crate) formatted_body: Option<Cow<'a, str>>, } -#[derive(Encode, Decode)] -pub(crate) enum FormatType { - #[n(0)] - Html, +#[derive(Encode, Decode, Debug)] +pub(crate) struct AudioContent<'a> { + #[b(0)] + pub(crate) body: Cow<'a, str>, + #[b(1)] + pub(crate) url: Cow<'a, str>, + #[b(2)] + pub(crate) info: Option<AudioInfo<'a>>, +} + +#[derive(Encode, Decode, Debug)] +pub(crate) struct AudioInfo<'a> { + #[b(0)] + pub(crate) duration: u64, + #[b(1)] + pub(crate) mime_type: Cow<'a, str>, + #[b(2)] + pub(crate) size: u64, } -#[derive(Encode, Decode)] -pub(crate) enum MessageType { - #[n(0)] - Text, +#[derive(Encode, Decode, Debug)] +pub(crate) enum FormatType { + #[b(0)] + Html, } -#[derive(Encode, Decode)] -pub(crate) enum EventType { - #[n(0)] - RoomMessage, +#[derive(Encode, Decode, Debug)] +pub(crate) enum Content<'a> { + #[b(0)] + RoomMessage(#[b(0)] RoomMessageContent<'a>), } diff --git a/src/main.rs b/src/main.rs @@ -8,7 +8,7 @@ use std::{ use crate::events::{ common::Unsigned, - message::{Event, EventType, MessageType, TextContent}, + message::{Content, Event, RoomMessageContent, TextContent}, }; mod events; @@ -31,16 +31,21 @@ fn main() -> Result<()> { } if args.read { - todo!(); + let file = File::open("data.cbor")?; + let mut reader = BufReader::new(file); + let mut buffer = Vec::new(); + + // Read file into vector. + reader.read_to_end(&mut buffer)?; + let output: Event = minicbor::decode(&buffer)?; + println!("{:#?}", output); } else if args.write { - let data: Event<TextContent> = Event { - event_type: EventType::RoomMessage, - content: TextContent { - msg_type: MessageType::Text, + let data: Event = Event { + content: Content::RoomMessage(RoomMessageContent::Text(TextContent { 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(),