Skip to main content

peer_pressure/
resolve.rs

1//! Async counterparts of [Validate](crate::Validate) and [ValidateFrom](crate::ValidateFrom).
2
3#![allow(async_fn_in_trait)]
4
5/// Async analogue of [Validate](crate::Validate).
6pub trait Resolve: Sized {
7    /// Immutable context of the resolution.
8    ///
9    /// Use `()` for context-free checks.
10    type Context: ?Sized;
11    /// Resolved form of `Self`.
12    type Output;
13    /// Resolution error.
14    type Error;
15
16    /// Resolves `self` in given context, returning either a valid `Output` or `Error`.
17    async fn resolve_in_context(self, ctx: &Self::Context) -> Result<Self::Output, Self::Error>;
18
19    /// Resolves `self`, returning either a valid `Output` or `Error`.
20    ///
21    /// Available only for types with context-free resolution (ie. for which `Context: From<()>`).
22    async fn resolve(self) -> Result<Self::Output, Self::Error>
23    where
24        Self::Context: From<()>,
25    {
26        self.resolve_in_context(&().into()).await
27    }
28}
29
30/// Async analogue of [ValidateFrom](crate::ValidateFrom).
31pub trait ResolveFrom<Raw>: Sized {
32    /// Immutable context of the resolution.
33    ///
34    /// Use `()` for context-free checks.
35    type Context: ?Sized;
36    /// Resolution error.
37    type Error;
38
39    /// Resolves `raw` in given context, returning either a valid `Self` or `Error`.
40    async fn resolve_from_in_context(raw: Raw, ctx: &Self::Context) -> Result<Self, Self::Error>;
41
42    /// Resolves `raw`, returning either a valid `Self` or `Error`.
43    ///
44    /// Available only for types with context-free resolution (ie. for which `Context: From<()>`).
45    async fn resolve_from(raw: Raw) -> Result<Self, Self::Error>
46    where
47        Self::Context: From<()>,
48    {
49        Self::resolve_from_in_context(raw, &().into()).await
50    }
51}