diff --git a/sg1.toml b/sg1.toml index 6702701..2f41576 100644 --- a/sg1.toml +++ b/sg1.toml @@ -4,11 +4,7 @@ from = "a" to = "https://cptlobster.dev" [[routes]] -from = "b" -to = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" - -[[routes]] -from = "d" +from = ["b", "d"] to = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # This is a simple regex matcher. It will match any string that ends in "ic". diff --git a/src/config.rs b/src/config.rs index b714b78..7da1160 100644 --- a/src/config.rs +++ b/src/config.rs @@ -57,35 +57,68 @@ pub struct Config { #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] #[serde(crate = "rocket::serde")] pub struct Route { - from: String, + from: Multiable, to: String, #[serde(default)] using: MatcherType } +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +#[serde(crate = "rocket::serde", untagged)] +enum Multiable { + Single(T), + Multi(Vec) +} + impl Route { pub fn matches(&self, input: &str) -> bool { - match self.using { - MatcherType::Regex => { - let re = Regex::new(format!("^{}$", self.from).as_str()).unwrap(); + match &self.from { + Multiable::Single(from) => match self.using { + MatcherType::Regex => { + let re = Regex::new(format ! ("^{}$", from).as_str()).unwrap(); re.is_match(input) - }, - _ => self.from == input.to_string() + }, + _ => *from == input.to_string() + } + Multiable::Multi(from_vec) => match self.using { + MatcherType::Regex => { + from_vec.into_iter().find(|from| { + let re = Regex::new(format ! ("^{}$", from).as_str()).unwrap(); + re.is_match(input) + }).is_some() + }, + _ => from_vec.into_iter().find(|from| **from == input.to_string()).is_some() + } } } - pub fn src(&self) -> String { - self.from.clone() + pub fn src(&self) -> Vec { + match &self.from { + Multiable::Single(from) => vec![from.clone()], + Multiable::Multi(from_vec) => from_vec.clone() + } } pub fn dest(&self, input: &str) -> String { - match self.using { - MatcherType::Regex => { - let re = Regex::new(format!("^{}$", self.from).as_str()).unwrap(); + match &self.from { + Multiable::Single(from) => match self.using { + MatcherType::Regex => { + let re = Regex::new(format ! ("^{}$", from).as_str()).unwrap(); let result = re.replace_all(input, self.to.clone()); result.clone().parse().unwrap() + }, + _ => self.to.clone() }, - _ => self.to.clone() + Multiable::Multi(from_vec) => match self.using { + MatcherType::Regex => { + from_vec.into_iter().find(|from| { + let re = Regex::new(format!("^{}$", from).as_str()).unwrap(); + let result = re.replace_all(input, self.to.clone()); + result.clone().parse().unwrap() + }).unwrap().clone() + }, + _ => self.to.clone() + } } } } diff --git a/src/main.rs b/src/main.rs index 6e0c167..5a69966 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,7 +29,7 @@ use rocket_dyn_templates::{Template, tera::Tera, context}; async fn list_paths(cm: &State) -> Result { let config = cm.get().await; if config.hide_routes { return Err(NoContent) } - let all_routes = config.routes.iter().map(|r: &Route| r.src()).collect::>(); + let all_routes = config.routes.iter().flat_map(|r: &Route| r.src()).collect::>(); if all_routes.is_empty() { Err(NoContent) } else { Ok(all_routes.into_iter().map(|a| format!("/{}", a)).collect::>().join("\n")) } diff --git a/templates/main.html.tera b/templates/main.html.tera index 799ddfb..fc97a84 100644 --- a/templates/main.html.tera +++ b/templates/main.html.tera @@ -12,9 +12,17 @@ {%- for route in config.routes %}
  • {%- if route.using == "regex" %} - /{{ route.from }} + {% if route.from is iterable %} + {% for r in route.from %}/{{ r }}{% if not loop.last %}, {% endif %}{% endfor %} + {% else %} + /{{ route.from }} + {% endif %} {%- else %} - /{{ route.from }} + {% if route.from is iterable %} + {% for r in route.from %}/{{ r }}{% if not loop.last %}, {% endif %}{% endfor %} + {% else %} + /{{ route.from }} + {% endif %} {%- endif %} ({{ route.using }})