runestick/modules/
core.rs

1//! The core `std` module.
2
3use crate::{ContextError, Module, Panic, Value};
4
5/// Construct the `std` module.
6pub fn module() -> Result<Module, ContextError> {
7    let mut module = Module::with_crate("std");
8
9    module.unit("unit")?;
10    module.ty::<bool>()?;
11    module.ty::<char>()?;
12    module.ty::<u8>()?;
13    module.ty::<f64>()?;
14    module.ty::<i64>()?;
15
16    module.function(&["panic"], panic_impl)?;
17    module.function(&["is_readable"], is_readable)?;
18    module.function(&["is_writable"], is_writable)?;
19    Ok(module)
20}
21
22fn panic_impl(m: &str) -> Result<(), Panic> {
23    Err(Panic::custom(m.to_owned()))
24}
25
26fn is_readable(value: Value) -> bool {
27    match value {
28        Value::Any(any) => any.is_readable(),
29        Value::String(string) => string.is_readable(),
30        Value::Bytes(bytes) => bytes.is_readable(),
31        Value::Vec(vec) => vec.is_readable(),
32        Value::Tuple(tuple) => tuple.is_readable(),
33        Value::Object(object) => object.is_readable(),
34        Value::UnitStruct(empty) => empty.is_readable(),
35        Value::TupleStruct(tuple) => tuple.is_readable(),
36        Value::Struct(object) => object.is_readable(),
37        Value::Variant(variant) => variant.is_readable(),
38        _ => true,
39    }
40}
41
42fn is_writable(value: Value) -> bool {
43    match value {
44        Value::Any(any) => any.is_writable(),
45        Value::String(string) => string.is_writable(),
46        Value::Bytes(bytes) => bytes.is_writable(),
47        Value::Vec(vec) => vec.is_writable(),
48        Value::Tuple(tuple) => tuple.is_writable(),
49        Value::Object(object) => object.is_writable(),
50        Value::UnitStruct(empty) => empty.is_writable(),
51        Value::TupleStruct(tuple) => tuple.is_writable(),
52        Value::Struct(object) => object.is_writable(),
53        Value::Variant(variant) => variant.is_writable(),
54        _ => true,
55    }
56}