diff --git a/examples/date_time_field.rs b/examples/date_time_field.rs index 13d37a39f..d508bc80f 100644 --- a/examples/date_time_field.rs +++ b/examples/date_time_field.rs @@ -4,7 +4,7 @@ use tantivy::collector::TopDocs; use tantivy::query::QueryParser; -use tantivy::schema::{DateOptions, OwnedValue, Schema, INDEXED, STORED, STRING}; +use tantivy::schema::{DateOptions, Document, OwnedValue, Schema, INDEXED, STORED, STRING}; use tantivy::{Index, IndexWriter, TantivyDocument}; fn main() -> tantivy::Result<()> { diff --git a/src/indexer/segment_writer.rs b/src/indexer/segment_writer.rs index aac738d4d..214d3ded4 100644 --- a/src/indexer/segment_writer.rs +++ b/src/indexer/segment_writer.rs @@ -494,7 +494,8 @@ mod tests { use crate::query::PhraseQuery; use crate::schema::document::Value; use crate::schema::{ - IndexRecordOption, Schema, TextFieldIndexing, TextOptions, Type, STORED, STRING, TEXT, + Document, IndexRecordOption, Schema, TextFieldIndexing, TextOptions, Type, STORED, STRING, + TEXT, }; use crate::store::{Compressor, StoreReader, StoreWriter}; use crate::time::format_description::well_known::Rfc3339; diff --git a/src/schema/document/default_doc_type.rs b/src/schema/document/default_doc_type.rs index 4f8ff6302..7e1469439 100644 --- a/src/schema/document/default_doc_type.rs +++ b/src/schema/document/default_doc_type.rs @@ -195,14 +195,6 @@ impl TantivyDocument { Ok(document) } - /// Encode the schema in JSON. - /// - /// Encoding a document cannot fail. - pub fn to_json(&self, schema: &Schema) -> String { - serde_json::to_string(&self.to_named_doc(schema)) - .expect("doc encoding failed. This is a bug") - } - /// Build a document object from a json-object. pub fn parse_json(schema: &Schema, doc_json: &str) -> Result { let json_obj: Map = diff --git a/src/schema/document/mod.rs b/src/schema/document/mod.rs index f0cdde67c..0a7b085c8 100644 --- a/src/schema/document/mod.rs +++ b/src/schema/document/mod.rs @@ -235,6 +235,14 @@ pub trait Document: DocumentDeserialize + Send + Sync + 'static { } NamedFieldDocument(field_map) } + + /// Encode the doc in JSON. + /// + /// Encoding a document cannot fail. + fn to_json(&self, schema: &Schema) -> String { + serde_json::to_string(&self.to_named_doc(schema)) + .expect("doc encoding failed. This is a bug") + } } /// A single field value. @@ -357,6 +365,26 @@ pub trait Value<'a>: Send + Sync + Debug { } } + #[inline] + /// Returns the iterator over the array if the Value is an array. + fn as_array(&self) -> Option { + if let ReferenceValue::Array(val) = self.as_value() { + Some(val) + } else { + None + } + } + + #[inline] + /// Returns the iterator over the object if the Value is an object. + fn as_object(&self) -> Option { + if let ReferenceValue::Object(val) = self.as_value() { + Some(val) + } else { + None + } + } + #[inline] /// Returns true if the Value is an array. fn is_array(&self) -> bool { diff --git a/src/schema/field_type.rs b/src/schema/field_type.rs index e4c36b8f0..a419f6835 100644 --- a/src/schema/field_type.rs +++ b/src/schema/field_type.rs @@ -535,7 +535,9 @@ mod tests { use super::FieldType; use crate::schema::field_type::ValueParsingError; - use crate::schema::{NumericOptions, OwnedValue, Schema, TextOptions, Type, COERCE, INDEXED}; + use crate::schema::{ + Document, NumericOptions, OwnedValue, Schema, TextOptions, Type, COERCE, INDEXED, + }; use crate::time::{Date, Month, PrimitiveDateTime, Time}; use crate::tokenizer::{PreTokenizedString, Token}; use crate::{DateTime, TantivyDocument}; diff --git a/src/schema/value.rs b/src/schema/value.rs index 48b978e4a..a15946209 100644 --- a/src/schema/value.rs +++ b/src/schema/value.rs @@ -49,6 +49,13 @@ pub enum OwnedValue { IpAddr(Ipv6Addr), } +impl AsRef for OwnedValue { + #[inline] + fn as_ref(&self) -> &OwnedValue { + self + } +} + impl<'a> Value<'a> for &'a OwnedValue { type ChildValue = Self; type ArrayIter = ArrayIter<'a>; @@ -441,11 +448,11 @@ impl<'a> Iterator for ObjectMapIter<'a> { #[cfg(test)] mod tests { - use super::OwnedValue; + use super::*; use crate::schema::{BytesOptions, Schema}; use crate::time::format_description::well_known::Rfc3339; use crate::time::OffsetDateTime; - use crate::{DateTime, TantivyDocument}; + use crate::{DateTime, Document, TantivyDocument}; #[test] fn test_parse_bytes_doc() {