mirror of
https://github.com/logos-messaging/logos-messaging-rust-bindings.git
synced 2026-01-07 16:33:08 +00:00
38 lines
981 B
Rust
38 lines
981 B
Rust
use chrono::{DateTime, LocalResult, TimeZone, Utc};
|
|
use prost::Message;
|
|
use waku_bindings::{Encoding, WakuContentTopic};
|
|
|
|
pub static TOY_CHAT_CONTENT_TOPIC: WakuContentTopic =
|
|
WakuContentTopic::new("toy-chat", 2, "huilong", 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) -> LocalResult<DateTime<Utc>> {
|
|
Utc.timestamp_opt(self.timestamp as i64, 0)
|
|
}
|
|
}
|