stellation_bridge/
routines.rs

1//! Bridge Types.
2
3use std::error::Error;
4use std::hash::Hash;
5use std::marker::PhantomData;
6use std::rc::Rc;
7
8use serde::{Deserialize, Serialize};
9
10use crate::error::BridgeError;
11
12#[cold]
13fn panic_network_error(e: BridgeError) -> ! {
14    panic!("failed to communicate with server: {e:?}");
15}
16
17/// A Bridged Query.
18///
19/// This types defines a request that does not incur any side-effect on the server.
20/// This type is cachable and will only resolve once until refreshed.
21pub trait BridgedQuery: Serialize + for<'de> Deserialize<'de> + PartialEq {
22    /// The Query Input.
23    type Input: 'static + Serialize + for<'de> Deserialize<'de> + Hash + Eq + Clone;
24    /// The Query Error Type.
25    type Error: 'static + Serialize + for<'de> Deserialize<'de> + Error + PartialEq + Clone;
26
27    /// Converts a BridgeError into the error type of current query.
28    ///
29    /// # Panics
30    ///
31    /// The default behaviour of a network error is panic.
32    /// Override this method to make the error fallible.
33    #[cold]
34    fn into_query_error(e: BridgeError) -> Self::Error {
35        panic_network_error(e);
36    }
37}
38
39/// The query result type.
40pub type QueryResult<T> = std::result::Result<Rc<T>, <T as BridgedQuery>::Error>;
41
42/// A Bridged Mutation.
43///
44/// This types defines a request that incur side-effects on the server / cannot be cached.
45pub trait BridgedMutation: Serialize + for<'de> Deserialize<'de> + PartialEq {
46    /// The Mutation Input.
47    type Input: 'static + Serialize + for<'de> Deserialize<'de>;
48    /// The Mutation Error.
49    type Error: 'static + Serialize + for<'de> Deserialize<'de> + Error + PartialEq + Clone;
50
51    /// Converts a BridgeError into the error type of current mutation.
52    ///
53    /// # Panics
54    ///
55    /// The default behaviour of a network error is panic.
56    /// Override this method to make the error fallible.
57    #[cold]
58    fn into_mutation_error(e: BridgeError) -> Self::Error {
59        panic_network_error(e);
60    }
61}
62
63/// The mutation result type.
64pub type MutationResult<T> = std::result::Result<Rc<T>, <T as BridgedMutation>::Error>;
65
66/// A placeholder type until never type lands in std.
67#[derive(thiserror::Error, Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
68#[error("this never happens")]
69pub struct Never(PhantomData<()>);