-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6606b4e
commit 8ec16e6
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod runtime; | ||
mod test; | ||
mod http; | ||
mod http; | ||
mod stdlib; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
use crate::reporting::error::ERROR_HANDLER_CALL; | ||
|
||
use super::runtime::{WXRTValue, WXRuntimeError, WXRuntimeInfo}; | ||
|
||
/// Serve static content from the filesystem. | ||
/// | ||
/// # Arguments | ||
/// - `path`: The path to the file to serve relative to the project root. | ||
fn webx_static(relative_path: &WXRTValue, info: &WXRuntimeInfo) -> Result<WXRTValue, WXRuntimeError> { | ||
// Read the file from the filesystem. | ||
if let WXRTValue::String(path) = relative_path { | ||
let file = std::fs::read(info.project_root.join(path)); | ||
if let Ok(file) = file { | ||
return Ok(WXRTValue::String(String::from_utf8(file).unwrap())); | ||
} else { | ||
return Err(WXRuntimeError { | ||
message: format!("static: failed to read file '{}'", path), | ||
code: ERROR_HANDLER_CALL | ||
}); | ||
} | ||
} | ||
Err(WXRuntimeError { | ||
message: format!("static: failed to read file '{}'", relative_path.to_js()), | ||
code: ERROR_HANDLER_CALL | ||
}) | ||
} | ||
|
||
pub fn try_call(name: &str, args: Vec<WXRTValue>, info: &WXRuntimeInfo) -> Option<Result<WXRTValue, WXRuntimeError>> { | ||
let assert_args = |n: usize| { | ||
if args.len() != n { | ||
return Some(Err::<WXRTValue, WXRuntimeError>(WXRuntimeError { | ||
message: format!("{}: expected {} arguments, got {}", name, n, args.len()), | ||
code: ERROR_HANDLER_CALL | ||
})); | ||
} | ||
None | ||
}; | ||
|
||
match name { | ||
"static" => Some(match assert_args(1) { Some(err) => err, None => webx_static(&args[0], info) }), | ||
_ => None | ||
} | ||
} |