Skip to main content

sage_runtime/stdlib/
mod.rs

1//! Standard library functions for Sage.
2//!
3//! This module provides runtime helper functions for the Sage standard library.
4//! Most stdlib functions are inlined during codegen, but some require runtime
5//! helpers for correct Unicode handling or complex logic.
6
7#[cfg(not(target_arch = "wasm32"))]
8mod io;
9mod json;
10mod parsing;
11mod string;
12mod time;
13
14#[cfg(not(target_arch = "wasm32"))]
15pub use io::*;
16pub use json::*;
17pub use parsing::*;
18pub use string::*;
19pub use time::*;
20
21// WASM stubs for I/O functions that require filesystem/stdin access
22#[cfg(target_arch = "wasm32")]
23mod wasm_io {
24    pub fn read_file(_path: &str) -> Result<String, String> {
25        Err("read_file is not available in the WASM target".to_string())
26    }
27
28    pub fn write_file(_path: &str, _contents: &str) -> Result<(), String> {
29        Err("write_file is not available in the WASM target".to_string())
30    }
31
32    pub fn append_file(_path: &str, _contents: &str) -> Result<(), String> {
33        Err("append_file is not available in the WASM target".to_string())
34    }
35
36    #[must_use]
37    pub fn file_exists(_path: &str) -> bool {
38        false
39    }
40
41    pub fn delete_file(_path: &str) -> Result<(), String> {
42        Err("delete_file is not available in the WASM target".to_string())
43    }
44
45    pub fn list_dir(_path: &str) -> Result<Vec<String>, String> {
46        Err("list_dir is not available in the WASM target".to_string())
47    }
48
49    pub fn make_dir(_path: &str) -> Result<(), String> {
50        Err("make_dir is not available in the WASM target".to_string())
51    }
52
53    pub fn read_line() -> Result<String, String> {
54        Err("read_line is not available in the WASM target".to_string())
55    }
56
57    pub fn read_all() -> Result<String, String> {
58        Err("read_all is not available in the WASM target".to_string())
59    }
60}
61
62#[cfg(target_arch = "wasm32")]
63pub use wasm_io::*;