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
use std::collections::HashMap;

use anyhow::Error;
use serde::{Deserialize, Serialize};

use crate::protocol::command_custom_header::{CommandCustomHeader, FromMap};

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct NotifyMinBrokerIdChangeRequestHeader {
    #[serde(rename = "minBrokerId")]
    pub min_broker_id: Option<i64>,

    #[serde(rename = "brokerName")]
    pub broker_name: Option<String>,

    #[serde(rename = "minBrokerAddr")]
    pub min_broker_addr: Option<String>,

    #[serde(rename = "offlineBrokerAddr")]
    pub offline_broker_addr: Option<String>,

    #[serde(rename = "haBrokerAddr")]
    pub ha_broker_addr: Option<String>,
}

impl NotifyMinBrokerIdChangeRequestHeader {
    const MIN_BROKER_ID: &'static str = "minBrokerId";
    const BROKER_NAME: &'static str = "brokerName";
    const MIN_BROKER_ADDR: &'static str = "minBrokerAddr";
    const OFFLINE_BROKER_ADDR: &'static str = "offlineBrokerAddr";
    const HA_BROKER_ADDR: &'static str = "haBrokerAddr";

    pub fn new(
        min_broker_id: Option<i64>,
        broker_name: Option<String>,
        min_broker_addr: Option<String>,
        offline_broker_addr: Option<String>,
        ha_broker_addr: Option<String>,
    ) -> Self {
        NotifyMinBrokerIdChangeRequestHeader {
            min_broker_id,
            broker_name,
            min_broker_addr,
            offline_broker_addr,
            ha_broker_addr,
        }
    }
}

impl FromMap for NotifyMinBrokerIdChangeRequestHeader {
    type Target = Self;

    fn from(map: &HashMap<String, String>) -> Option<Self::Target> {
        Some(NotifyMinBrokerIdChangeRequestHeader {
            min_broker_id: map
                .get(NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ID)
                .and_then(|s| s.parse::<i64>().ok()),
            broker_name: map
                .get(NotifyMinBrokerIdChangeRequestHeader::BROKER_NAME)
                .map(|s| s.to_string()),
            min_broker_addr: map
                .get(NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ADDR)
                .map(|s| s.to_string()),
            offline_broker_addr: map
                .get(NotifyMinBrokerIdChangeRequestHeader::OFFLINE_BROKER_ADDR)
                .map(|s| s.to_string()),
            ha_broker_addr: map
                .get(NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR)
                .map(|s| s.to_string()),
        })
    }
}

impl CommandCustomHeader for NotifyMinBrokerIdChangeRequestHeader {
    fn check_fields(&self) -> anyhow::Result<(), Error> {
        todo!()
    }

    fn to_map(&self) -> Option<HashMap<String, String>> {
        let mut map = HashMap::<String, String>::new();
        if let Some(min_broker_id) = self.min_broker_id {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ID.to_string(),
                min_broker_id.to_string(),
            );
        }
        if let Some(ref broker_name) = self.broker_name {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::BROKER_NAME.to_string(),
                broker_name.clone(),
            );
        }
        if let Some(ref min_broker_addr) = self.min_broker_addr {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::MIN_BROKER_ADDR.to_string(),
                min_broker_addr.clone(),
            );
        }

        if let Some(ref ha_broker_addr) = self.ha_broker_addr {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR.to_string(),
                ha_broker_addr.clone(),
            );
        }

        if let Some(ref offline_broker_addr) = self.offline_broker_addr {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::OFFLINE_BROKER_ADDR.to_string(),
                offline_broker_addr.clone(),
            );
        }

        if let Some(ref ha_broker_addr) = self.ha_broker_addr {
            map.insert(
                NotifyMinBrokerIdChangeRequestHeader::HA_BROKER_ADDR.to_string(),
                ha_broker_addr.to_string(),
            );
        }

        Some(map)
    }
}