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

Small improvement to how context! works #79

Merged
merged 1 commit into from Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 32 additions & 6 deletions minijinja/src/context.rs
@@ -1,6 +1,28 @@
#[cfg(test)]
use similar_asserts::assert_eq;

/// Hidden utility module for the [`context!`] macro.
#[doc(hidden)]
pub mod __context {
use crate::key::Key;
use crate::value::{RcType, Value, ValueMap, ValueRepr};

#[inline(always)]
pub fn make() -> ValueMap {
ValueMap::default()
}

#[inline(always)]
pub fn add(ctx: &mut ValueMap, key: &'static str, value: Value) {
ctx.insert(Key::Str(key), value);
}

#[inline(always)]
pub fn build(ctx: ValueMap) -> Value {
ValueRepr::Map(RcType::new(ctx)).into()
}
}

/// Creates a template context with keys and values.
///
/// ```rust
Expand Down Expand Up @@ -37,25 +59,29 @@ use similar_asserts::assert_eq;
/// ```
#[macro_export]
macro_rules! context {
() => {
$crate::__context::build($crate::__context::make())
};
(
$($key:ident $(=> $value:expr)?),* $(,)?
) => {{
let mut ctx = std::collections::BTreeMap::default();
let mut ctx = $crate::__context::make();
$(
$crate::__pair!(ctx, $key $(, $value)?);
$crate::__context_pair!(ctx, $key $(, $value)?);
)*
$crate::value::Value::from(ctx)
$crate::__context::build(ctx)
}}
}

#[macro_export]
#[doc(hidden)]
macro_rules! __pair {
macro_rules! __context_pair {
($ctx:ident, $key:ident) => {{
$crate::__pair!($ctx, $key, $key);
$crate::__context_pair!($ctx, $key, $key);
}};
($ctx:ident, $key:ident, $value:expr) => {
$ctx.insert(
$crate::__context::add(
&mut $ctx,
stringify!($key),
$crate::value::Value::from_serializable(&$value),
);
Expand Down
4 changes: 2 additions & 2 deletions minijinja/src/functions.rs
Expand Up @@ -174,8 +174,8 @@ mod builtins {
step: Option<u32>,
) -> Result<Vec<u32>, Error> {
let rng = match upper {
Some(upper) => (lower..upper),
None => (0..lower),
Some(upper) => lower..upper,
None => 0..lower,
};
Ok(if let Some(step) = step {
rng.step_by(step as usize).collect()
Expand Down
16 changes: 8 additions & 8 deletions minijinja/src/value.rs
Expand Up @@ -95,10 +95,10 @@ pub(crate) type RcType<T> = std::rc::Rc<T>;
const VALUE_HANDLE_MARKER: &str = "\x01__minijinja_ValueHandle";

#[cfg(feature = "preserve_order")]
pub(crate) type ValueMap<K, V> = indexmap::IndexMap<K, V>;
pub(crate) type ValueMap = indexmap::IndexMap<Key<'static>, Value>;

#[cfg(not(feature = "preserve_order"))]
pub(crate) type ValueMap<K, V> = std::collections::BTreeMap<K, V>;
pub(crate) type ValueMap = std::collections::BTreeMap<Key<'static>, Value>;

thread_local! {
static INTERNAL_SERIALIZATION: AtomicBool = AtomicBool::new(false);
Expand Down Expand Up @@ -261,7 +261,7 @@ pub(crate) enum ValueRepr {
SafeString(RcType<String>),
Bytes(RcType<Vec<u8>>),
Seq(RcType<Vec<Value>>),
Map(RcType<ValueMap<Key<'static>, Value>>),
Map(RcType<ValueMap>),
Dynamic(RcType<dyn Object>),
}

Expand Down Expand Up @@ -1470,7 +1470,7 @@ impl ser::SerializeTupleVariant for SerializeTupleVariant {
}

struct SerializeMap {
entries: ValueMap<Key<'static>, Value>,
entries: ValueMap,
key: Option<Key<'static>>,
}

Expand Down Expand Up @@ -1518,7 +1518,7 @@ impl ser::SerializeMap for SerializeMap {

struct SerializeStruct {
name: &'static str,
fields: ValueMap<Key<'static>, Value>,
fields: ValueMap,
}

impl ser::SerializeStruct for SerializeStruct {
Expand Down Expand Up @@ -1555,7 +1555,7 @@ impl ser::SerializeStruct for SerializeStruct {

struct SerializeStructVariant {
variant: &'static str,
map: ValueMap<Key<'static>, Value>,
map: ValueMap,
}

impl ser::SerializeStructVariant for SerializeStructVariant {
Expand Down Expand Up @@ -1613,9 +1613,9 @@ enum ValueIteratorState {
Empty,
Seq(usize, RcType<Vec<Value>>),
#[cfg(not(feature = "preserve_order"))]
Map(Option<Key<'static>>, RcType<ValueMap<Key<'static>, Value>>),
Map(Option<Key<'static>>, RcType<ValueMap>),
#[cfg(feature = "preserve_order")]
Map(usize, RcType<ValueMap<Key<'static>, Value>>),
Map(usize, RcType<ValueMap>),
}

impl ValueIteratorState {
Expand Down