rocketmq_remoting/protocol/heartbeat/
message_model.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17use std::fmt;
18use std::fmt::Display;
19
20use serde::Deserialize;
21use serde::Deserializer;
22use serde::Serialize;
23use serde::Serializer;
24
25#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Default)]
26pub enum MessageModel {
27    Broadcasting,
28    #[default]
29    Clustering,
30}
31
32impl MessageModel {
33    fn get_mode_cn(&self) -> &'static str {
34        match self {
35            MessageModel::Broadcasting => "BROADCASTING",
36            MessageModel::Clustering => "CLUSTERING",
37        }
38    }
39}
40
41impl Serialize for MessageModel {
42    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43    where
44        S: Serializer,
45    {
46        let value = match self {
47            MessageModel::Broadcasting => "BROADCASTING",
48            MessageModel::Clustering => "CLUSTERING",
49        };
50        serializer.serialize_str(value)
51    }
52}
53
54impl<'de> Deserialize<'de> for MessageModel {
55    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
56    where
57        D: Deserializer<'de>,
58    {
59        struct MessageModelVisitor;
60
61        impl serde::de::Visitor<'_> for MessageModelVisitor {
62            type Value = MessageModel;
63
64            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
65                formatter.write_str("a string representing TopicFilterType")
66            }
67
68            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
69            where
70                E: serde::de::Error,
71            {
72                match value {
73                    "BROADCASTING" => Ok(MessageModel::Broadcasting),
74                    "CLUSTERING" => Ok(MessageModel::Clustering),
75                    _ => Err(serde::de::Error::unknown_variant(
76                        value,
77                        &["BROADCASTING", "CLUSTERING"],
78                    )),
79                }
80            }
81        }
82
83        deserializer.deserialize_str(MessageModelVisitor)
84    }
85}
86
87impl Display for MessageModel {
88    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89        write!(f, "{}", self.get_mode_cn())
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use serde_json;
96
97    use super::*;
98
99    #[test]
100    fn serialize_message_model_broadcasting() {
101        let model = MessageModel::Broadcasting;
102        let serialized = serde_json::to_string(&model).unwrap();
103        assert_eq!(serialized, "\"BROADCASTING\"");
104    }
105
106    #[test]
107    fn serialize_message_model_clustering() {
108        let model = MessageModel::Clustering;
109        let serialized = serde_json::to_string(&model).unwrap();
110        assert_eq!(serialized, "\"CLUSTERING\"");
111    }
112
113    #[test]
114    fn deserialize_message_model_broadcasting() {
115        let json = "\"BROADCASTING\"";
116        let deserialized: MessageModel = serde_json::from_str(json).unwrap();
117        assert_eq!(deserialized, MessageModel::Broadcasting);
118    }
119
120    #[test]
121    fn deserialize_message_model_clustering() {
122        let json = "\"CLUSTERING\"";
123        let deserialized: MessageModel = serde_json::from_str(json).unwrap();
124        assert_eq!(deserialized, MessageModel::Clustering);
125    }
126
127    #[test]
128    fn deserialize_message_model_invalid() {
129        let json = "\"INVALID\"";
130        let deserialized: Result<MessageModel, _> = serde_json::from_str(json);
131        assert!(deserialized.is_err());
132    }
133
134    #[test]
135    fn display_message_model_broadcasting() {
136        let model = MessageModel::Broadcasting;
137        assert_eq!(model.to_string(), "BROADCASTING");
138    }
139
140    #[test]
141    fn display_message_model_clustering() {
142        let model = MessageModel::Clustering;
143        assert_eq!(model.to_string(), "CLUSTERING");
144    }
145}