Skip to main content

reqsign_file_read_tokio/
lib.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Tokio-based file reading implementation for reqsign.
19//!
20//! This crate provides `TokioFileRead`, an async file reader that implements
21//! the `FileRead` trait from `reqsign_core` using Tokio's file system operations.
22//!
23//! ## Overview
24//!
25//! `TokioFileRead` enables reqsign to read files asynchronously using Tokio's
26//! efficient async I/O primitives. This is particularly useful when loading
27//! credentials or configuration from the file system.
28//!
29//! ## Example
30//!
31//! ```no_run
32//! use reqsign_core::{Context, OsEnv};
33//! use reqsign_file_read_tokio::TokioFileRead;
34//!
35//! #[tokio::main]
36//! async fn main() {
37//!     // Create a context with Tokio file reader
38//!     let ctx = Context::new()
39//!         .with_file_read(TokioFileRead::default())
40//!         .with_env(OsEnv);
41//!
42//!     // The context can now read files asynchronously
43//!     match ctx.file_read("/path/to/credentials.json").await {
44//!         Ok(content) => println!("Read {} bytes", content.len()),
45//!         Err(e) => eprintln!("Failed to read file: {}", e),
46//!     }
47//! }
48//! ```
49//!
50//! ## Usage with Service Signers
51//!
52//! ```no_run
53//! use reqsign_core::Context;
54//! use reqsign_file_read_tokio::TokioFileRead;
55//!
56//! # async fn example() -> anyhow::Result<()> {
57//! // Many cloud services require reading credentials from files
58//! let ctx = Context::new()
59//!     .with_file_read(TokioFileRead::default());
60//!
61//! // Create a signer that can load credentials from files
62//! // let signer = Signer::new(ctx, credential_loader, request_builder);
63//! # Ok(())
64//! # }
65//! ```
66use reqsign_core::{Error, FileRead, Result};
67
68/// Tokio-based implementation of the `FileRead` trait.
69///
70/// This struct provides async file reading capabilities using Tokio's
71/// file system operations.
72#[derive(Debug, Clone, Copy, Default)]
73pub struct TokioFileRead;
74
75#[cfg(not(target_family = "wasm"))]
76impl FileRead for TokioFileRead {
77    async fn file_read(&self, path: &str) -> Result<Vec<u8>> {
78        tokio::fs::read(path)
79            .await
80            .map_err(|e| Error::unexpected("failed to read file").with_source(e))
81    }
82}
83
84#[cfg(target_family = "wasm")]
85impl FileRead for TokioFileRead {
86    async fn file_read(&self, _path: &str) -> Result<Vec<u8>> {
87        Err(Error::unexpected(
88            "TokioFileRead is unsupported on wasm targets",
89        ))
90    }
91}