1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 std::{fmt, str::FromStr};

pub use faq::FAQUrl;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub use crate::common::sys_flag::topic_sys_flag as TopicSysFlag;

pub mod attribute;
pub mod boundary_type;
pub mod broker;
pub mod compression;
pub mod config;
pub mod config_manager;
pub mod constant;
pub mod consumer;
mod faq;
pub mod filter;
pub mod future;
pub mod hasher;
pub mod macros;
pub mod message;
pub mod mix_all;
pub mod mq_version;
pub mod namesrv;
pub mod server;
pub mod sys_flag;
pub mod thread;
pub mod topic;

#[derive(Debug, Clone, Default, Eq, PartialEq, Copy)]
pub enum TopicFilterType {
    #[default]
    SingleTag,
    MultiTag,
}

impl From<&str> for TopicFilterType {
    fn from(s: &str) -> TopicFilterType {
        match s {
            "SINGLE_TAG" => TopicFilterType::SingleTag,
            "MULTI_TAG" => TopicFilterType::MultiTag,
            _ => TopicFilterType::SingleTag,
        }
    }
}

impl From<String> for TopicFilterType {
    fn from(s: String) -> TopicFilterType {
        TopicFilterType::from(s.as_str())
    }
}

impl From<i32> for TopicFilterType {
    fn from(i: i32) -> TopicFilterType {
        match i {
            0 => TopicFilterType::SingleTag,
            1 => TopicFilterType::MultiTag,
            _ => TopicFilterType::SingleTag,
        }
    }
}

impl Serialize for TopicFilterType {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let value = match self {
            TopicFilterType::SingleTag => "SINGLE_TAG",
            TopicFilterType::MultiTag => "MULTI_TAG",
        };
        serializer.serialize_str(value)
    }
}

impl<'de> Deserialize<'de> for TopicFilterType {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct TopicFilterTypeVisitor;

        impl<'de> serde::de::Visitor<'de> for TopicFilterTypeVisitor {
            type Value = TopicFilterType;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("a string representing TopicFilterType")
            }

            fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                match value {
                    "SINGLE_TAG" => Ok(TopicFilterType::SingleTag),
                    "MULTI_TAG" => Ok(TopicFilterType::MultiTag),
                    _ => Err(serde::de::Error::unknown_variant(
                        value,
                        &["SingleTag", "MultiTag"],
                    )),
                }
            }
        }

        deserializer.deserialize_str(TopicFilterTypeVisitor)
    }
}

pub struct Pair<T, U> {
    pub left: T,
    pub right: U,
}

impl<T, U> Pair<T, U> {
    pub fn new(left: T, right: U) -> Self {
        Self { left, right }
    }
}