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
use crate::MessageBuildError;
use std::collections::HashMap;
#[derive(Debug, Clone, Eq, PartialEq, Default)]
pub struct Message<'a> {
tags: HashMap<&'a str, &'a str>,
prefix_name: Option<&'a str>,
prefix_user: Option<&'a str>,
prefix_host: Option<&'a str>,
command: Option<&'a str>,
params: Vec<&'a str>,
trailing: Option<&'a str>,
}
impl<'a> Message<'a> {
pub fn new() -> Self {
Message {
tags: HashMap::new(),
prefix_name: None,
prefix_user: None,
prefix_host: None,
command: None,
params: Vec::new(),
trailing: None,
}
}
pub fn command(mut self, cmd: &'a str) -> Message<'a> {
self.command = Some(cmd);
self
}
pub fn tag(mut self, key: &'a str, value: &'a str) -> Message<'a> {
self.tags.insert(key, value);
self
}
pub fn prefix_name(mut self, name: &'a str) -> Message<'a> {
self.prefix_name = Some(name);
self
}
pub fn prefix_user(mut self, user: &'a str) -> Message<'a> {
self.prefix_user = Some(user);
self
}
pub fn prefix_host(mut self, host: &'a str) -> Message<'a> {
self.prefix_host = Some(host);
self
}
pub fn param(mut self, param: &'a str) -> Message<'a> {
self.params.push(param);
self
}
pub fn set_param(mut self, index: usize, param: &'a str) -> Message<'a> {
if index >= self.params.len() {
self.params.push(param);
}
self.params[index] = param;
self
}
pub fn trailing(mut self, trailing: &'a str) -> Message<'a> {
self.trailing = Some(trailing);
self
}
pub fn build(self) -> Result<crate::message::Message, MessageBuildError> {
let mut str = String::new();
if !self.tags.is_empty() {
str.push('@');
for (key, val) in self.tags {
str.push_str(key);
str.push('=');
str.push_str(val);
str.push(';')
}
str.pop();
str.push(' ');
}
if let Some(prefix_name) = self.prefix_name {
str.push(':');
str.push_str(prefix_name);
if self.prefix_user.is_some() && self.prefix_host.is_none() {
return Err(MessageBuildError::UserWithoutHost);
}
if let Some(user) = self.prefix_user {
str.push('!');
str.push_str(user);
}
if let Some(host) = self.prefix_host {
str.push('@');
str.push_str(host);
}
str.push(' ')
}
if let Some(command) = self.command {
str.push_str(command);
} else {
return Err(MessageBuildError::MissingCommand);
}
if !self.params.is_empty() {
str.push(' ');
str.push_str(&self.params.join(" "));
}
if let Some(trailing) = self.trailing {
str.push_str(" :");
str.push_str(trailing);
}
Ok(crate::message::Message::from(str))
}
}