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

Add support for indexmap #527

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
27 changes: 25 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pre-release-replacements = [
[features]
default = ["stdlib"]
stdlib = ["liquid-lib/stdlib"]
indexmap = ["liquid-core/indexmap"]

[dependencies]
doc-comment = "0.3"
Expand Down
2 changes: 2 additions & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ num-traits = "0.2"
pest = "2.0"
pest_derive = "2.0"
regex = "1.5"
indexmap = { version = "2.1.0", optional = true }

# Exposed in API
time = { version = "0.3", default-features = false, features = ["formatting", "macros", "parsing"] }
Expand All @@ -38,3 +39,4 @@ snapbox = "0.4.14"
[features]
default = []
derive = ["liquid-derive"]
indexmap = ["dep:indexmap"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should suffix the feature with a 2 to track that this feature is for v2 (and allow us to support a v3, etc)?

76 changes: 76 additions & 0 deletions crates/core/src/model/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
pub mod map;
mod ser;

#[cfg(feature = "indexmap")]
use indexmap::IndexMap;

use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt;
Expand Down Expand Up @@ -358,6 +361,79 @@ impl<'s, O: ObjectView> fmt::Display for ObjectRender<'s, O> {
}
}

#[cfg(feature = "indexmap")]
impl<K: ObjectIndex, V: ValueView> ValueView for IndexMap<K, V> {
Comment on lines +364 to +365
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put this right after BTreeMaps impl?

fn as_debug(&self) -> &dyn fmt::Debug {
self
}

fn render(&self) -> DisplayCow<'_> {
DisplayCow::Owned(Box::new(ObjectRender { s: self }))
}
fn source(&self) -> DisplayCow<'_> {
DisplayCow::Owned(Box::new(ObjectSource { s: self }))
}
fn type_name(&self) -> &'static str {
"object"
}
fn query_state(&self, state: State) -> bool {
match state {
State::Truthy => true,
State::DefaultValue | State::Empty | State::Blank => self.is_empty(),
}
}

fn to_kstr(&self) -> KStringCow<'_> {
let s = ObjectRender { s: self }.to_string();
KStringCow::from_string(s)
}
fn to_value(&self) -> Value {
Value::Object(
self.iter()
.map(|(k, v)| (crate::model::KString::from_ref(k.as_index()), v.to_value()))
.collect(),
)
}

fn as_object(&self) -> Option<&dyn ObjectView> {
Some(self)
}
}

#[cfg(feature = "indexmap")]
impl<K: ObjectIndex, V: ValueView> ObjectView for IndexMap<K, V> {
fn as_value(&self) -> &dyn ValueView {
self
}

fn size(&self) -> i64 {
self.len() as i64
}

fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = KStringCow<'k>> + 'k> {
let keys = IndexMap::keys(self).map(|s| s.as_index().into());
Box::new(keys)
}

fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn ValueView> + 'k> {
let i = IndexMap::values(self).map(as_view);
Box::new(i)
}

fn iter<'k>(&'k self) -> Box<dyn Iterator<Item = (KStringCow<'k>, &'k dyn ValueView)> + 'k> {
let i = IndexMap::iter(self).map(|(k, v)| (k.as_index().into(), as_view(v)));
Box::new(i)
}

fn contains_key(&self, index: &str) -> bool {
IndexMap::contains_key(self, index)
}

fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> {
IndexMap::get(self, index).map(as_view)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading