use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct MessageFlags {
pub entries: Vec<String>,
}
impl Default for MessageFlags {
fn default() -> Self {
Self::new()
}
}
impl FromStr for MessageFlags {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
let flags = s.replace('\n', "");
let segments = flags.split(',');
let mut result = Self::new();
for x in segments {
if !x.is_empty() {
result.entries.push(String::from(x.trim()));
}
}
Ok(result)
}
}
impl Display for MessageFlags {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.is_empty() {
write!(f, "")
} else {
write!(f, "{}", self.entries.join(", "))
}
}
}
impl MessageFlags {
pub fn new() -> Self {
MessageFlags { entries: vec![] }
}
pub fn count(&self) -> usize {
self.entries.len()
}
pub fn is_empty(&self) -> bool {
self.entries.is_empty()
}
pub fn contains(&self, flag: &str) -> bool {
let flag = flag.to_string();
self.entries.contains(&flag)
}
pub fn is_fuzzy(&self) -> bool {
self.contains("fuzzy")
}
pub fn add_flag(&mut self, flag: &str) {
if !self.contains(flag) {
self.entries.push(flag.to_string());
}
}
pub fn remove_flag(&mut self, flag: &str) {
if let Some(index) = self.entries.iter().position(|x| *x == flag) {
self.entries.remove(index);
}
}
pub fn iter(&self) -> std::slice::Iter<'_, String> {
self.entries.iter()
}
pub fn iter_mut(&mut self) -> std::slice::IterMut<'_, String> {
self.entries.iter_mut()
}
}
#[cfg(test)]
mod test {
use crate::message::MessageFlags;
use std::str::FromStr;
#[test]
fn test_flags_from_string() {
assert_eq!(
MessageFlags::from_str("").unwrap().entries,
Vec::<String>::new()
);
assert_eq!(
MessageFlags::from_str("fuzzy").unwrap().entries,
vec!["fuzzy"]
);
assert_eq!(
MessageFlags::from_str("c-format, fuzzy").unwrap().entries,
vec!["c-format", "fuzzy"]
);
}
#[test]
fn test_flags_to_string() {
assert_eq!(MessageFlags { entries: vec![] }.to_string(), "");
assert_eq!(
MessageFlags {
entries: vec![String::from("fuzzy")]
}
.to_string(),
"fuzzy"
);
assert_eq!(
MessageFlags {
entries: vec![String::from("c-format"), String::from("fuzzy")]
}
.to_string(),
"c-format, fuzzy"
);
}
}