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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::collections::BTreeMap;
use std::fmt;
use std::fmt::Write;
use std::option::Option;
use std::str::FromStr;
use super::error::Error;
use crate::escaped::{escape_char, unescape_char};
#[derive(Debug, PartialEq, Default)]
pub struct Message {
pub tags: BTreeMap<String, String>,
pub prefix: Option<String>,
pub command: String,
pub params: Vec<String>,
}
fn parse_tags(input: &str) -> Result<BTreeMap<String, String>, Error> {
let mut tags = BTreeMap::new();
for tag_data in input.split(';') {
let mut pieces = tag_data.splitn(2, '=');
let tag_name = pieces
.next()
.ok_or_else(|| Error::TagError("missing tag name".to_string()))?;
let raw_tag_value = pieces.next().unwrap_or("");
let mut tag_value = String::new();
let mut tag_value_chars = raw_tag_value.chars();
while let Some(c) = tag_value_chars.next() {
if c == '\\' {
if let Some(escaped_char) = tag_value_chars.next() {
tag_value.push(unescape_char(escaped_char));
}
} else {
tag_value.push(c);
}
}
tags.insert(tag_name.to_string(), tag_value);
}
Ok(tags)
}
impl FromStr for Message {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
let mut input = input;
if input.ends_with('\n') {
input = &input[..input.len() - 1];
}
if input.ends_with('\r') {
input = &input[..input.len() - 1];
}
let mut tags = BTreeMap::new();
let mut prefix = None;
if input.starts_with('@') {
let mut parts = (&input[1..]).splitn(2, ' ');
let tag_data = parts
.next()
.ok_or_else(|| Error::TagError("failed to parse tag data".to_string()))?;
tags = parse_tags(tag_data)?;
input = parts.next().unwrap_or("").trim_start_matches(' ');
}
if input.starts_with(':') {
let mut parts = (&input[1..]).splitn(2, ' ');
prefix = Some(
parts
.next()
.ok_or_else(|| Error::TagError("failed to parse tag data".to_string()))?
.to_string(),
);
input = parts.next().unwrap_or("").trim_start_matches(' ');
}
let mut parts = input.splitn(2, ' ');
let command = parts
.next()
.ok_or_else(|| Error::CommandError("missing command".to_string()))?
.to_string();
input = parts.next().unwrap_or("").trim_start_matches(' ');
let mut params = Vec::new();
while !input.is_empty() {
if input.starts_with(':') {
params.push(input[1..].to_string());
break;
}
let mut parts = input.splitn(2, ' ');
if let Some(param) = parts.next() {
params.push(param.to_string());
}
input = parts.next().unwrap_or("").trim_start_matches(' ');
}
Ok(Message {
tags,
prefix,
command,
params,
})
}
}
impl fmt::Display for Message {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if !self.tags.is_empty() {
f.write_char('@')?;
for (i, (k, v)) in self.tags.iter().enumerate() {
if i != 0 {
f.write_char(';')?;
}
f.write_str(k)?;
if v.is_empty() {
continue;
}
f.write_char('=')?;
for c in v.chars() {
match escape_char(c) {
Some(escaped_str) => f.write_str(escaped_str)?,
None => f.write_char(c)?,
}
}
}
f.write_char(' ')?;
}
if let Some(prefix) = &self.prefix {
f.write_char(':')?;
f.write_str(prefix)?;
f.write_char(' ')?;
}
f.write_str(&self.command)?;
if let Some((last, params)) = self.params.split_last() {
for param in params {
f.write_char(' ')?;
f.write_str(param)?;
}
f.write_str(" :")?;
f.write_str(last)?;
}
Ok(())
}
}