| Crates.io | async-try-from |
| lib.rs | async-try-from |
| version | 0.2.0 |
| created_at | 2025-02-08 16:53:37.29356+00 |
| updated_at | 2025-03-25 05:05:29.84705+00 |
| description | A crate that provides asynchronous trait-based creation and validation patterns. |
| homepage | |
| repository | https://github.com/klebs6/klebs-general |
| max_upload_size | |
| id | 1548161 |
| size | 17,774 |
async-try-from provides a set of traits for defining asynchronous object creation and validation patterns in Rust. This crate builds on the async-trait library to allow trait-based asynchronous construction, integrity checks, and more.
AsyncTryFrom<X>: Asynchronously create a type T from some input X.ValidateIntegrity: Perform synchronous integrity checks on an object.AsyncCreateWithAndValidate<X>: Combines creation and validation into one asynchronous routine.AsyncPathValidator and AsyncFindItems: Simplify common filesystem-related validations and item discovery.Below is a minimal working example demonstrating how to implement AsyncTryFrom and ValidateIntegrity, and then use the combined AsyncCreateWithAndValidate trait for easy creation-and-validation in a single call.
Create a file, for example examples/basic_usage.rs, and paste in the following full Rust code:
mod basic_usage_example {
use async_trait::async_trait;
use async_try_from::{AsyncTryFrom, ValidateIntegrity, AsyncCreateWithAndValidate};
use std::io;
use tokio;
// A simple struct that we will create asynchronously
pub struct MyType;
// Implement async creation from a String
#[async_trait]
impl AsyncTryFrom<String> for MyType {
type Error = io::Error;
async fn new(input: &String) -> Result<Self, Self::Error> {
if input.is_empty() {
Err(io::Error::new(io::ErrorKind::Other, "Input string is empty."))
} else {
Ok(MyType)
}
}
}
// Implement a basic validation check
impl ValidateIntegrity for MyType {
type Error = io::Error;
fn validate_integrity(&self) -> Result<(), Self::Error> {
// Add real integrity checks here if needed
Ok(())
}
}
// Demonstrate creating and validating our type in one step
#[tokio::main]
pub async fn run_example() -> Result<(), Box<dyn std::error::Error>> {
let input = "Some input".to_string();
let instance = MyType::new_and_validate(&input).await?;
println!("Successfully created and validated MyType instance.");
Ok(())
}
}
fn main() {
// In a real project, you could call:
// basic_usage_example::run_example();
// This example keeps main synchronous for demonstration.
println!("Run 'cargo run --example basic_usage' to see the async creation and validation in action.");
}
Then run:
cargo run --example basic_usage
to see the async creation and validation in action.
AsyncTryFrom.ValidateIntegrity to provide domain-specific checks on newly created objects.AsyncCreateWithAndValidate merges creation and validation into a single method for convenience.AsyncPathValidator and AsyncFindItems can be used to handle path validation and item discovery.This project is licensed under the MIT License.