use crate::prefix::EmissionPrefix;
use quote::{format_ident, quote};
#[cfg(test)]
mod kind_tests;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ResourceMode {
#[default]
Block,
Async,
}
impl ResourceMode {
#[inline]
#[must_use]
pub const fn sync(self) -> bool {
matches!(self, Self::Block)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ResourceKind {
Tantivy,
}
impl ResourceKind {
pub const WORDS: &'static [&'static str] = &["tantivy"];
#[inline]
#[must_use]
pub fn from_word(word: &str) -> Option<Self> {
match word {
"tantivy" => Some(Self::Tantivy),
_ => None,
}
}
#[inline]
#[must_use]
pub const fn word(self) -> &'static str {
match self {
Self::Tantivy => "tantivy",
}
}
#[inline]
#[must_use]
pub const fn mode(self) -> ResourceMode {
match self {
Self::Tantivy => ResourceMode::Block,
}
}
#[must_use]
pub fn kinds(self) -> proc_macro2::TokenStream {
match self {
Self::Tantivy => quote!(DOCUMENTS),
}
}
#[must_use]
pub fn blueprint(self, krate: &syn::Path) -> proc_macro2::TokenStream {
match self {
Self::Tantivy => {
let schema = crate::emit::framework(krate, &["transform"], "SlotSchema");
quote!(&'static [#schema])
}
}
}
#[must_use]
pub fn maker(self, krate: &syn::Path, name: &syn::Ident) -> proc_macro2::TokenStream {
match self {
Self::Tantivy => {
let habitat = crate::emit::framework(krate, &["transform"], "Habitat");
let initialize = crate::emit::framework(krate, &["drive"], "Initialize");
let shared = crate::emit::framework(krate, &["resource"], "Shared");
let maker = crate::emit::framework(krate, &["store"], "TantivySlotMaker");
let blueprint = self.blueprint(krate);
quote! {
impl #habitat for #name {
type Blueprint = #blueprint;
}
impl #initialize for #name {
type Visitor = #maker<Self>;
#[inline]
fn initializer(mount: #shared<Self>) -> Self::Visitor {
#maker::new(mount)
}
}
}
}
}
}
#[inline]
#[must_use]
pub const fn gate(self) -> (&'static str, &'static str) {
match self {
Self::Tantivy => ("if_tantivy", "if_no_tantivy"),
}
}
#[must_use]
pub fn absent(self) -> String {
format!(
"`#[ingraph::resource({word})]` needs `ingraph`'s `{word}` feature — the kind word emits this \
engine's own impls, and a build without the engine has no types for them to name. Add \
`features = [\"{word}\"]` to the `ingraph` dependency, or drop the word and write the \
resource's impls by hand (the foreign form).",
word = self.word()
)
}
#[must_use]
pub fn engine_field(self, krate: &syn::Path, prefix: &EmissionPrefix) -> syn::Field {
use syn::parse::Parser as _;
let ty: syn::Type = match self {
Self::Tantivy => syn::parse_quote!(#krate::__private::store::TantivyStore),
};
let name = engine_field(prefix);
syn::Field::parse_named
.parse2(quote!(#name: #ty))
.expect("a named field")
}
#[must_use]
pub fn emit(
self,
krate: &syn::Path,
name: &syn::Ident,
prefix: &EmissionPrefix,
) -> proc_macro2::TokenStream {
let engine = match self {
Self::Tantivy => tantivy(krate, name, self.mode(), prefix),
};
let maker = self.maker(krate, name);
quote! {
#engine
#maker
}
}
}
#[derive(Debug, Clone)]
pub struct ResourceModel {
pub prefix: EmissionPrefix,
pub krate: syn::Path,
pub name: syn::Ident,
pub kind: Option<ResourceKind>,
pub mode: ResourceMode,
}
impl ResourceModel {
#[must_use]
pub fn carried_engine(&self) -> Option<syn::Field> {
self
.kind
.map(|kind| kind.engine_field(&self.krate, &self.prefix))
}
#[must_use]
pub fn emit(&self) -> proc_macro2::TokenStream {
let krate = &self.krate;
let name = &self.name;
let sync = self.mode.sync();
let kinds = self.kind.map(|kind| {
let held = kind.kinds();
quote! {
const KINDS: #krate::__private::resource::ResourceKinds =
#krate::__private::resource::ResourceKinds::#held;
}
});
let citizenship = quote! {
impl #krate::__private::resource::DeclaredResource for #name {
const SYNC: bool = #sync;
#kinds
}
};
match self.kind {
None => citizenship,
Some(kind) => {
let engine = kind.emit(krate, name, &self.prefix);
quote! {
#citizenship
#engine
}
}
}
}
#[must_use]
pub fn expand(
&self,
plain: &proc_macro2::TokenStream,
carrying: &proc_macro2::TokenStream,
) -> proc_macro2::TokenStream {
let emitted = self.emit();
let Some(kind) = self.kind else {
return quote! {
#plain
#emitted
};
};
let krate = &self.krate;
let (open, shut) = kind.gate();
let (open, shut) = (format_ident!("{open}"), format_ident!("{shut}"));
let absent = kind.absent();
quote! {
#krate::__private::gate::#open! {
#carrying
#emitted
}
#krate::__private::gate::#shut! {
#plain
::core::compile_error!(#absent);
}
}
}
}
fn engine_field(prefix: &EmissionPrefix) -> syn::Ident {
prefix.framed_ident("engine")
}
fn tantivy(
krate: &syn::Path,
name: &syn::Ident,
mode: ResourceMode,
prefix: &EmissionPrefix,
) -> proc_macro2::TokenStream {
let engine = engine_field(prefix);
let store = quote!(#krate::__private::store::TantivyStore);
let handle = quote!(#krate::__private::store::TantivyFulltext);
let construction = match mode {
ResourceMode::Block => quote!(#krate::__private::store::FulltextStore),
ResourceMode::Async => quote!(#krate::__private::store::AsyncFulltextStore),
};
let base = quote!(#krate::__private::store::FulltextEngine);
let mount = quote!(#krate::__private::store::FulltextMount);
let bytes = quote!(#krate::__private::store::Utf8Bytes);
let opened = match mode {
ResourceMode::Block => quote! {
::core::future::ready(<Self as #construction>::open(options))
},
ResourceMode::Async => quote!(<Self as #construction>::open(options)),
};
let open = match mode {
ResourceMode::Block => quote! {
#[inline]
fn open(options: Self::Options) -> ::core::result::Result<Self, Self::Error> {
<#store as #construction>::open(options).map(|#engine| Self { #engine })
}
},
ResourceMode::Async => quote! {
#[inline]
fn open(
options: Self::Options,
) -> impl ::core::future::Future<Output = ::core::result::Result<Self, Self::Error>>
+ ::core::marker::Send {
async move {
<#store as #construction>::open(options)
.await
.map(|#engine| Self { #engine })
}
}
},
};
quote! {
impl #name {
#[inline]
#[must_use]
pub fn connection(&self) -> &#store {
&self.#engine
}
}
impl #krate::__private::resource::AsyncResource for #name {
type Options = <#store as #construction>::Options;
type Error = <#store as #construction>::Error;
#[inline]
fn build(
_ctx: &#krate::__private::drive::Resources,
options: Self::Options,
) -> impl ::core::future::Future<Output = ::core::result::Result<Self, Self::Error>>
+ ::core::marker::Send {
#opened
}
}
impl #construction for #name {
type Options = <#store as #construction>::Options;
type Error = <#store as #construction>::Error;
type Handle = <#store as #construction>::Handle;
#open
#[inline]
fn fulltext(&self, slot: impl ::core::convert::Into<#bytes>) -> Self::Handle {
<#store as #construction>::fulltext(&self.#engine, slot)
}
}
impl #mount for #name {
type Handle = <Self as #construction>::Handle;
#[inline]
fn corpus(&self, slot: impl ::core::convert::Into<#bytes>) -> Self::Handle {
<Self as #construction>::fulltext(self, slot)
}
#[inline]
fn connection(&self) -> &<Self::Handle as #base>::Connection {
&self.#engine
}
}
impl #base for #name {
type Options = <#handle as #base>::Options;
type SearchOptions = <#handle as #base>::SearchOptions;
type Error = <#handle as #base>::Error;
type Score = <#handle as #base>::Score;
type Address = <#handle as #base>::Address;
type Connection = <#handle as #base>::Connection;
type WriteBuffer = <#handle as #base>::WriteBuffer;
type ReadBuffer = <#handle as #base>::ReadBuffer;
#[inline]
fn buffer() -> Self::WriteBuffer {
<#handle as #base>::buffer()
}
#[inline]
fn finalize(buf: Self::WriteBuffer) -> Self::Address {
<#handle as #base>::finalize(buf)
}
#[inline]
fn open(address: Self::Address) -> Self::ReadBuffer {
<#handle as #base>::open(address)
}
}
}
}