Skip to main content

cross_model_search/
lib.rs

1//! Search through several models at once, and get one ordered and paginated list of mixed models.
2//!
3//! This is Cross Model Search for Laravel Rust. Declare an enum with a variant for each model, add
4//! each model's query and the columns to search through, and run the search:
5//!
6//! ```no_run
7//! use cross_model_search::{SearchResult, through};
8//! use serde::Serialize;
9//! # use laravel::model::{HasMany, model};
10//! # #[model] pub struct Post { pub id: i64, pub video_id: Option<i64>, pub title: String, #[has_many] pub comments: HasMany<Comment> }
11//! # #[model] pub struct Comment { pub id: i64, pub post_id: i64, pub body: String }
12//! # #[model] pub struct Video { pub id: i64, pub title: String, pub subtitle: Option<String>, #[has_many] pub posts: HasMany<Post>, #[has_many] pub blogs: HasMany<Blog> }
13//! # #[model] pub struct Blog { pub id: i64, pub video_id: Option<i64>, pub title: String, pub subtitle: String, pub body: String }
14//!
15//! #[derive(SearchResult, Serialize)]
16//! #[serde(tag = "type")]
17//! enum Hit {
18//!     Post(Post),
19//!     Video(Video),
20//! }
21//!
22//! # async fn example() -> laravel::Result<()> {
23//! let hits = Hit::search("laravel eloquent")
24//!     .add_query(Post::query().with(Post::comments), (Post::title, through(Post::comments, Comment::body)))
25//!     .add(Video::title)
26//!     .order_by_desc()
27//!     .paginate(15)
28//!     .await?;
29//!
30//! for hit in hits.items() {
31//!     match hit {
32//!         Hit::Post(post) => println!("post {}", post.title),
33//!         Hit::Video(video) => println!("video {}", video.title),
34//!     }
35//! }
36//! # Ok(()) }
37//! ```
38//!
39//! The documentation in `docs/` covers each feature in Laravel PHP and Laravel Rust.
40
41#![warn(missing_docs)]
42
43mod columns;
44mod error;
45mod result;
46mod search;
47mod sql;
48mod terms;
49
50pub use self::columns::{IntoColumns, SearchColumn, through};
51pub use self::error::{MissingModelError, NoModelsError};
52pub use self::result::{FromModel, Key, SearchResult};
53pub use self::search::{ModelOrder, PaginatedSearch, Search, SimplePaginatedSearch};
54pub use self::terms::parse_terms;
55
56/// Derive [`SearchResult`], [`FromModel`], and a `search` function on an enum whose variants each
57/// hold one model.
58///
59/// ```
60/// use cross_model_search::SearchResult;
61/// # use laravel::model::{HasMany, model};
62/// # #[model] pub struct Post { pub id: i64, pub video_id: Option<i64>, pub title: String, #[has_many] pub comments: HasMany<Comment> }
63/// # #[model] pub struct Comment { pub id: i64, pub post_id: i64, pub body: String }
64/// # #[model] pub struct Video { pub id: i64, pub title: String, pub subtitle: Option<String>, #[has_many] pub posts: HasMany<Post>, #[has_many] pub blogs: HasMany<Blog> }
65/// # #[model] pub struct Blog { pub id: i64, pub video_id: Option<i64>, pub title: String, pub subtitle: String, pub body: String }
66///
67/// #[derive(SearchResult)]
68/// enum Hit {
69///     Post(Post),
70///     Video(Video),
71/// }
72///
73/// let search = Hit::search("howto");
74/// ```
75pub use cross_model_search_macros::SearchResult;
76
77#[doc(hidden)]
78pub mod __private {
79    pub use laravel::Result;
80    pub use laravel::db::{AnyRow, Builder};
81
82    use crate::Key;
83
84    /// The error for a variant number that a `SearchResult` type doesn't have.
85    pub fn unknown_variant<H>(variant: usize) -> Result<Key> {
86        Err(laravel::Error::new(
87            500,
88            UnknownVariant {
89                searchable: std::any::type_name::<H>(),
90                variant,
91            },
92        ))
93    }
94
95    #[derive(Debug, thiserror::Error)]
96    #[error("`{searchable}` has no variant number {variant}")]
97    struct UnknownVariant {
98        searchable: &'static str,
99        variant: usize,
100    }
101}