feat(tvix/eval): Implement builtins.readFile
Change-Id: If3fd0b087009a2bfbad8bb7aca0aa20de906eb12 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6921 Tested-by: BuildkiteCI Reviewed-by: Adam Joseph <adam@westernsemico.com> Reviewed-by: kanepyork <rikingcoding@gmail.com> Autosubmit: grfn <grfn@gws.fyi> Reviewed-by: tazjin <tazjin@tvl.su>
This commit is contained in:
parent
1f84d90811
commit
9669fa8ff1
4 changed files with 16 additions and 1 deletions
|
@ -1,7 +1,8 @@
|
|||
use std::{
|
||||
cell::RefCell,
|
||||
collections::{BTreeMap, HashMap},
|
||||
io,
|
||||
fs::File,
|
||||
io::{self, Read},
|
||||
rc::Rc,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
@ -52,6 +53,11 @@ fn impure_builtins() -> Vec<Builtin> {
|
|||
}
|
||||
Ok(Value::attrs(NixAttrs::from_map(res)))
|
||||
}),
|
||||
Builtin::new("readFile", &[true], |args: Vec<Value>, vm: &mut VM| {
|
||||
let mut buf = String::new();
|
||||
File::open(&super::coerce_value_to_path(&args[0], vm)?)?.read_to_string(&mut buf)?;
|
||||
Ok(buf.into())
|
||||
}),
|
||||
]
|
||||
}
|
||||
|
||||
|
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-readfile.exp
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-readfile.exp
Normal file
|
@ -0,0 +1 @@
|
|||
"builtins.readFile ./eval-okay-readfile.nix\n"
|
1
tvix/eval/src/tests/tvix_tests/eval-okay-readfile.nix
Normal file
1
tvix/eval/src/tests/tvix_tests/eval-okay-readfile.nix
Normal file
|
@ -0,0 +1 @@
|
|||
builtins.readFile ./eval-okay-readfile.nix
|
|
@ -1,6 +1,7 @@
|
|||
//! This module implements Nix language strings and their different
|
||||
//! backing implementations.
|
||||
use smol_str::SmolStr;
|
||||
use std::ffi::OsStr;
|
||||
use std::hash::Hash;
|
||||
use std::ops::Deref;
|
||||
use std::path::Path;
|
||||
|
@ -186,6 +187,12 @@ impl AsRef<str> for NixString {
|
|||
}
|
||||
}
|
||||
|
||||
impl AsRef<OsStr> for NixString {
|
||||
fn as_ref(&self) -> &OsStr {
|
||||
self.as_str().as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<Path> for NixString {
|
||||
fn as_ref(&self) -> &Path {
|
||||
self.as_str().as_ref()
|
||||
|
|
Loading…
Reference in a new issue