compose_library/foundations/
int.rs

1use crate::diag::bail;
2use compose_library::diag::StrResult;
3use compose_library::Value;
4use compose_macros::{cast, func};
5use compose_macros::{scope, ty};
6
7#[ty(scope, cast, name = "Int")]
8type i64;
9
10#[scope]
11impl i64 {
12    #[func]
13    pub fn increment(value: i64) -> i64 {
14        value + 1
15    }
16
17    #[func]
18    pub fn incremented(self) -> i64 {
19        self + 1
20    }
21
22    #[func]
23    pub fn add(self, other: i64) -> i64 {
24        self + other
25    }
26
27    #[func(name = "pow")]
28    pub fn power(self, exponent: i64) -> StrResult<i64> {
29        match exponent.try_into() {
30            Ok(as_u32) => Ok(self.pow(as_u32)),
31            Err(e) => bail!("{} is too big for an exponent", e.to_string(),),
32        }
33    }
34}
35
36cast! {
37    usize,
38    self => Value::Int(self as i64),
39    v: i64 => v as usize,
40}