From fd080b7d836c5a5fa92a69c166c4bfaae3a369fb Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 16 May 2022 02:58:39 +0100 Subject: [PATCH] spec loop detecton: Detect for magic too. Introduce PieceLoadArgs::recursing, and use it in the one current call site. Make the error more general. So now we can call it for magic too. Signed-off-by: Ian Jackson --- src/gamestate.rs | 9 +++++++++ src/pcaliases.rs | 10 ++++------ src/shapelib.rs | 2 +- src/spec.rs | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/gamestate.rs b/src/gamestate.rs index b4200be0..62cd3b39 100644 --- a/src/gamestate.rs +++ b/src/gamestate.rs @@ -411,6 +411,15 @@ impl SpecDepth { } } +impl<'a> PieceLoadArgs<'a> { + #[throws(SpE)] + pub fn recursing(mut self) -> Self { + self.depth = self.depth.increment() + .ok_or_else(|| SpE::ImageOrAliasLoop)?; + self + } +} + #[derive(Error, Debug)] #[error("{self:?}")] pub struct PieceTraitDowncastFailed<'p> { diff --git a/src/pcaliases.rs b/src/pcaliases.rs index 84b0788f..1a2281fc 100644 --- a/src/pcaliases.rs +++ b/src/pcaliases.rs @@ -49,9 +49,7 @@ impl Alias { #[throws(SpecError)] fn new_depth(&self, depth: SpecDepth) -> SpecDepth { - depth.increment().ok_or_else(||{ - SpE::AliasLoop(self.target.clone()) - })? + depth.increment().ok_or_else(|| SpE::ImageOrAliasLoop)? } } @@ -64,9 +62,9 @@ impl PieceSpec for Alias { self.resolve(pcaliases)?.count(&default())? } #[throws(SpecError)] - fn load(&self, PLA { i,gpc,ig,depth,.. }: PLA) -> SpecLoaded { - let r = self.resolve(&ig.pcaliases)? - .load(PLA { i, gpc, ig, depth: self.new_depth(depth)? })?; + fn load(&self, pla: PLA) -> SpecLoaded { + let r = self.resolve(&pla.ig.pcaliases)? + .load(pla.recursing()?)?; r } #[throws(SpecError)] diff --git a/src/shapelib.rs b/src/shapelib.rs index 43150def..669b393d 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -481,7 +481,7 @@ impl PieceSpec for ItemSpec { self.find_load_general( pla.ig, pla.depth, |loaded| Ok(loaded.into()), - |magic| magic.load(pla), + |magic| magic.load(pla.recursing()?) )? } #[throws(SpecError)] diff --git a/src/spec.rs b/src/spec.rs index 07929b0d..31e4eff0 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -89,7 +89,7 @@ pub enum SpecError { #[error("piece alias not found")] AliasNotFound, #[error("piece alias target is multi spec")] AliasTargetMultiSpec, #[error("invalid range ({0} to {1})")] InvalidRange(usize,usize), - #[error("piece alias loop")] AliasLoop(String), + #[error("piece image/alias depth exceeded")] ImageOrAliasLoop, #[error("invalid size scale")] InvalidSizeScale, #[error("multiple faces required")] MultipleFacesRequired, #[error("far too many faces ({0})")] FarTooManyFaces(usize), -- 2.30.2