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
136
137
138
139
140
141
142
143
144
145
146
/*
 * 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 lazy_static::lazy_static;

/*const VALID_CHAR_BIT_MAP: [bool; 128] = {
    let mut bit_map = [false; 128];
    bit_map['%' as usize] = true;
    bit_map['-' as usize] = true;
    bit_map['_' as usize] = true;
    bit_map['|' as usize] = true;
    for i in '0' as u8..'9' as u8 + 1 {
        bit_map[i as usize] = true;
    }
    for i in 'A' as u8..'Z' as u8 + 1 {
        bit_map[i as usize] = true;
    }
    for i in 'a' as u8..'z' as u8 + 1 {
        bit_map[i as usize] = true;
    }
    bit_map
};*/

const TOPIC_MAX_LENGTH: usize = 127;

pub struct ValidateTopicResult {
    valid: bool,
    remark: String,
}

impl ValidateTopicResult {
    pub fn new(valid: bool, remark: impl Into<String>) -> Self {
        Self {
            valid,
            remark: remark.into(),
        }
    }

    pub fn valid(&self) -> bool {
        self.valid
    }

    pub fn remark(&self) -> &str {
        &self.remark
    }
}

pub struct TopicValidator;

lazy_static! {
    pub static ref SYSTEM_TOPIC_SET: std::collections::HashSet<&'static str> = {
        let mut set = std::collections::HashSet::new();
        set.insert(TopicValidator::AUTO_CREATE_TOPIC_KEY_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_BENCHMARK_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_HALF_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRACE_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_OP_HALF_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_CHECK_MAX_TIME_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_SELF_TEST_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_OFFSET_MOVED_EVENT);
        set.insert(TopicValidator::RMQ_SYS_ROCKSDB_OFFSET_TOPIC);
        set
    };
    pub static ref NOT_ALLOWED_SEND_TOPIC_SET: std::collections::HashSet<&'static str> = {
        let mut set = std::collections::HashSet::new();

        set.insert(TopicValidator::RMQ_SYS_SCHEDULE_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_HALF_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_OP_HALF_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_TRANS_CHECK_MAX_TIME_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_SELF_TEST_TOPIC);
        set.insert(TopicValidator::RMQ_SYS_OFFSET_MOVED_EVENT);
        set
    };
}

impl TopicValidator {
    pub const AUTO_CREATE_TOPIC_KEY_TOPIC: &'static str = "TBW102";
    pub const RMQ_SYS_SCHEDULE_TOPIC: &'static str = "SCHEDULE_TOPIC_XXXX";
    pub const RMQ_SYS_BENCHMARK_TOPIC: &'static str = "BenchmarkTest";
    pub const RMQ_SYS_TRANS_HALF_TOPIC: &'static str = "RMQ_SYS_TRANS_HALF_TOPIC";
    pub const RMQ_SYS_TRACE_TOPIC: &'static str = "RMQ_SYS_TRACE_TOPIC";
    pub const RMQ_SYS_TRANS_OP_HALF_TOPIC: &'static str = "RMQ_SYS_TRANS_OP_HALF_TOPIC";
    pub const RMQ_SYS_TRANS_CHECK_MAX_TIME_TOPIC: &'static str = "TRANS_CHECK_MAX_TIME_TOPIC";
    pub const RMQ_SYS_SELF_TEST_TOPIC: &'static str = "SELF_TEST_TOPIC";
    pub const RMQ_SYS_OFFSET_MOVED_EVENT: &'static str = "OFFSET_MOVED_EVENT";
    pub const RMQ_SYS_ROCKSDB_OFFSET_TOPIC: &'static str = "CHECKPOINT_TOPIC";
    pub const SYSTEM_TOPIC_PREFIX: &'static str = "rmq_sys_";
    pub const SYNC_BROKER_MEMBER_GROUP_PREFIX: &'static str = "SYNC_BROKER_MEMBER_";

    pub fn is_system_topic(topic: &str) -> bool {
        SYSTEM_TOPIC_SET.contains(topic) || topic.starts_with(Self::SYSTEM_TOPIC_PREFIX)
    }

    pub fn is_topic_or_group_illegal(_topic: &str) -> bool {
        /*topic
        .chars()
        .any(|ch| ch as usize >= 128 || !VALID_CHAR_BIT_MAP[ch as usize])*/
        false
    }

    pub fn validate_topic(topic: &str) -> ValidateTopicResult {
        if topic.is_empty() {
            return ValidateTopicResult::new(false, "The specified topic is blank.");
        }

        if Self::is_topic_or_group_illegal(topic) {
            return ValidateTopicResult::new(
                false,
                "The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$",
            );
        }

        if topic.len() > TOPIC_MAX_LENGTH {
            return ValidateTopicResult::new(
                false,
                "The specified topic is longer than topic max length.",
            );
        }

        ValidateTopicResult::new(true, "")
    }

    pub fn is_not_allowed_send_topic(topic: &str) -> bool {
        NOT_ALLOWED_SEND_TOPIC_SET.contains(topic)
    }

    /*    pub fn add_system_topic(&mut self, system_topic: &str) {
        self.system_topic_set.insert(system_topic.to_string());
    }*/
}