use std::fmt::{self, Display};
use crate::format::{self, Separator};
const PREFIX: &str = "__";
const SEPARATOR: &str = "_";
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct Keywords(Vec<String>);
impl Keywords {
pub fn parse_user_input<S: AsRef<str>>(string: S) -> Option<Self> {
Self::parse(string, ",")
}
pub fn parse_schemed_string<S: AsRef<str>>(string: S) -> Option<Self> {
Self::parse(string, "_")
}
fn parse<S: AsRef<str>>(string: S, separator: &str) -> Option<Self> {
fn inner(string: &str, separator: &str) -> Option<Keywords> {
let keywords: Vec<_> = string
.split(separator)
.map(|s| format::slugify(s, &Separator::None))
.filter(|k| !k.is_empty())
.collect();
(!keywords.is_empty()).then_some(keywords).map(Keywords)
}
inner(string.as_ref(), separator)
}
}
impl Display for Keywords {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", PREFIX, self.0.join(SEPARATOR))
}
}