From b007ed6be91d05173c081d94029d321330926018 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Tue, 27 May 2025 10:04:14 +0200 Subject: [PATCH 1/2] Remove TemplateChecker --- crates/milli/src/prompt/error.rs | 2 + crates/milli/src/prompt/mod.rs | 8 +- crates/milli/src/prompt/template_checker.rs | 301 -------------------- 3 files changed, 4 insertions(+), 307 deletions(-) delete mode 100644 crates/milli/src/prompt/template_checker.rs diff --git a/crates/milli/src/prompt/error.rs b/crates/milli/src/prompt/error.rs index a92e2fdc3..a21031ff1 100644 --- a/crates/milli/src/prompt/error.rs +++ b/crates/milli/src/prompt/error.rs @@ -18,6 +18,7 @@ impl NewPromptError { Self { kind: NewPromptErrorKind::CannotParseTemplate(inner), fault: FaultSource::User } } + #[allow(unused)] pub(crate) fn invalid_fields_in_template(inner: liquid::Error) -> NewPromptError { Self { kind: NewPromptErrorKind::InvalidFieldsInTemplate(inner), fault: FaultSource::User } } @@ -27,6 +28,7 @@ impl NewPromptError { pub enum NewPromptErrorKind { #[error("cannot parse template: {0}")] CannotParseTemplate(liquid::Error), + #[allow(unused)] #[error("template contains invalid fields: {0}. Only `doc.*`, `fields[i].name`, `fields[i].value` are supported")] InvalidFieldsInTemplate(liquid::Error), } diff --git a/crates/milli/src/prompt/mod.rs b/crates/milli/src/prompt/mod.rs index a5cb8de48..c24069c00 100644 --- a/crates/milli/src/prompt/mod.rs +++ b/crates/milli/src/prompt/mod.rs @@ -2,7 +2,6 @@ mod context; mod document; pub(crate) mod error; mod fields; -mod template_checker; use std::cell::RefCell; use std::convert::TryFrom; @@ -105,11 +104,6 @@ impl Prompt { max_bytes, }; - // render template with special object that's OK with `doc.*` and `fields.*` - this.template - .render(&template_checker::TemplateChecker) - .map_err(NewPromptError::invalid_fields_in_template)?; - Ok(this) } @@ -206,6 +200,7 @@ mod test { } #[test] + #[ignore] fn template_missing_doc() { assert!(matches!( Prompt::new("{{title}}: {{overview}}".into(), None), @@ -236,6 +231,7 @@ mod test { } #[test] + #[ignore] fn template_fields_invalid() { assert!(matches!( // intentionally garbled field diff --git a/crates/milli/src/prompt/template_checker.rs b/crates/milli/src/prompt/template_checker.rs deleted file mode 100644 index 4cda4a70d..000000000 --- a/crates/milli/src/prompt/template_checker.rs +++ /dev/null @@ -1,301 +0,0 @@ -use liquid::model::{ - ArrayView, DisplayCow, KStringCow, ObjectRender, ObjectSource, State, Value as LiquidValue, -}; -use liquid::{Object, ObjectView, ValueView}; - -#[derive(Debug)] -pub struct TemplateChecker; - -#[derive(Debug)] -pub struct DummyDoc; - -#[derive(Debug)] -pub struct DummyFields; - -#[derive(Debug)] -pub struct DummyField; - -const DUMMY_VALUE: &LiquidValue = &LiquidValue::Nil; - -impl ObjectView for DummyField { - fn as_value(&self) -> &dyn ValueView { - self - } - - fn size(&self) -> i64 { - 2 - } - - fn keys<'k>(&'k self) -> Box> + 'k> { - Box::new(["name", "value"].iter().map(|s| KStringCow::from_static(s))) - } - - fn values<'k>(&'k self) -> Box + 'k> { - Box::new(vec![DUMMY_VALUE.as_view(), DUMMY_VALUE.as_view()].into_iter()) - } - - fn iter<'k>(&'k self) -> Box, &'k dyn ValueView)> + 'k> { - Box::new(self.keys().zip(self.values())) - } - - fn contains_key(&self, index: &str) -> bool { - index == "name" || index == "value" - } - - fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> { - if self.contains_key(index) { - Some(DUMMY_VALUE.as_view()) - } else { - None - } - } -} - -impl ValueView for DummyField { - fn as_debug(&self) -> &dyn std::fmt::Debug { - self - } - - fn render(&self) -> DisplayCow<'_> { - DUMMY_VALUE.render() - } - - fn source(&self) -> DisplayCow<'_> { - DUMMY_VALUE.source() - } - - fn type_name(&self) -> &'static str { - "object" - } - - fn query_state(&self, state: State) -> bool { - match state { - State::Truthy => true, - State::DefaultValue => false, - State::Empty => false, - State::Blank => false, - } - } - - fn to_kstr(&self) -> KStringCow<'_> { - DUMMY_VALUE.to_kstr() - } - - fn to_value(&self) -> LiquidValue { - let mut this = Object::new(); - this.insert("name".into(), LiquidValue::Nil); - this.insert("value".into(), LiquidValue::Nil); - LiquidValue::Object(this) - } - - fn as_object(&self) -> Option<&dyn ObjectView> { - Some(self) - } -} - -impl ValueView for DummyFields { - fn as_debug(&self) -> &dyn std::fmt::Debug { - self - } - - fn render(&self) -> DisplayCow<'_> { - DUMMY_VALUE.render() - } - - fn source(&self) -> DisplayCow<'_> { - DUMMY_VALUE.source() - } - - fn type_name(&self) -> &'static str { - "array" - } - - fn query_state(&self, state: State) -> bool { - match state { - State::Truthy => true, - State::DefaultValue => false, - State::Empty => false, - State::Blank => false, - } - } - - fn to_kstr(&self) -> KStringCow<'_> { - DUMMY_VALUE.to_kstr() - } - - fn to_value(&self) -> LiquidValue { - LiquidValue::Array(vec![DummyField.to_value()]) - } - - fn as_array(&self) -> Option<&dyn ArrayView> { - Some(self) - } -} - -impl ArrayView for DummyFields { - fn as_value(&self) -> &dyn ValueView { - self - } - - fn size(&self) -> i64 { - u16::MAX as i64 - } - - fn values<'k>(&'k self) -> Box + 'k> { - Box::new(std::iter::once(DummyField.as_value())) - } - - fn contains_key(&self, index: i64) -> bool { - index < self.size() - } - - fn get(&self, _index: i64) -> Option<&dyn ValueView> { - Some(DummyField.as_value()) - } -} - -impl ObjectView for DummyDoc { - fn as_value(&self) -> &dyn ValueView { - self - } - - fn size(&self) -> i64 { - 1000 - } - - fn keys<'k>(&'k self) -> Box> + 'k> { - Box::new(std::iter::empty()) - } - - fn values<'k>(&'k self) -> Box + 'k> { - Box::new(std::iter::empty()) - } - - fn iter<'k>(&'k self) -> Box, &'k dyn ValueView)> + 'k> { - Box::new(std::iter::empty()) - } - - fn contains_key(&self, _index: &str) -> bool { - true - } - - fn get<'s>(&'s self, _index: &str) -> Option<&'s dyn ValueView> { - // Recursively sends itself - Some(self) - } -} - -impl ValueView for DummyDoc { - fn as_debug(&self) -> &dyn std::fmt::Debug { - self - } - - fn render(&self) -> DisplayCow<'_> { - DUMMY_VALUE.render() - } - - fn source(&self) -> DisplayCow<'_> { - DUMMY_VALUE.source() - } - - fn type_name(&self) -> &'static str { - "object" - } - - fn query_state(&self, state: State) -> bool { - match state { - State::Truthy => true, - State::DefaultValue => false, - State::Empty => false, - State::Blank => false, - } - } - - fn to_kstr(&self) -> KStringCow<'_> { - DUMMY_VALUE.to_kstr() - } - - fn to_value(&self) -> LiquidValue { - LiquidValue::Nil - } - - fn as_object(&self) -> Option<&dyn ObjectView> { - Some(self) - } -} - -impl ObjectView for TemplateChecker { - fn as_value(&self) -> &dyn ValueView { - self - } - - fn size(&self) -> i64 { - 2 - } - - fn keys<'k>(&'k self) -> Box> + 'k> { - Box::new(["doc", "fields"].iter().map(|s| KStringCow::from_static(s))) - } - - fn values<'k>(&'k self) -> Box + 'k> { - Box::new( - std::iter::once(DummyDoc.as_value()).chain(std::iter::once(DummyFields.as_value())), - ) - } - - fn iter<'k>(&'k self) -> Box, &'k dyn ValueView)> + 'k> { - Box::new(self.keys().zip(self.values())) - } - - fn contains_key(&self, index: &str) -> bool { - index == "doc" || index == "fields" - } - - fn get<'s>(&'s self, index: &str) -> Option<&'s dyn ValueView> { - match index { - "doc" => Some(DummyDoc.as_value()), - "fields" => Some(DummyFields.as_value()), - _ => None, - } - } -} - -impl ValueView for TemplateChecker { - fn as_debug(&self) -> &dyn std::fmt::Debug { - self - } - - fn render(&self) -> liquid::model::DisplayCow<'_> { - DisplayCow::Owned(Box::new(ObjectRender::new(self))) - } - - fn source(&self) -> liquid::model::DisplayCow<'_> { - DisplayCow::Owned(Box::new(ObjectSource::new(self))) - } - - fn type_name(&self) -> &'static str { - "object" - } - - fn query_state(&self, state: liquid::model::State) -> bool { - match state { - State::Truthy => true, - State::DefaultValue | State::Empty | State::Blank => false, - } - } - - fn to_kstr(&self) -> liquid::model::KStringCow<'_> { - let s = ObjectRender::new(self).to_string(); - KStringCow::from_string(s) - } - - fn to_value(&self) -> LiquidValue { - LiquidValue::Object( - self.iter().map(|(k, x)| (k.to_string().into(), x.to_value())).collect(), - ) - } - - fn as_object(&self) -> Option<&dyn ObjectView> { - Some(self) - } -} From 5810fb239ffdf8cf8b72d12000e3b3ddd246ac23 Mon Sep 17 00:00:00 2001 From: Louis Dureuil Date: Tue, 27 May 2025 10:24:04 +0200 Subject: [PATCH 2/2] Reference PR in comments --- crates/milli/src/prompt/error.rs | 4 ++-- crates/milli/src/prompt/mod.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/milli/src/prompt/error.rs b/crates/milli/src/prompt/error.rs index a21031ff1..03f3fb8a8 100644 --- a/crates/milli/src/prompt/error.rs +++ b/crates/milli/src/prompt/error.rs @@ -18,7 +18,7 @@ impl NewPromptError { Self { kind: NewPromptErrorKind::CannotParseTemplate(inner), fault: FaultSource::User } } - #[allow(unused)] + #[allow(unused)] // See for explanation pub(crate) fn invalid_fields_in_template(inner: liquid::Error) -> NewPromptError { Self { kind: NewPromptErrorKind::InvalidFieldsInTemplate(inner), fault: FaultSource::User } } @@ -28,7 +28,7 @@ impl NewPromptError { pub enum NewPromptErrorKind { #[error("cannot parse template: {0}")] CannotParseTemplate(liquid::Error), - #[allow(unused)] + #[allow(unused)] // See for explanation #[error("template contains invalid fields: {0}. Only `doc.*`, `fields[i].name`, `fields[i].value` are supported")] InvalidFieldsInTemplate(liquid::Error), } diff --git a/crates/milli/src/prompt/mod.rs b/crates/milli/src/prompt/mod.rs index c24069c00..d40fcd27f 100644 --- a/crates/milli/src/prompt/mod.rs +++ b/crates/milli/src/prompt/mod.rs @@ -200,7 +200,7 @@ mod test { } #[test] - #[ignore] + #[ignore] // See for explanation fn template_missing_doc() { assert!(matches!( Prompt::new("{{title}}: {{overview}}".into(), None), @@ -231,7 +231,7 @@ mod test { } #[test] - #[ignore] + #[ignore] // See for explanation fn template_fields_invalid() { assert!(matches!( // intentionally garbled field