enum Instructions {
Uppercase,
Lowercase,
Unknown,
}
impl<T> From<T> for Instructions
where
T: ToString,
{
fn from(value: T) -> Self {
match value.to_string().to_lowercase().as_str() {
"uppercase" => Self::Uppercase,
"lowercase" => Self::Lowercase,
_ => Self::Unknown,
}
}
}
pub struct Apply(Vec<Instructions>);
impl Apply {
pub fn parse<T>(input: T) -> Apply
where
T: ToString,
{
Apply(
input
.to_string()
.split(",")
.map(Instructions::from)
.collect::<Vec<Instructions>>(),
)
}
pub fn execute<T>(&self, input: T) -> String
where
T: ToString,
{
let initial = input.to_string();
Self::recurse(&self.0, initial, 0)
}
fn recurse(instance: &[Instructions], current: String, idx: usize) -> String {
if idx >= instance.len() {
return current;
}
let next = match instance[idx] {
Instructions::Uppercase => current.to_uppercase(),
Instructions::Lowercase => current.to_lowercase(),
Instructions::Unknown => current,
};
Self::recurse(instance, next, idx + 1)
}
}