From f43972af48f8be1cbb1d27640a8aba8b30955028 Mon Sep 17 00:00:00 2001 From: LFC Date: Thu, 18 May 2023 15:03:00 +0800 Subject: [PATCH] feat: some helper function for mailbox message (#37) * feat: some helper function for mailbox message * feat: some helper function for mailbox message * feat: some helper function for mailbox message --- Cargo.toml | 2 ++ src/v1/meta.rs | 2 ++ src/v1/meta/mailbox.rs | 52 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 src/v1/meta/mailbox.rs diff --git a/Cargo.toml b/Cargo.toml index ca1a658..0d8bbcb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,8 @@ license = "Apache-2.0" [dependencies] prost = "0.11" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" tonic = "0.9" [build-dependencies] diff --git a/src/v1/meta.rs b/src/v1/meta.rs index 038946e..c90a567 100644 --- a/src/v1/meta.rs +++ b/src/v1/meta.rs @@ -14,6 +14,8 @@ tonic::include_proto!("greptime.v1.meta"); +mod mailbox; + use std::collections::HashMap; use std::hash::{Hash, Hasher}; diff --git a/src/v1/meta/mailbox.rs b/src/v1/meta/mailbox.rs new file mode 100644 index 0000000..e66db79 --- /dev/null +++ b/src/v1/meta/mailbox.rs @@ -0,0 +1,52 @@ +// Copyright 2023 Greptime Team +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use crate::v1::meta::mailbox_message::Payload; +use crate::v1::meta::MailboxMessage; +use serde::Serialize; +use serde_json::Result; +use std::fmt::{Display, Formatter}; + +impl MailboxMessage { + pub fn json_message( + subject: &str, + from: &str, + to: &str, + timestamp_millis: i64, + payload: &T, + ) -> Result + where + T: ?Sized + Serialize + Display, + { + let payload = serde_json::to_string(payload)?; + Ok(MailboxMessage { + id: 0, // "id" will be set by the mailbox. + subject: subject.to_string(), + from: from.to_string(), + to: to.to_string(), + timestamp_millis, + payload: Some(Payload::Json(payload)), + }) + } +} + +impl Display for MailboxMessage { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "MailboxMessage(id={}, subject={}, from={}, to={}, timestamp_millis={}, payload={:?})", + self.id, self.subject, self.from, self.to, self.timestamp_millis, self.payload + ) + } +}