Skip to main content

windjammer_runtime/platform/native/
fs.rs

1//! Native file system implementation
2//!
3//! Re-exports the existing windjammer-runtime fs module.
4
5// Re-export all functions from the parent fs module
6pub use crate::fs::*;
7
8// Add any additional functions needed by std::fs API
9pub fn read_file(path: String) -> Result<String, String> {
10    read_to_string(path)
11}
12
13pub fn read_bytes(path: String) -> Result<Vec<u8>, String> {
14    std::fs::read(&path).map_err(|e| format!("Failed to read file {}: {}", path, e))
15}
16
17pub fn write_file(path: String, content: String) -> Result<(), String> {
18    write_string(path, &content)
19}
20
21pub fn file_exists(path: String) -> bool {
22    exists(path)
23}
24
25pub fn create_directory(path: String) -> Result<(), String> {
26    create_dir_all(path)
27}
28
29pub fn delete_file(path: String) -> Result<(), String> {
30    remove_file(path)
31}
32
33pub fn list_directory(path: String) -> Result<Vec<FileEntry>, String> {
34    use std::fs;
35
36    let entries = fs::read_dir(&path).map_err(|e| format!("Failed to read directory: {}", e))?;
37
38    let mut files = Vec::new();
39    for entry in entries {
40        let entry = entry.map_err(|e| format!("Failed to read entry: {}", e))?;
41        let path_buf = entry.path();
42        let name = path_buf
43            .file_name()
44            .and_then(|n| n.to_str())
45            .unwrap_or("")
46            .to_string();
47
48        files.push(FileEntry {
49            name,
50            path: path_buf.to_string_lossy().to_string(),
51            is_directory: path_buf.is_dir(),
52        });
53    }
54
55    Ok(files)
56}
57
58#[derive(Debug, Clone)]
59pub struct FileEntry {
60    pub name: String,
61    pub path: String,
62    pub is_directory: bool,
63}