From a4ed8551a548128279ef211a43854fdf229f9160 Mon Sep 17 00:00:00 2001 From: Sokhibjon Orzikulov Date: Mon, 19 Jan 2026 13:41:37 +0500 Subject: [PATCH] fix: recursion in template parsing --- src/error.rs | 2 ++ src/manager.rs | 2 +- src/schemes/mod.rs | 10 ++++++++-- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/error.rs b/src/error.rs index c757ec8..d134062 100644 --- a/src/error.rs +++ b/src/error.rs @@ -63,6 +63,8 @@ pub enum BleurError { InvalidRepositoryName(String), #[error("git error: {0}")] GitError(git2::Error), + #[error("brotha, what on earth makes you want collection more than {0} depths?")] + AintNoWayThisDeepCollection(u8), // To be used only if you get despaired. // Until so, don't touch, for the sake of your own sanity! diff --git a/src/manager.rs b/src/manager.rs index a45756c..b6a27de 100644 --- a/src/manager.rs +++ b/src/manager.rs @@ -115,7 +115,7 @@ impl Manager { } pub fn parse(self) -> Result { - let template = Configuration::surely_template(self.temporary.path().to_path_buf())?; + let template = Configuration::surely_template(self.temporary.path().to_path_buf(), 1)?; Ok(Self { template, diff --git a/src/schemes/mod.rs b/src/schemes/mod.rs index 9459128..62bb236 100644 --- a/src/schemes/mod.rs +++ b/src/schemes/mod.rs @@ -6,6 +6,8 @@ use crate::{Error, Result}; use std::fs; use std::path::PathBuf; +static MAX_COLLECTIONS_DEPTH: u8 = 5; + #[derive(Debug, Default, Clone)] pub enum Configuration { // If repo is a single template @@ -44,9 +46,13 @@ impl Configuration { Self::Empty } - pub fn surely_template(path: PathBuf) -> Result { + pub fn surely_template(path: PathBuf, depth: u8) -> Result { use Configuration::*; + if depth > MAX_COLLECTIONS_DEPTH { + return Err(Error::AintNoWayThisDeepCollection(depth)); + } + match Self::parse(path.clone()) { Template(t) => Ok(Self::Template(t)), Empty => Err(Error::NoTemplateConfiguration), @@ -60,7 +66,7 @@ impl Configuration { let option = c.select(option).ok_or(Error::NoSuchTemplateInCollection)?; - Self::surely_template(option.path(path)) + Self::surely_template(option.path(path), depth + 1) } } }