From 3b4d944291961cdc93131ad196b1e513da97d00f Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 18 Feb 2022 22:54:22 +0530 Subject: [PATCH] feat: Gist::get_file_route utility method to substitute values in get file routes --- src/api/v1/routes.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/api/v1/routes.rs b/src/api/v1/routes.rs index 419d5dc..8a834cf 100644 --- a/src/api/v1/routes.rs +++ b/src/api/v1/routes.rs @@ -16,6 +16,7 @@ */ //! V1 API Routes use actix_auth_middleware::{Authentication, GetLoginRoute}; +use serde::*; use super::meta::routes::Meta; @@ -44,17 +45,37 @@ impl Auth { } } } +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct GetFilePath { + pub username: String, + pub gist: String, + pub file: String, +} /// Authentication routes pub struct Gist { /// logout route pub new: &'static str, + + /// get fie route + pub get_file: &'static str, } + impl Gist { /// create new instance of Authentication route pub const fn new() -> Gist { let new = "/api/v1/gist/new"; - Gist { new } + let get_file = "/api/v1/gist/profile/{username}/{gist}/{file}"; + Gist { new, get_file } + } + + /// get file routes with placeholders replaced with values provided. + /// filename is auto-escaped using [urlencoding::encode] + pub fn get_file_route(&self, components: &GetFilePath) -> String { + self.get_file + .replace("{username}", &components.username) + .replace("{gist}", &components.gist) + .replace("{file}", &urlencoding::encode(&components.file)) } }