rune_modules/
fs.rs

1//! The native `fs` module for the [Rune Language].
2//!
3//! [Rune Language]: https://rune-rs.github.io
4//!
5//! ## Usage
6//!
7//! Add the following to your `Cargo.toml`:
8//!
9//! ```toml
10//! rune-modules = { version = "0.14.1", features = ["fs"] }
11//! ```
12//!
13//! Install it into your context:
14//!
15//! ```rust
16//! let mut context = rune::Context::with_default_modules()?;
17//! context.install(rune_modules::fs::module(true)?)?;
18//! # Ok::<_, rune::support::Error>(())
19//! ```
20//!
21//! Use it in Rune:
22//!
23//! ```rust,ignore
24//! fn main() {
25//!     let file = fs::read_to_string("file.txt").await?;
26//!     println(`{file}`);
27//! }
28//! ```
29
30use rune::{ContextError, Module};
31use std::io;
32use tokio::fs;
33
34/// Construct the `fs` module.
35pub fn module(_stdio: bool) -> Result<Module, ContextError> {
36    let mut module = Module::with_crate("fs")?;
37    module.function("read_to_string", read_to_string).build()?;
38    Ok(module)
39}
40
41async fn read_to_string(path: String) -> io::Result<String> {
42    fs::read_to_string(&path).await
43}