mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-07 00:13:10 +00:00
* Added main skeleton for toy-chat example * Implement chat example main block * Polish chat and add necessary fixes * Added author info to crates
43 lines
1.0 KiB
Rust
43 lines
1.0 KiB
Rust
use chrono::{DateTime, TimeZone, Utc};
|
|
use once_cell::sync::Lazy;
|
|
use prost::Message;
|
|
use waku::{Encoding, WakuContentTopic};
|
|
|
|
pub static TOY_CHAT_CONTENT_TOPIC: Lazy<WakuContentTopic> = Lazy::new(|| WakuContentTopic {
|
|
application_name: "toy-chat".into(),
|
|
version: 2,
|
|
content_topic_name: "huilong".into(),
|
|
encoding: Encoding::Proto,
|
|
});
|
|
|
|
#[derive(Clone, Message)]
|
|
pub struct Chat2Message {
|
|
#[prost(uint64, tag = "1")]
|
|
timestamp: u64,
|
|
#[prost(string, tag = "2")]
|
|
nick: String,
|
|
#[prost(bytes, tag = "3")]
|
|
payload: Vec<u8>,
|
|
}
|
|
|
|
impl Chat2Message {
|
|
pub fn new(nick: &str, payload: &str) -> Self {
|
|
Self {
|
|
timestamp: Utc::now().timestamp() as u64,
|
|
nick: nick.to_string(),
|
|
payload: payload.as_bytes().to_vec(),
|
|
}
|
|
}
|
|
pub fn message(&self) -> String {
|
|
String::from_utf8(self.payload.clone()).unwrap()
|
|
}
|
|
|
|
pub fn nick(&self) -> &str {
|
|
&self.nick
|
|
}
|
|
|
|
pub fn timestamp(&self) -> DateTime<Utc> {
|
|
Utc.timestamp(self.timestamp as i64, 0)
|
|
}
|
|
}
|