mirror of
https://github.com/quickwit-oss/tantivy.git
synced 2026-01-07 17:42:55 +00:00
Using the $crate thing to make the macro usable in and outside tantivy
This commit is contained in:
62
src/lib.rs
62
src/lib.rs
@@ -78,66 +78,10 @@ extern crate rand;
|
||||
mod functional_test;
|
||||
|
||||
|
||||
|
||||
|
||||
#[macro_use]
|
||||
mod macros_use {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get(
|
||||
($e:expr) => (match $e { Some(e) => e, None => return None })
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! doc(
|
||||
() => {
|
||||
{
|
||||
use Document;
|
||||
(Document::default())
|
||||
}
|
||||
}; // avoids a warning due to the useless `mut`.
|
||||
($($field:ident => $value:expr),*) => {
|
||||
{
|
||||
use Document;
|
||||
use schema::FieldValue;
|
||||
|
||||
let mut document = Document::default();
|
||||
$(
|
||||
document.add(FieldValue::new($field, $value.into()));
|
||||
)*
|
||||
document
|
||||
}
|
||||
};
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
mod macros {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get(
|
||||
($e:expr) => (match $e { Some(e) => e, None => return None })
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! doc(
|
||||
() => {
|
||||
{
|
||||
use tantivy::Document;
|
||||
(Document::default())
|
||||
}
|
||||
}; // avoids a warning due to the useless `mut`.
|
||||
($($field:ident => $value:expr),*) => {
|
||||
{
|
||||
use tantivy::Document;
|
||||
use tantivy::schema::FieldValue;
|
||||
let mut document = Document::default();
|
||||
$(
|
||||
document.add(FieldValue::new($field, $value.into()));
|
||||
)*
|
||||
document
|
||||
}
|
||||
};
|
||||
);
|
||||
}
|
||||
mod macros;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
|
||||
49
src/macros.rs
Normal file
49
src/macros.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
|
||||
macro_rules! get(
|
||||
($e:expr) => (match $e { Some(e) => e, None => return None })
|
||||
);
|
||||
|
||||
|
||||
/// `doc!` is a shortcut that helps building `Document`
|
||||
/// object, assuming you have the field object.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// #[macro_use]
|
||||
/// extern crate tantivy;
|
||||
///
|
||||
/// use tantivy::schema::{SchemaBuilder, TEXT, FAST};
|
||||
///
|
||||
/// //...
|
||||
///
|
||||
/// # fn main() {
|
||||
/// let mut schema_builder = SchemaBuilder::new();
|
||||
/// let title = schema_builder.add_text_field("title", TEXT);
|
||||
/// let author = schema_builder.add_text_field("text", TEXT);
|
||||
/// let likes = schema_builder.add_u64_field("num_u64", FAST);
|
||||
/// let schema = schema_builder.build();
|
||||
/// let doc = doc!(
|
||||
/// title => "Life Aquatic",
|
||||
/// author => "Wes Anderson",
|
||||
/// likes => 4u64
|
||||
/// );
|
||||
/// # }
|
||||
/// ```
|
||||
#[macro_export]
|
||||
macro_rules! doc(
|
||||
() => {
|
||||
{
|
||||
($crate::Document::default())
|
||||
}
|
||||
}; // avoids a warning due to the useless `mut`.
|
||||
($($field:ident => $value:expr),*) => {
|
||||
{
|
||||
let mut document = $crate::Document::default();
|
||||
$(
|
||||
document.add($crate::schema::FieldValue::new($field, $value.into()));
|
||||
)*
|
||||
document
|
||||
}
|
||||
};
|
||||
);
|
||||
Reference in New Issue
Block a user