code wiki / (root) / fs_result_test.nx

fs_result_test.nx source

↩ module page · 40 lines · 1397 B

1// fs_result_test.nx -- end-to-end Result<T, FsError> smoke. 2// 3// Opens a path that does not exist, expects fs_open_rd to return 4// Result::Err(FsError::NotFound), and threads it through the match. 5// The collapsed code for NotFound is -2; as an 8-bit unsigned exit 6// status that surfaces as 254. Proves: Result constructor + tagged 7// match + payload binding + i64 return all work together. 8// 9// expect_exit: 254 10 11import "nx_syscalls.nx" 12import "nx_stdlib.nx" 13import "nx_fs.nx" 14 15// Take a Result<fd, FsError>, collapse to a single i64 where: 16// Ok(fd) -> fd (>= 0) 17// Err(NotFound) -> -2 18// Err(PermDenied) -> -13 19// Err(other) -> -1 20func fs_open_result_code(r: *Result<i64, FsError>) -> i64 { 21 match r { 22 Result::Ok(fd) => { return fd }, 23 Result::Err(e) => { 24 if e == FsError::NotFound { return 0 - 2 } 25 if e == FsError::PermDenied { return 0 - 13 } 26 return 0 - 1 27 }, 28 } 29 return 0 30} 31 32// Drive: attempt to open a path that almost certainly doesn't exist 33// and expect NotFound. The actual syscall would need a RISC-V 34// runtime to exercise -- here we just prove the Result plumbing 35// compiles. 36func main() -> i64 { 37 let path: *u8 = "/nonexistent/path/hopefully" as *u8 38 let r: *Result<i64, FsError> = fs_open_rd(path) 39 return fs_open_result_code(r) 40}