stk/packages/int.rs
1//! The `int` package.
2//!
3//! Contains functions such as:
4//! * `int::parse` to parse a string into a number.
5
6use crate::context::{ContextError, Module};
7use crate::error::Result;
8
9/// Parse an integer.
10fn parse(s: &str) -> Result<i64> {
11 Ok(str::parse::<i64>(s)?)
12}
13
14/// Convert a whole number to float.
15fn to_float(value: i64) -> f64 {
16 value as f64
17}
18
19/// Install the core package into the given functions namespace.
20pub fn module() -> Result<Module, ContextError> {
21 let mut module = Module::new(&["std"]);
22
23 module.ty(&["int"]).build::<i64>()?;
24
25 module.function(&["int", "parse"], parse)?;
26 module.inst_fn("to_float", to_float)?;
27
28 Ok(module)
29}