rocketmq_common/
common.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 */
17
18use std::fmt;
19use std::fmt::Debug;
20use std::fmt::Display;
21use std::str::FromStr;
22
23pub use faq::FAQUrl;
24use serde::Deserialize;
25use serde::Deserializer;
26use serde::Serialize;
27use serde::Serializer;
28
29pub use crate::common::sys_flag::topic_sys_flag as TopicSysFlag;
30
31pub mod attribute;
32pub mod base;
33pub mod boundary_type;
34pub mod broker;
35pub mod compression;
36pub mod config;
37pub mod config_manager;
38pub mod constant;
39pub mod consumer;
40mod faq;
41pub mod filter;
42pub mod future;
43pub mod hasher;
44pub mod key_builder;
45pub mod macros;
46pub mod message;
47pub mod mix_all;
48pub mod mq_version;
49pub mod namesrv;
50pub mod pop_ack_constants;
51
52pub mod running;
53pub mod server;
54pub mod statistics;
55pub mod stats;
56pub mod sys_flag;
57
58pub mod system_clock;
59pub mod thread;
60pub mod topic;
61
62#[derive(Clone, Default, Eq, PartialEq, Copy)]
63pub enum TopicFilterType {
64    #[default]
65    SingleTag,
66    MultiTag,
67}
68
69impl Display for TopicFilterType {
70    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71        match self {
72            TopicFilterType::SingleTag => write!(f, "SINGLE_TAG"),
73            TopicFilterType::MultiTag => write!(f, "MULTI_TAG"),
74        }
75    }
76}
77
78impl Debug for TopicFilterType {
79    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80        match self {
81            TopicFilterType::SingleTag => write!(f, "SINGLE_TAG"),
82            TopicFilterType::MultiTag => write!(f, "MULTI_TAG"),
83        }
84    }
85}
86
87impl From<&str> for TopicFilterType {
88    fn from(s: &str) -> TopicFilterType {
89        match s {
90            "SINGLE_TAG" => TopicFilterType::SingleTag,
91            "MULTI_TAG" => TopicFilterType::MultiTag,
92            _ => TopicFilterType::SingleTag,
93        }
94    }
95}
96
97impl From<String> for TopicFilterType {
98    fn from(s: String) -> TopicFilterType {
99        TopicFilterType::from(s.as_str())
100    }
101}
102
103impl From<i32> for TopicFilterType {
104    fn from(i: i32) -> TopicFilterType {
105        match i {
106            0 => TopicFilterType::SingleTag,
107            1 => TopicFilterType::MultiTag,
108            _ => TopicFilterType::SingleTag,
109        }
110    }
111}
112
113impl Serialize for TopicFilterType {
114    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
115    where
116        S: Serializer,
117    {
118        let value = match self {
119            TopicFilterType::SingleTag => "SINGLE_TAG",
120            TopicFilterType::MultiTag => "MULTI_TAG",
121        };
122        serializer.serialize_str(value)
123    }
124}
125
126impl<'de> Deserialize<'de> for TopicFilterType {
127    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
128    where
129        D: Deserializer<'de>,
130    {
131        struct TopicFilterTypeVisitor;
132
133        impl serde::de::Visitor<'_> for TopicFilterTypeVisitor {
134            type Value = TopicFilterType;
135
136            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
137                formatter.write_str("a string representing TopicFilterType")
138            }
139
140            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
141            where
142                E: serde::de::Error,
143            {
144                match value {
145                    "SINGLE_TAG" => Ok(TopicFilterType::SingleTag),
146                    "MULTI_TAG" => Ok(TopicFilterType::MultiTag),
147                    _ => Err(serde::de::Error::unknown_variant(
148                        value,
149                        &["SingleTag", "MultiTag"],
150                    )),
151                }
152            }
153        }
154
155        deserializer.deserialize_str(TopicFilterTypeVisitor)
156    }
157}
158
159pub struct Pair<T, U> {
160    pub left: T,
161    pub right: U,
162}
163
164impl<T, U> Pair<T, U> {
165    pub fn new(left: T, right: U) -> Self {
166        Self { left, right }
167    }
168}