Skip to content

Commit

Permalink
refactors: simplify builder
Browse files Browse the repository at this point in the history
  • Loading branch information
SteelAlloy committed Sep 17, 2024
1 parent 1f0e443 commit 5d2bcd6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 52 deletions.
34 changes: 34 additions & 0 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Display;

use crate::{
geometry::{Coords, Rect},
server::Server,
Expand Down Expand Up @@ -43,6 +45,38 @@ pub enum FitIn {
Full,
}

struct Smart;

impl Display for Smart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "smart")
}
}

struct Filters<'a>(&'a [Filter]);

impl<'a> Filters<'a> {
fn new(filters: &'a [Filter]) -> Option<Self> {
if filters.is_empty() {
None
} else {
Some(Self(filters))
}
}
}

impl Display for Filters<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let filters = self
.0
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(":");
write!(f, "filters:{}", filters)
}
}

#[derive(strum::Display)]
pub enum ResponseMode {
/// The metadata endpoint has **ALL** the options that the image one has,
Expand Down
75 changes: 23 additions & 52 deletions src/endpoint/builder.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,29 @@
use super::Endpoint;
use super::{Endpoint, Filters, Smart};
use crate::server::Security;
use base64ct::{Base64Url, Encoding};
use hmac::Mac;

impl Endpoint {
fn build_path(&self, image_uri: &str) -> String {
let mut path = vec![];

if let Some(resp) = &self.response {
path.push(resp.to_string());
}

if let Some(orientation) = &self.trim {
path.push(orientation.to_string());
}

if let Some(crop) = &self.crop {
path.push(crop.to_string());
}

if let Some(fit_in) = &self.fit_in {
path.push(fit_in.to_string());
}

if let Some(resize) = &self.resize {
path.push(resize.to_string());
}

if let Some(h_align) = &self.h_align {
path.push(h_align.to_string());
}

if let Some(v_align) = &self.v_align {
path.push(v_align.to_string());
}

if self.smart {
path.push("smart".to_string());
}

if !self.filters.is_empty() {
let filters = self
.filters
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(":");

path.push(format!("filters:{filters}"));
}

path.push(image_uri.to_owned());

path.join("/")
fn build_path(&self, image_uri: impl ToString) -> String {
let smart = self.smart.then_some(Smart);
let filters = Filters::new(&self.filters);

[
self.response.as_ref().map(ToString::to_string),
self.trim.as_ref().map(ToString::to_string),
self.crop.as_ref().map(ToString::to_string),
self.fit_in.as_ref().map(ToString::to_string),
self.resize.as_ref().map(ToString::to_string),
self.h_align.as_ref().map(ToString::to_string),
self.v_align.as_ref().map(ToString::to_string),
smart.as_ref().map(ToString::to_string),
filters.as_ref().map(ToString::to_string),
Some(image_uri.to_string()),
]
.into_iter()
.flatten()
.collect::<Vec<_>>()
.join("/")
}

/// ```
Expand All @@ -64,7 +35,7 @@ impl Endpoint {
///
/// assert_eq!(path, "/unsafe/path/to/my/image.jpg");
/// ```
pub fn to_path(&self, image_uri: &str) -> String {
pub fn to_path(&self, image_uri: impl ToString) -> String {
let path = self.build_path(image_uri);

let security = match &self.server.security {
Expand All @@ -90,7 +61,7 @@ impl Endpoint {
///
/// assert_eq!(path, "http://localhost:8888/unsafe/path/to/my/image.jpg");
/// ```
pub fn to_url(&self, image_uri: &str) -> String {
pub fn to_url(&self, image_uri: impl ToString) -> String {
format!("{}{}", self.server.origin, self.to_path(image_uri))
}
}

0 comments on commit 5d2bcd6

Please sign in to comment.