Skip to main content

libdd_capabilities/
file.rs

1// Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/
2// SPDX-License-Identifier: Apache-2.0
3
4//! File-system capability trait and error types.
5//!
6//! Async so a wasm impl can await `fs.promises`. Paths are `&str` because
7//! wasm callers hand them across the JS boundary as strings.
8
9use crate::maybe_send::MaybeSend;
10use core::future::Future;
11
12#[derive(Debug, thiserror::Error)]
13pub enum FileError {
14    #[error("File not found: {0}")]
15    NotFound(String),
16    #[error("Permission denied: {0}")]
17    PermissionDenied(String),
18    #[error("IO error: {0}")]
19    Io(anyhow::Error),
20}
21
22/// Snapshot of a file-system entry's metadata.
23///
24/// `inode` is `None` when the underlying platform does not expose one (Windows
25/// via `std`). Node.js exposes an inode on every platform, so the wasm impl
26/// always populates it.
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub struct FileMetadata {
29    pub size: u64,
30    pub inode: Option<u64>,
31    pub is_file: bool,
32    pub is_dir: bool,
33}
34
35pub trait FileCapability: Clone + std::fmt::Debug {
36    fn new() -> Self;
37
38    fn read(&self, path: &str)
39        -> impl Future<Output = Result<bytes::Bytes, FileError>> + MaybeSend;
40
41    fn write(
42        &self,
43        path: &str,
44        contents: bytes::Bytes,
45    ) -> impl Future<Output = Result<(), FileError>> + MaybeSend;
46
47    fn metadata(
48        &self,
49        path: &str,
50    ) -> impl Future<Output = Result<FileMetadata, FileError>> + MaybeSend;
51
52    fn exists(&self, path: &str) -> impl Future<Output = Result<bool, FileError>> + MaybeSend;
53}