Skip to content

Commit

Permalink
feat: some helper function for mailbox message (#37)
Browse files Browse the repository at this point in the history
* feat: some helper function for mailbox message

* feat: some helper function for mailbox message

* feat: some helper function for mailbox message
  • Loading branch information
MichaelScofield committed May 18, 2023
1 parent e94a248 commit f43972a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions src/v1/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

tonic::include_proto!("greptime.v1.meta");

mod mailbox;

use std::collections::HashMap;
use std::hash::{Hash, Hasher};

Expand Down
52 changes: 52 additions & 0 deletions src/v1/meta/mailbox.rs
Original file line number Diff line number Diff line change
@@ -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<T>(
subject: &str,
from: &str,
to: &str,
timestamp_millis: i64,
payload: &T,
) -> Result<MailboxMessage>
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
)
}
}

0 comments on commit f43972a

Please sign in to comment.