#[derive(Debug)]
pub struct Rewrite {
pub from: String,
pub to: String,
}
impl Rewrite {
pub fn new(from: String, to: String) -> Self {
let from_parts = from.split("/").collect::<Vec<&str>>();
let to_parts = to.split("/").collect::<Vec<&str>>();
let from_parts_in = from_parts.clone();
let to_parts_in = to_parts.clone();
let mut end = false;
let mut index = 0;
for (from_part, to_part) in from_parts.into_iter().zip(to_parts) {
if from_parts_in[index] == "*" {
if to_parts_in[index] != "*" {
panic!("Rewrite from and to must have the same number of parts");
}
} else if from_part == "**" {
if to_part != "**" {
panic!("Rewrite from and to must have the same number of parts");
}
if from_parts_in[index + 1] != "" {
panic!("** must be at the end of the from path");
}
if to_parts_in[index + 1] != "" {
panic!("** must be at the end of the to path");
}
end = true;
}
index += 1;
}
if !end {
panic!("Rewrite from and to must have the same number of parts");
}
Rewrite {
from,
to,
}
}
}