Your one-stop-cake-shop for everything Freshly Baked has to offer

fix(m): correct start/end requirements

Previously we were doctoring the regex in an interesting way that
wouldn't have worked with | (regex or). Since as ^^ still matches the
start of the string, we don't need to strip it out. Finally, to make
sure that regex or can't scupper things, we need to wrap everything in a
non-capturing group...

+4 -18
+2 -2
menu/.sqlx/query-631d51301aa4b22c09d3899089e4d40a8e2485ab6aa9ab9e42292958c2c1bf93.json menu/.sqlx/query-cee60970a2dad15fade0c542f47330294824d077982d81d37a153c3240fb4076.json
··· 1 1 { 2 2 "db_name": "PostgreSQL", 3 - "query": "SELECT \"from\", \"to\" FROM regex WHERE $1 ~* ('^' || \"from\" || '$') LIMIT 1", 3 + "query": "SELECT \"from\", \"to\" FROM regex WHERE $1 ~* ('^(?:' || \"from\" || ')$') LIMIT 1", 4 4 "describe": { 5 5 "columns": [ 6 6 { ··· 24 24 false 25 25 ] 26 26 }, 27 - "hash": "631d51301aa4b22c09d3899089e4d40a8e2485ab6aa9ab9e42292958c2c1bf93" 27 + "hash": "cee60970a2dad15fade0c542f47330294824d077982d81d37a153c3240fb4076" 28 28 }
+2 -16
menu/src/regex.rs
··· 17 17 18 18 pub(crate) async fn get_redirect(go: &str) -> Option<Redirect> { 19 19 let redirect = sqlx::query!( 20 - r#"SELECT "from", "to" FROM regex WHERE $1 ~* ('^' || "from" || '$') LIMIT 1"#, 20 + r#"SELECT "from", "to" FROM regex WHERE $1 ~* ('^(?:' || "from" || ')$') LIMIT 1"#, 21 21 go.to_lowercase() 22 22 ) 23 23 .fetch_one( ··· 32 32 .await; 33 33 34 34 if let Ok(record) = redirect { 35 - let re = RegexBuilder::new(&format!("^{}$", record.from)) 35 + let re = RegexBuilder::new(&format!("^(?:{})$", record.from)) 36 36 .case_insensitive(true) 37 37 .build() 38 38 .unwrap(); ··· 87 87 Ok(link_table) 88 88 } 89 89 90 - fn trim_prefix(s: &str, prefix: char) -> &str { 91 - if s.starts_with(prefix) { &s[1..] } else { s } 92 - } 93 - 94 - fn trim_suffix(s: &str, suffix: char) -> &str { 95 - if s.starts_with(suffix) { 96 - &s[..s.len() - 1] 97 - } else { 98 - s 99 - } 100 - } 101 - 102 90 pub(crate) async fn create( 103 91 from: &str, 104 92 to: &str, ··· 106 94 current: Option<&String>, 107 95 ) -> CreationResult { 108 96 println!("Attempting to make go/{} -> {}", from, to); 109 - 110 - let from = trim_suffix(trim_prefix(from, '^'), '$'); // I think this is maybe broken with | 111 97 112 98 let create_call = sqlx::query!( 113 99 r#"