about summary refs log tree commit diff
path: root/tvix/eval/src/io.rs
blob: cf9f6304305f8c4fed8ee7f3adcb8839e595ddf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Interface for injecting I/O-related functionality into tvix-eval.
//!
//! The Nix language contains several builtins (e.g. `builtins.readDir`), as
//! well as language feature (e.g. string-"coercion" of paths) that interact
//! with the filesystem.
//!
//! The language evaluator implemented by this crate does not depend on any
//! particular filesystem interaction model. Instead, this module provides a
//! trait that can be implemented by tvix-eval callers to provide the
//! functionality they desire.
//!
//! In theory this can be used to implement "mocked" filesystem interactions, or
//! interaction with remote filesystems, etc.
//!
//! In the context of Nix builds, callers also use this interface to determine
//! how store paths are opened and so on.

use std::path::PathBuf;

use crate::errors::ErrorKind;

/// Defines how filesystem interaction occurs inside of tvix-eval.
pub trait EvalIO {
    /// Read the file at the specified path to a string.
    fn read_to_string(&self, path: PathBuf) -> Result<String, ErrorKind>;
}

/// Implementation of [`EvalIO`] that simply uses the equivalent
/// standard library functions, i.e. does local file-IO.
pub struct StdIO;

impl EvalIO for StdIO {
    fn read_to_string(&self, path: PathBuf) -> Result<String, ErrorKind> {
        let path: PathBuf = path.into();
        std::fs::read_to_string(&path).map_err(|e| ErrorKind::IO {
            path: Some(path),
            error: std::rc::Rc::new(e),
        })
    }
}

/// Dummy implementation of [`EvalIO`], can be used in contexts where
/// IO is not available but code should "pretend" that it is.
pub struct DummyIO;

impl EvalIO for DummyIO {
    fn read_to_string(&self, _: PathBuf) -> Result<String, ErrorKind> {
        Err(ErrorKind::NotImplemented(
            "I/O methods are not implemented in DummyIO",
        ))
    }
}