Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
26 changes: 21 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,6 @@ assertables = "9.8.6"

[build-dependencies]
prost-build = { version = "0.14.1", optional = true }

[patch.crates-io]
liquid-core = { path = "/Users/jesseditson/code/liquid-rust/crates/core" }
18 changes: 9 additions & 9 deletions src/binary/carriers/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ pub(crate) fn build_payload<F: FileSystemAPI>(
.object_definitions
.get(&name)
.unwrap_or_else(|| panic!("missing object definition {}", name));
let value =
match &entry {
ObjectEntry::List(objects) => Value::array(objects.iter().map(|o| {
o.liquid_object_with(definition, &site.field_config, CARRIER_OPTIONS)
})),
ObjectEntry::Object(o) => {
o.liquid_object_with(definition, &site.field_config, CARRIER_OPTIONS)
}
};
let value = match &entry {
ObjectEntry::List(objects) => Value::array(objects.iter().map(|o| {
o.liquid_object_with(definition, &site.field_config, CARRIER_OPTIONS)
.to_value()
})),
ObjectEntry::Object(o) => o
.liquid_object_with(definition, &site.field_config, CARRIER_OPTIONS)
.to_value(),
};
for object in entry.into_iter() {
collect_uploads(object, &mut uploads);
}
Expand Down
24 changes: 12 additions & 12 deletions src/binary/command/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use crate::{
ExitStatus,
},
file_system_stdlib,
object::ObjectEntry,
object::{
context_value::{ContextObject, ContextValue},
ObjectEntry,
},
page::debug_context,
site::Site,
};
use anyhow::Result;
use clap::ArgMatches;
use liquid_core::Value;
use ordermap::OrderMap;
use std::sync::{atomic::AtomicBool, Arc};

pub struct Command {}
Expand All @@ -34,25 +35,24 @@ impl BinaryCommand for Command {
let root_dir = command_root(args);
let fs = file_system_stdlib::NativeFileSystem::new(&root_dir);
let site = Site::load(&fs, Some(""))?;
let mut objects: OrderMap<String, liquid::model::Value> = OrderMap::new();
let mut objects = ContextObject::new();
let definitions = &site.object_definitions;
for (name, obj_entry) in site.get_objects(&fs)? {
let definition = definitions
.get(&name)
.unwrap_or_else(|| panic!("missing object definition {}", name));
let values = match obj_entry {
ObjectEntry::List(l) => Value::array(
ObjectEntry::List(l) => ContextValue::array(
l.iter()
.map(|o| o.liquid_object(definition, &site.field_config)),
.map(|o| o.liquid_object(definition, &site.field_config).into()),
),
ObjectEntry::Object(o) => o.liquid_object(definition, &site.field_config),
ObjectEntry::Object(o) => o.liquid_object(definition, &site.field_config).into(),
};
objects.insert(name.to_string(), values);
objects.insert(liquid::model::KString::from_string(name.clone()), values);
}
println!(
"{}",
debug_context(&liquid::object!({"objects": objects}), 0)
);
let mut context = ContextObject::new();
context.insert("objects".into(), ContextValue::Object(objects));
println!("{}", debug_context(&context, 0));
// let page = Page::new(
// "objects-template",
// "",
Expand Down
13 changes: 7 additions & 6 deletions src/fields/field_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::object::to_liquid::{object_to_liquid_with, ToLiquidOptions};
use crate::object::Renderable;
use crate::util::integer_decode;
use crate::value_path::ValuePathError;
use crate::{FieldConfig, ObjectDefinition, ValuePath};
use crate::{object::context_value::ContextValue, FieldConfig, ObjectDefinition, ValuePath};
use anyhow::Result;
use comrak::{markdown_to_html, ComrakOptions};
use liquid::{model, ValueView};
Expand Down Expand Up @@ -275,7 +275,7 @@ impl FieldValue {
&self,
definition: &ObjectDefinition,
field_config: &FieldConfig,
) -> model::Value {
) -> ContextValue {
self.typed_objects_with(definition, field_config, ToLiquidOptions::default())
}

Expand All @@ -284,13 +284,13 @@ impl FieldValue {
definition: &ObjectDefinition,
field_config: &FieldConfig,
options: ToLiquidOptions,
) -> model::Value {
) -> ContextValue {
if let FieldValue::Objects(children) = self {
model::Value::Array(
ContextValue::Array(
children
.iter()
.map(|child| {
model::Value::Object(object_to_liquid_with(
ContextValue::Object(object_to_liquid_with(
child,
definition,
field_config,
Expand Down Expand Up @@ -1274,7 +1274,8 @@ pub mod file_tests {
.parse("{% if file %}BLANK{% else %}OH NO!!{% endif %}")
.unwrap();
let field_config = FieldConfig::default();
let ctx = liquid::object!({ "file": file.to_liquid(&field_config) });
let mut ctx = crate::object::context_value::ContextObject::new();
ctx.insert("file".into(), file.to_liquid(&field_config));
assert_eq!(template.render(&ctx).unwrap(), "BLANK");
}
}
Expand Down
52 changes: 49 additions & 3 deletions src/fields/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ impl ValueView for DisplayType {
}
}

#[derive(
Debug, ObjectView, ValueView, Deserialize, Serialize, Clone, PartialEq, PartialOrd, Hash,
)]
#[derive(Debug, ObjectView, Deserialize, Serialize, Clone, PartialEq, PartialOrd, Hash)]
#[cfg_attr(feature = "typescript", derive(typescript_type_def::TypeDef))]
pub struct RenderedFile {
pub display_type: DisplayType,
Expand All @@ -104,6 +102,54 @@ pub struct RenderedFile {
pub description: Option<String>,
pub url: String,
}
/// A file is its url everywhere a template asks for one value - `{{ photo }}`,
/// a string filter, a comparison - while `{{ photo.url }}` and every other key
/// keep working through `as_object`. Rendering it as an object instead spells
/// each key and value into the page run together, which builds, so nothing but
/// the broken page reports it.
///
/// `to_value` stays an object: it is what a value flattens to when liquid has
/// to own it (`{% assign %}`, json for carriers), where dropping the keys would
/// be the worse trade.
impl ValueView for RenderedFile {
fn as_debug(&self) -> &dyn std::fmt::Debug {
self
}
fn render(&self) -> model::DisplayCow<'_> {
model::DisplayCow::Borrowed(&self.url)
}
fn source(&self) -> model::DisplayCow<'_> {
model::DisplayCow::Borrowed(&self.url)
}
fn type_name(&self) -> &'static str {
"object"
}
fn query_state(&self, state: model::State) -> bool {
match state {
model::State::Truthy => true,
model::State::DefaultValue | model::State::Empty | model::State::Blank => {
self.url.is_empty()
}
}
}
fn to_kstr(&self) -> model::KStringCow<'_> {
model::KStringCow::from_ref(&self.url)
}
fn as_scalar(&self) -> Option<model::ScalarCow<'_>> {
Some(model::ScalarCow::new(self.url.as_str()))
}
fn to_value(&self) -> liquid_core::Value {
liquid_core::Value::Object(
ObjectView::iter(self)
.map(|(k, v)| (model::KString::from_string(k.into_string()), v.to_value()))
.collect(),
)
}
fn as_object(&self) -> Option<&dyn ObjectView> {
Some(self)
}
}

impl RenderedFile {
pub fn from_file(file: File, field_config: &FieldConfig) -> Self {
let url = file.url(field_config);
Expand Down
Loading
Loading