WIP prefix fixes

This commit is contained in:
Frankie Bagnardi 2018-09-17 23:55:35 -07:00
parent 4728e85e0e
commit 5a50e4eea8

View file

@ -1,5 +1,4 @@
//! A module providing an enum for a message prefix. //! A module providing an enum for a message prefix.
use std::borrow::ToOwned;
use std::string; use std::string;
use std::str::FromStr; use std::str::FromStr;
use std::fmt; use std::fmt;
@ -30,34 +29,37 @@ impl Prefix {
pub fn new_from_str(s: &str) -> Prefix { pub fn new_from_str(s: &str) -> Prefix {
#[derive(Copy, Clone, Eq, PartialEq)] #[derive(Copy, Clone, Eq, PartialEq)]
enum Active { enum Active {
Name = 0, Name,
User = 1, User,
Host = 2, Host,
} }
let mut name = String::new(); let mut name = String::new();
let mut user = String::new(); let mut user = String::new();
let mut host = String::new(); let mut host = String::new();
let mut active = Active::Name; let mut active = Active::Name;
let mut is_server = false;
for c in s.chars() { for c in s.chars() {
match c { if c == '.' {
// We consider the '.' to be a ServerName except if a ! has already // We won't return Nickname("nick", "", "") but if @ or ! are
// been encountered. // encountered, then we set this back to false
'.' if active == Active::Name => return Prefix::ServerName(s.to_owned()), is_server = true;
}
match c {
'!' if active == Active::Name => { '!' if active == Active::Name => {
is_server = false;
active = Active::User; active = Active::User;
}, },
// The '@' is not special until we've started the username '@' if active != Active::Host => {
// portion is_server = false;
'@' if active == Active::User => {
active = Active::Host; active = Active::Host;
}, },
_ => { _ => {
// Push onto the latest buffer // Push onto the active buffer
match active { match active {
Active::Name => &mut name, Active::Name => &mut name,
Active::User => &mut user, Active::User => &mut user,
@ -67,7 +69,11 @@ impl Prefix {
} }
} }
Prefix::Nickname(name, user, host) if is_server {
Prefix::ServerName(name)
} else {
Prefix::Nickname(name, user, host)
}
} }
} }
@ -89,9 +95,7 @@ impl fmt::Display for Prefix {
("", "", "") => write!(f, ""), ("", "", "") => write!(f, ""),
(name, "", "") => write!(f, "{}", name), (name, "", "") => write!(f, "{}", name),
(name, user, "") => write!(f, "{}!{}", name, user), (name, user, "") => write!(f, "{}!{}", name, user),
// This case shouldn't happen normally, but user!@host is invalid, so we (name, "", host) => write!(f, "{}@{}", name, host),
// format it without the host
(name, "", _host) => write!(f, "{}", name),
(name, user, host) => write!(f, "{}!{}@{}", name, user, host), (name, user, host) => write!(f, "{}!{}@{}", name, user, host),
}, },
} }