rocketmq_remoting/rpc/
topic_request_header.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::collections::HashMap;
18
19use cheetah_string::CheetahString;
20use serde::Deserialize;
21use serde::Serialize;
22
23use crate::protocol::command_custom_header::CommandCustomHeader;
24use crate::protocol::command_custom_header::FromMap;
25use crate::rpc::rpc_request_header::RpcRequestHeader;
26
27#[derive(Clone, Debug, Serialize, Deserialize, Default)]
28pub struct TopicRequestHeader {
29    #[serde(flatten)]
30    pub rpc_request_header: Option<RpcRequestHeader>,
31    pub lo: Option<bool>,
32}
33
34impl TopicRequestHeader {
35    pub const LO: &'static str = "lo";
36
37    pub fn get_lo(&self) -> Option<&bool> {
38        self.lo.as_ref()
39    }
40
41    pub fn set_lo(&mut self, lo: bool) {
42        self.lo = Some(lo);
43    }
44
45    pub fn get_broker_name(&self) -> Option<&CheetahString> {
46        self.rpc_request_header
47            .as_ref()
48            .and_then(|v| v.broker_name.as_ref())
49    }
50}
51
52impl FromMap for TopicRequestHeader {
53    type Error = rocketmq_error::RocketMQError;
54
55    type Target = Self;
56
57    fn from(map: &HashMap<CheetahString, CheetahString>) -> Result<Self::Target, Self::Error> {
58        Ok(TopicRequestHeader {
59            lo: map
60                .get(&CheetahString::from_static_str(Self::LO))
61                .and_then(|v| v.parse().ok()),
62            rpc_request_header: Some(<RpcRequestHeader as FromMap>::from(map)?),
63        })
64    }
65}
66
67impl CommandCustomHeader for TopicRequestHeader {
68    fn to_map(&self) -> Option<HashMap<CheetahString, CheetahString>> {
69        let mut map = HashMap::new();
70        if let Some(ref lo) = self.lo {
71            map.insert(
72                CheetahString::from_static_str(Self::LO),
73                CheetahString::from_string(lo.to_string()),
74            );
75        }
76        if let Some(value) = self.rpc_request_header.as_ref() {
77            if let Some(rpc_map) = value.to_map() {
78                map.extend(rpc_map);
79            }
80        }
81        Some(map)
82    }
83}