From 644bbf99145042266ea8cf2890cef5771bd04be4 Mon Sep 17 00:00:00 2001 From: MALO <57839069+MDLC01@users.noreply.github.com> Date: Tue, 30 May 2023 10:13:27 +0200 Subject: Add `fields` method to content (#1340) --- src/eval/methods.rs | 9 ++++++++- src/model/content.rs | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/eval/methods.rs b/src/eval/methods.rs index 7de3bc20..f57bf84d 100644 --- a/src/eval/methods.rs +++ b/src/eval/methods.rs @@ -81,6 +81,7 @@ pub fn call( "at" => content .at(&args.expect::("field")?, args.named("default")?) .at(span)?, + "fields" => Value::Dict(content.dict()), "location" => content .location() .ok_or("this method can only be called on content returned by query(..)") @@ -332,7 +333,13 @@ pub fn methods_on(type_name: &str) -> &[(&'static str, bool)] { ("starts-with", true), ("trim", true), ], - "content" => &[("func", false), ("has", true), ("at", true), ("location", false)], + "content" => &[ + ("func", false), + ("has", true), + ("at", true), + ("fields", false), + ("location", false), + ], "array" => &[ ("all", true), ("any", true), diff --git a/src/model/content.rs b/src/model/content.rs index 8b60ab44..f262d027 100644 --- a/src/model/content.rs +++ b/src/model/content.rs @@ -12,7 +12,7 @@ use super::{ }; use crate::diag::{SourceResult, StrResult}; use crate::doc::Meta; -use crate::eval::{Cast, Str, Value, Vm}; +use crate::eval::{Cast, Dict, Str, Value, Vm}; use crate::syntax::Span; use crate::util::pretty_array_like; @@ -251,6 +251,13 @@ impl Content { .ok_or_else(|| missing_field_no_default(field)) } + /// Return the fields of the content as a dict. + pub fn dict(&self) -> Dict { + self.fields() + .map(|(key, value)| (key.to_owned().into(), value)) + .collect() + } + /// The content's label. pub fn label(&self) -> Option<&Label> { match self.field_ref("label")? { -- cgit v1.2.3