diff --git a/crates/core/src/model/find.rs b/crates/core/src/model/find.rs index fb8051c29..ac12759c3 100644 --- a/crates/core/src/model/find.rs +++ b/crates/core/src/model/find.rs @@ -193,37 +193,39 @@ fn augmented_get<'o>(value: &'o dyn ValueView, index: &ScalarCow<'_>) -> Option< /// Find a `ValueView` nested in an `ObjectView` pub fn find<'o>(value: &'o dyn ValueView, path: &[ScalarCow<'_>]) -> Result> { - if let Some(res) = try_find(value, path) { + let first_res = try_find(value, path); + if let Some(res) = first_res { Ok(res) } else { for cur_idx in 1..path.len() { let subpath_end = path.len() - cur_idx; let subpath = &path[0..subpath_end]; - if let Some(parent) = try_find(value, subpath) { - let subpath = itertools::join(subpath.iter().map(ValueView::render), "."); - let requested = &path[subpath_end]; - let available = if let Some(arr) = parent.as_array() { - let mut available = vec![ - KStringCow::from_static("first"), - KStringCow::from_static("last"), - ]; - if 0 < arr.size() { - available - .insert(0, KStringCow::from_string(format!("0..{}", arr.size() - 1))); - } - available - } else if let Some(obj) = parent.as_object() { - let available: Vec<_> = obj.keys().collect(); - available - } else { - Vec::new() - }; - let available = itertools::join(available.iter(), ", "); - return Error::with_msg("Unknown index") - .context("variable", subpath) - .context("requested index", format!("{}", requested.render())) - .context("available indexes", available) - .into_err(); + if let Some(_parent) = try_find(value, subpath) { + // let subpath = itertools::join(subpath.iter().map(ValueView::render), "."); + // let requested = &path[subpath_end]; + // let available = if let Some(arr) = parent.as_array() { + // let mut available = vec![ + // KStringCow::from_static("first"), + // KStringCow::from_static("last"), + // ]; + // if 0 < arr.size() { + // available + // .insert(0, KStringCow::from_string(format!("0..{}", arr.size() - 1))); + // } + // available + // } else if let Some(obj) = parent.as_object() { + // let available: Vec<_> = obj.keys().collect(); + // available + // } else { + // Vec::new() + // }; + // let available = itertools::join(available.iter(), ", "); + return Ok(ValueCow::Owned(Value::Nil)); + // return Error::with_msg("Unknown index") + // .context("variable", subpath) + // .context("requested index", format!("{}", requested.render())) + // .context("available indexes", available) + // .into_err(); } } diff --git a/crates/core/src/runtime/runtime.rs b/crates/core/src/runtime/runtime.rs index 15fb5694e..29acfc74b 100644 --- a/crates/core/src/runtime/runtime.rs +++ b/crates/core/src/runtime/runtime.rs @@ -242,11 +242,8 @@ impl<'g> Runtime for RuntimeCore<'g> { None } - fn get(&self, path: &[ScalarCow<'_>]) -> Result> { - let key = path.first().cloned().unwrap_or_else(|| Scalar::new("nil")); - Error::with_msg("Unknown variable") - .context("requested variable", key.to_kstr()) - .into_err() + fn get(&self, _path: &[ScalarCow<'_>]) -> Result> { + Ok(ValueCow::Owned(Value::Nil)) } fn set_global( diff --git a/crates/core/src/runtime/stack.rs b/crates/core/src/runtime/stack.rs index e4c633539..9c7a62871 100644 --- a/crates/core/src/runtime/stack.rs +++ b/crates/core/src/runtime/stack.rs @@ -59,7 +59,7 @@ impl super::Runtime for StackFrame { fn get(&self, path: &[ScalarCow<'_>]) -> Result> { let key = path.first().ok_or_else(|| { - Error::with_msg("Unknown variable").context("requested variable", "nil") + Error::with_msg("Unknown variable").context("requested variable StackFrame", "nil") })?; let key = key.to_kstr(); let data = &self.data; @@ -135,7 +135,7 @@ impl super::Runtime for GlobalFrame

{ fn get(&self, path: &[ScalarCow<'_>]) -> Result> { let key = path.first().ok_or_else(|| { - Error::with_msg("Unknown variable").context("requested variable", "nil") + Error::with_msg("Unknown variable").context("requested variable GlobalFrame", "nil") })?; let key = key.to_kstr(); let data = self.data.borrow(); @@ -210,7 +210,7 @@ impl super::Runtime for IndexFrame

{ fn get(&self, path: &[ScalarCow<'_>]) -> Result> { let key = path.first().ok_or_else(|| { - Error::with_msg("Unknown variable").context("requested variable", "nil") + Error::with_msg("Unknown variable").context("requested variable IndexFrame", "nil") })?; let key = key.to_kstr(); let data = self.data.borrow(); @@ -298,14 +298,18 @@ impl super::Runtime for SandboxedStackFrame]) -> Result> { let key = path.first().ok_or_else(|| { - Error::with_msg("Unknown variable").context("requested variable", "nil") + Error::with_msg("Unknown variable") + .context("requested variable SandboxedStackFrame 1", "nil") })?; let key = key.to_kstr(); let data = &self.data; data.get(key.as_str()) .and_then(|_| crate::model::try_find(data.as_value(), path)) .map(|v| v.into_owned().into()) - .ok_or_else(|| Error::with_msg("Unknown variable").context("requested variable", key)) + .ok_or_else(|| { + Error::with_msg("Unknown variable") + .context("requested variable SandboxedStackFrame 2", key) + }) } fn set_global( diff --git a/crates/lib/src/jekyll/include_tag.rs b/crates/lib/src/jekyll/include_tag.rs index a789bd660..cd3032293 100644 --- a/crates/lib/src/jekyll/include_tag.rs +++ b/crates/lib/src/jekyll/include_tag.rs @@ -96,7 +96,7 @@ impl Renderable for Include { for (id, val) in &self.vars { let value = val .try_evaluate(runtime) - .ok_or_else(|| Error::with_msg("failed to evaluate value"))? + .ok_or_else(|| Error::with_msg("failed to evaluate value1"))? .into_owned(); helper_vars.insert(id.as_ref(), value); diff --git a/crates/lib/src/stdlib/tags/include_tag.rs b/crates/lib/src/stdlib/tags/include_tag.rs index 7e89011a5..cbfef875a 100644 --- a/crates/lib/src/stdlib/tags/include_tag.rs +++ b/crates/lib/src/stdlib/tags/include_tag.rs @@ -5,6 +5,8 @@ use liquid_core::model::KString; use liquid_core::Expression; use liquid_core::Language; use liquid_core::Renderable; +use liquid_core::Value; +use liquid_core::ValueCow; use liquid_core::ValueView; use liquid_core::{runtime::StackFrame, Runtime}; use liquid_core::{Error, Result}; @@ -100,7 +102,7 @@ impl Renderable for Include { for (id, val) in &self.vars { let value = val .try_evaluate(runtime) - .ok_or_else(|| Error::with_msg("failed to evaluate value"))?; + .unwrap_or_else(|| ValueCow::Owned(Value::Nil)); pass_through.insert(id.as_ref(), value); } diff --git a/crates/lib/src/stdlib/tags/render_tag.rs b/crates/lib/src/stdlib/tags/render_tag.rs index e5d97ab92..083cc8df0 100644 --- a/crates/lib/src/stdlib/tags/render_tag.rs +++ b/crates/lib/src/stdlib/tags/render_tag.rs @@ -175,7 +175,7 @@ impl Renderable for Render { for (id, val) in &self.vars { let value = val .try_evaluate(runtime) - .ok_or_else(|| Error::with_msg("failed to evaluate value"))?; + .ok_or_else(|| Error::with_msg("failed to evaluate value3"))?; root.insert(id.as_ref(), value); } @@ -212,7 +212,7 @@ impl Renderable for Render { for (id, val) in &self.vars { let value = val .try_evaluate(runtime) - .ok_or_else(|| Error::with_msg("failed to evaluate value"))?; + .ok_or_else(|| Error::with_msg("failed to evaluate value4"))?; root.insert(id.as_ref(), value); }