async_try_from/
lib.rs

1use std::path::Path;
2use async_trait::async_trait;
3
4// ---------------- [ File: src/async_traits.rs ]
5
6pub trait MaybeThrow {
7
8    type Error;
9
10    fn maybe_throw(&self) -> Result<(),Self::Error>;
11}
12
13/// Trait for creating instances asynchronously with input `X`
14#[async_trait]
15pub trait AsyncTryFrom<X> {
16    type Error;
17
18    async fn new(input: &X) -> Result<Self, Self::Error>
19    where
20        Self: Sized; // Ensure Self can be returned
21}
22
23/// Trait that combines async creation with integrity validation
24#[async_trait]
25pub trait AsyncCreateWithAndValidate<X>:
26    Sized 
27    + AsyncTryFrom<X> 
28    + ValidateIntegrity<Error=<Self as AsyncTryFrom<X>>::Error> // Ensure same error type
29    where for<'async_trait> X: Send + Sync + 'async_trait
30{
31    // Automatically forward the error type from AsyncTryFrom
32    async fn new_and_validate(input: &X) -> Result<Self, <Self as AsyncTryFrom<X>>::Error> {
33        let instance = Self::new(&input).await?;
34        instance.validate_integrity().await?; // Validation
35        Ok(instance)
36    }
37}
38
39// Blanket implementation of `AsyncCreateWithAndValidate` for types that implement
40// both `AsyncTryFrom` and `ValidateIntegrity`
41impl<X, T> AsyncCreateWithAndValidate<X> for T
42where
43    for<'async_trait> X: Send + Sync + 'async_trait,
44    T: AsyncTryFrom<X> + ValidateIntegrity<Error=<T as AsyncTryFrom<X>>::Error>, // Ensure error type consistency
45{
46    // The methods from `AsyncCreateWithAndValidate` can be used here
47}
48
49#[async_trait]
50pub trait AsyncPathValidator {
51
52    async fn is_valid(path: &Path) -> bool;
53}
54
55#[async_trait]
56pub trait AsyncFindItems {
57
58    type Item;
59    type Error;
60
61    /// Asynchronously finds all the crates in the workspace
62    async fn find_items(path: &Path) -> Result<Vec<Self::Item>, Self::Error>;
63}
64
65/// Trait for validating integrity of a component (e.g., Workspace or Crate)
66#[async_trait]
67pub trait ValidateIntegrity {
68
69    type Error;
70
71    async fn validate_integrity(&self) -> Result<(), Self::Error>;
72}
73
74/// Trait for asynchronously creating `Self` specifically from an environment variable name.
75#[async_trait]
76pub trait AsyncTryFromEnv {
77    type Error;
78
79    /// Creates an instance of `Self` by reading an environment variable asynchronously (if needed).
80    async fn new_from_env(var_name: &str) -> Result<Self, Self::Error>
81    where
82        Self: Sized;
83}
84
85/// Trait for asynchronously creating `Self` from a filesystem path.
86#[async_trait]
87pub trait AsyncTryFromFile {
88    type Error;
89
90    /// Asynchronously create `Self` by reading the file at `path`.
91    async fn new_from_file(path: &Path) -> Result<Self, Self::Error>
92    where
93        Self: Sized;
94}
95
96/// Trait that combines the environment creation with `ValidateIntegrity`.
97#[async_trait]
98pub trait AsyncCreateWithAndValidateEnv:
99    Sized
100    + AsyncTryFromEnv
101    + ValidateIntegrity<Error = <Self as AsyncTryFromEnv>::Error>
102{
103    async fn new_from_env_and_validate(var_name: &str) -> Result<Self, <Self as AsyncTryFromEnv>::Error> {
104        let instance = Self::new_from_env(var_name).await?;
105        instance.validate_integrity().await?;
106        Ok(instance)
107    }
108}
109
110// Blanket implementation if a type implements both `AsyncTryFromEnv` and `ValidateIntegrity`
111impl<T> AsyncCreateWithAndValidateEnv for T
112where
113    T: AsyncTryFromEnv + ValidateIntegrity<Error = <T as AsyncTryFromEnv>::Error>,
114{}
115
116/// Trait that combines file-based creation with `ValidateIntegrity`.
117#[async_trait]
118pub trait AsyncCreateWithAndValidateFile:
119    Sized
120    + AsyncTryFromFile
121    + ValidateIntegrity<Error = <Self as AsyncTryFromFile>::Error>
122{
123    async fn new_from_file_and_validate(path: &Path) -> Result<Self, <Self as AsyncTryFromFile>::Error> {
124        let instance = Self::new_from_file(path).await?;
125        instance.validate_integrity().await?;
126        Ok(instance)
127    }
128}
129
130// Blanket implementation if a type implements both `AsyncTryFromFile` and `ValidateIntegrity`
131impl<T> AsyncCreateWithAndValidateFile for T
132where
133    T: AsyncTryFromFile + ValidateIntegrity<Error = <T as AsyncTryFromFile>::Error>,
134{}