From 95f5f77d186757e9b2ff572c234c09a348d706a5 Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 25 Jun 2025 00:58:30 +0300 Subject: [PATCH] optimize template storage handlers --- README.md | 12 +++++++++++- src/config.rs | 12 +++++++++++- src/session/template.rs | 26 +++++++++++--------------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index f402e3f..a92a030 100644 --- a/README.md +++ b/README.md @@ -42,20 +42,30 @@ nexy -p /path/to/public_dir --template-access-denied Absolute path to the `Access denied` template file + * this template file can be in binary format (e.g. image) + --template-internal-server-error Absolute path to the `Internal server error` template file + * this template file can be in binary format (e.g. image) + --template-not-found Absolute path to the `Not found` template file + * this template file can be in binary format (e.g. image) + --template-welcome - Absolute path to the `Welcome` template file. Unlike `template_index`, this applies only to the `public` location + Absolute path to the `Welcome` template file. Unlike `template-index`, this applies only to the `public` location + + * this template file expects pattern and cannot be in binary format **Patterns** * `{list}` - entries list for the `public` directory --template-index Absolute path to the `Index` template file for each directory + * this template file expects pattern and cannot be in binary format + **Patterns** * `{list}` - entries list for the current directory -r, --read-chunk diff --git a/src/config.rs b/src/config.rs index f798014..f485563 100644 --- a/src/config.rs +++ b/src/config.rs @@ -28,19 +28,27 @@ pub struct Config { pub public: String, /// Absolute path to the `Access denied` template file + /// + /// * this template file can be in binary format (e.g. image) #[arg(long)] pub template_access_denied: Option, /// Absolute path to the `Internal server error` template file + /// + /// * this template file can be in binary format (e.g. image) #[arg(long)] pub template_internal_server_error: Option, /// Absolute path to the `Not found` template file + /// + /// * this template file can be in binary format (e.g. image) #[arg(long)] pub template_not_found: Option, /// Absolute path to the `Welcome` template file. - /// Unlike `template_index`, this applies only to the `public` location + /// Unlike `template-index`, this applies only to the `public` location + /// + /// * this template file expects pattern and cannot be in binary format /// /// **Patterns** /// * `{list}` - entries list for the `public` directory @@ -49,6 +57,8 @@ pub struct Config { /// Absolute path to the `Index` template file for each directory /// + /// * this template file expects pattern and cannot be in binary format + /// /// **Patterns** /// * `{list}` - entries list for the current directory #[arg(long)] diff --git a/src/session/template.rs b/src/session/template.rs index da51a46..40d353f 100644 --- a/src/session/template.rs +++ b/src/session/template.rs @@ -1,21 +1,21 @@ pub struct Template { access_denied: Vec, - index: Vec, + index: String, internal_server_error: Vec, not_found: Vec, - welcome: Vec, + welcome: String, } impl Template { pub fn init(config: &crate::config::Config) -> anyhow::Result { - use std::fs::read; + use std::fs::{read, read_to_string}; Ok(Self { access_denied: match config.template_access_denied { Some(ref p) => read(p)?, None => "Access denied".into(), }, index: match config.template_access_denied { - Some(ref p) => read(p)?, + Some(ref p) => read_to_string(p)?, None => "{list}".into(), }, internal_server_error: match config.template_access_denied { @@ -27,7 +27,7 @@ impl Template { None => "Not found".into(), }, welcome: match config.template_access_denied { - Some(ref p) => read(p)?, + Some(ref p) => read_to_string(p)?, None => "Welcome to Nexy!\n{list}".into(), }, }) @@ -38,11 +38,9 @@ impl Template { } pub fn index(&self, list: Option<&str>) -> Vec { - let l = list.unwrap_or_default(); - match std::str::from_utf8(&self.index) { - Ok(s) => s.replace("{list}", l).into(), - Err(_) => l.into(), - } + self.index + .replace("{list}", list.unwrap_or_default()) + .into() } pub fn internal_server_error(&self) -> &[u8] { @@ -54,10 +52,8 @@ impl Template { } pub fn welcome(&self, list: Option<&str>) -> Vec { - let l = list.unwrap_or_default(); - match std::str::from_utf8(&self.welcome) { - Ok(s) => s.replace("{list}", l).into(), - Err(_) => l.into(), - } + self.welcome + .replace("{list}", list.unwrap_or_default()) + .into() } }