Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/debug errors #3

Draft
wants to merge 25 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 28 additions & 26 deletions crates/core/src/model/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ValueCow<'o>> {
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();
}
}

Expand Down
7 changes: 2 additions & 5 deletions crates/core/src/runtime/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,11 +242,8 @@ impl<'g> Runtime for RuntimeCore<'g> {
None
}

fn get(&self, path: &[ScalarCow<'_>]) -> Result<ValueCow<'_>> {
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<ValueCow<'_>> {
Ok(ValueCow::Owned(Value::Nil))
}

fn set_global(
Expand Down
14 changes: 9 additions & 5 deletions crates/core/src/runtime/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl<P: super::Runtime, O: ObjectView> super::Runtime for StackFrame<P, O> {

fn get(&self, path: &[ScalarCow<'_>]) -> Result<ValueCow<'_>> {
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;
Expand Down Expand Up @@ -135,7 +135,7 @@ impl<P: super::Runtime> super::Runtime for GlobalFrame<P> {

fn get(&self, path: &[ScalarCow<'_>]) -> Result<ValueCow<'_>> {
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();
Expand Down Expand Up @@ -210,7 +210,7 @@ impl<P: super::Runtime> super::Runtime for IndexFrame<P> {

fn get(&self, path: &[ScalarCow<'_>]) -> Result<ValueCow<'_>> {
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();
Expand Down Expand Up @@ -298,14 +298,18 @@ impl<P: super::Runtime, O: ObjectView> super::Runtime for SandboxedStackFrame<P,

fn get(&self, path: &[ScalarCow<'_>]) -> Result<ValueCow<'_>> {
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(
Expand Down
2 changes: 1 addition & 1 deletion crates/lib/src/jekyll/include_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion crates/lib/src/stdlib/tags/include_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/stdlib/tags/render_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Loading