Skip to content

Commit

Permalink
avoid using a regex when not necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kruse committed May 5, 2024
1 parent f26cb5c commit 9602d4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 53 deletions.
13 changes: 2 additions & 11 deletions src/deafies/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use axum::{
http::header,
response::{IntoResponse, Response},
};
use regex::Regex;
use tokio_util::io::ReaderStream;

use super::actions;
Expand Down Expand Up @@ -49,17 +48,9 @@ pub async fn show(
None => ImageTypes::Original,
};

let rx = Regex::new(r"\.(\w+)$").map_err(|e| AppError::InternalError(e.to_string()))?;
if rx.is_match(&slug) {
let captures = rx
.captures(&slug)
.ok_or_else(|| AppError::InternalError("Invalid regex".to_string()))?;
let parts = slug.rsplit_once('.');

let ext = captures
.get(1)
.ok_or_else(|| AppError::InternalError("Invalid regex".to_string()))?;

let ext = ext.as_str();
if let Some((_, ext)) = parts {
let guid = guid.replace(format!(".{}", ext).as_str(), "");

show_img(state, guid, logged_in, pic_type, ext).await
Expand Down
21 changes: 3 additions & 18 deletions src/pictures/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use axum::{
http::header,
response::{IntoResponse, Response},
};
use regex::Regex;
use tokio_util::io::ReaderStream;

use super::{actions, ImageTypes, TypeParams};
Expand Down Expand Up @@ -38,28 +37,14 @@ pub async fn show(
EPath(info): EPath<String>,
pic_type: Query<TypeParams>,
) -> Result<Response, AppError> {
let rx = Regex::new(r"^(\d+)\.(\w+)$").map_err(|e| AppError::InternalError(e.to_string()))?;
if rx.is_match(&info) {
let captures = rx
.captures(&info)
.ok_or_else(|| AppError::InternalError("Invalid regex".to_string()))?;

let id = captures
.get(1)
.ok_or_else(|| AppError::InternalError("Invalid regex".to_string()))?;
let parts = info.rsplit_once('.');

if let Some((id, suffix)) = parts {
let id = id
.as_str()
.parse()
.map_err(|e| AppError::InternalError(format!("error parsing id: {}", e)))?;

let ext = captures
.get(2)
.ok_or_else(|| AppError::InternalError("Invalid regex".to_string()))?;

let ext = ext.as_str();

show_img(state, id, ext, pic_type).await
show_img(state, id, suffix, pic_type).await
} else {
let id = info
.parse()
Expand Down
30 changes: 6 additions & 24 deletions src/webmentions/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::str::FromStr;

use regex::Regex;
use sqlx::{query_as, query_scalar, PgConnection};
use url::Url;

Expand Down Expand Up @@ -74,32 +73,15 @@ impl FromStr for ObjectType {

pub fn get_object_type_and_id(url: &Url) -> Option<(ObjectType, i32)> {
let path = url.path();
let re = Regex::new(r"^/([^/]+)/(\d+)$").unwrap();
let pieces = path.split('/').collect::<Vec<_>>();

let caps = re.captures(path)?;
let object_type = pieces.get(pieces.len() - 2)?;
let id_str = pieces.last()?;

let object_type = caps.get(1);
let id_str = caps.get(2);
let object_type = ObjectType::from_str(object_type).ok()?;
let id: i32 = FromStr::from_str(id_str).ok()?;

if object_type.is_none() || id_str.is_none() {
return None;
}

let object_type = ObjectType::from_str(object_type.unwrap().as_str());

if object_type.is_err() {
return None;
}

let object_type = object_type.unwrap();

let id: Result<i32, _> = FromStr::from_str(id_str.unwrap().as_str());

if id.is_err() {
return None;
}

Some((object_type, id.unwrap()))
Some((object_type, id))
}

pub async fn mention_exists(source: &str, target: &str, conn: &mut PgConnection) -> bool {
Expand Down

0 comments on commit 9602d4e

Please sign in to comment.