Skip to main content

hexeract_core/
query.rs

1/// A read-only message asking for information.
2///
3/// Each query has exactly one [`QueryHandler`](crate::handler::QueryHandler).
4/// Unlike a [`Command`](crate::command::Command), a query must not mutate
5/// observable state. The framework treats both with the same machinery, but
6/// the distinction is encouraged for clarity.
7///
8/// # Example
9///
10/// ```
11/// use hexeract_core::Query;
12/// use uuid::Uuid;
13///
14/// struct FindUserById {
15///     pub id: Uuid,
16/// }
17///
18/// struct User {
19///     pub id: Uuid,
20///     pub email: String,
21/// }
22///
23/// impl Query for FindUserById {
24///     type Output = Option<User>;
25/// }
26/// ```
27pub trait Query: Send + Sync + 'static {
28    /// The result type returned by the handler upon successful execution.
29    type Output: Send + Sync + 'static;
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    struct DummyQuery;
37    impl Query for DummyQuery {
38        type Output = String;
39    }
40
41    fn assert_query<Q: Query>() {}
42
43    #[test]
44    fn dummy_query_implements_trait() {
45        assert_query::<DummyQuery>();
46    }
47}