Add a version of prompt::Context that has no fields

This commit is contained in:
Louis Dureuil 2025-07-03 11:08:31 +02:00
parent 0ca652de28
commit dfe0c8664e
No known key found for this signature in database
2 changed files with 26 additions and 11 deletions

View file

@ -6,12 +6,18 @@ use liquid::{ObjectView, ValueView};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Context<'a, D: ObjectView, F: ArrayView> { pub struct Context<'a, D: ObjectView, F: ArrayView> {
document: &'a D, document: &'a D,
fields: &'a F, fields: Option<&'a F>,
} }
impl<'a, D: ObjectView, F: ArrayView> Context<'a, D, F> { impl<'a, D: ObjectView, F: ArrayView> Context<'a, D, F> {
pub fn new(document: &'a D, fields: &'a F) -> Self { pub fn new(document: &'a D, fields: &'a F) -> Self {
Self { document, fields } Self { document, fields: Some(fields) }
}
}
impl<'a, D: ObjectView> Context<'a, D, Vec<bool>> {
pub fn without_fields(document: &'a D) -> Self {
Self { document, fields: None }
} }
} }
@ -21,17 +27,27 @@ impl<D: ObjectView, F: ArrayView> ObjectView for Context<'_, D, F> {
} }
fn size(&self) -> i64 { fn size(&self) -> i64 {
2 if self.fields.is_some() {
2
} else {
1
}
} }
fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = KStringCow<'k>> + 'k> { fn keys<'k>(&'k self) -> Box<dyn Iterator<Item = KStringCow<'k>> + 'k> {
Box::new(["doc", "fields"].iter().map(|s| KStringCow::from_static(s))) let keys = if self.fields.is_some() {
either::Either::Left(["doc", "fields"])
} else {
either::Either::Right(["doc"])
};
Box::new(keys.into_iter().map(KStringCow::from_static))
} }
fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn ValueView> + 'k> { fn values<'k>(&'k self) -> Box<dyn Iterator<Item = &'k dyn ValueView> + 'k> {
Box::new( Box::new(
std::iter::once(self.document.as_value()) std::iter::once(self.document.as_value())
.chain(std::iter::once(self.fields.as_value())), .chain(self.fields.iter().map(|fields| fields.as_value())),
) )
} }
@ -40,13 +56,13 @@ impl<D: ObjectView, F: ArrayView> ObjectView for Context<'_, D, F> {
} }
fn contains_key(&self, index: &str) -> bool { fn contains_key(&self, index: &str) -> bool {
index == "doc" || index == "fields" index == "doc" || (index == "fields" && self.fields.is_some())
} }
fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> { fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> {
match index { match (index, &self.fields) {
"doc" => Some(self.document.as_value()), ("doc", _) => Some(self.document.as_value()),
"fields" => Some(self.fields.as_value()), ("fields", Some(fields)) => Some(fields.as_value()),
_ => None, _ => None,
} }
} }

View file

@ -115,8 +115,7 @@ impl JsonTemplate {
doc_alloc: &'doc Bump, doc_alloc: &'doc Bump,
) -> Result<Value, Error> { ) -> Result<Value, Error> {
let document = ParseableDocument::new(document, doc_alloc); let document = ParseableDocument::new(document, doc_alloc);
let v: Vec<u32> = vec![]; let context = crate::prompt::Context::without_fields(&document);
let context = crate::prompt::Context::new(&document, &v);
self.render(&context) self.render(&context)
} }