Using the $crate thing to make the macro usable in and outside tantivy

This commit is contained in:
Paul Masurel
2017-05-22 09:05:11 +09:00
parent 2afa6c372a
commit 7ea5e740e0
2 changed files with 52 additions and 59 deletions

View File

@@ -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
View 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
}
};
);