xcell_types/integer/
mod.rs

1use std::str::FromStr;
2
3use serde::{Deserialize, Serialize};
4use xcell_errors::{
5    for_3rd::{BigInt, DataType, FromPrimitive, ToPrimitive},
6    XResult,
7};
8
9use crate::{typing::XCellTyped, utils::syntax_error, value::XCellValue};
10
11mod kind;
12mod parse_cell;
13
14#[derive(Debug, Default, Clone, Serialize, Deserialize)]
15pub struct IntegerDescription {
16    pub kind: IntegerKind,
17    pub min: BigInt,
18    pub max: BigInt,
19    pub default: BigInt,
20}
21
22#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
23pub enum IntegerKind {
24    Integer8,
25    Integer16,
26    Integer32,
27    Integer64,
28    Unsigned8,
29    Unsigned16,
30    Unsigned32,
31    Unsigned64,
32}
33
34impl IntegerDescription {
35    pub fn range<A, B>(min: A, max: B, kind: IntegerKind) -> Self
36    where
37        A: Into<BigInt>,
38        B: Into<BigInt>,
39    {
40        IntegerDescription { kind, min: min.into(), max: max.into(), default: Default::default() }
41    }
42    pub fn clamp<I>(&self, int: I) -> BigInt
43    where
44        I: Into<BigInt>,
45    {
46        int.into().clamp(self.min.clone(), self.max.clone())
47    }
48}
49
50impl XCellTyped {
51    pub fn as_integer(&self) -> Option<&IntegerDescription> {
52        match self {
53            XCellTyped::Integer(e) => Some(e),
54            _ => None,
55        }
56    }
57    pub fn is_integer(&self) -> bool {
58        self.as_integer().is_some()
59    }
60}