1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use xcell_errors::{
    for_3rd::{BigInt, DataType, FromPrimitive, ToPrimitive},
    XResult,
};

use crate::{typing::XCellTyped, utils::syntax_error, value::XCellValue};

mod kind;
mod parse_cell;

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct IntegerDescription {
    pub kind: IntegerKind,
    pub min: BigInt,
    pub max: BigInt,
    pub default: BigInt,
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub enum IntegerKind {
    Integer8,
    Integer16,
    Integer32,
    Integer64,
    Unsigned8,
    Unsigned16,
    Unsigned32,
    Unsigned64,
}

impl IntegerDescription {
    pub fn range<A, B>(min: A, max: B, kind: IntegerKind) -> Self
    where
        A: Into<BigInt>,
        B: Into<BigInt>,
    {
        IntegerDescription { kind, min: min.into(), max: max.into(), default: Default::default() }
    }
    pub fn clamp<I>(&self, int: I) -> BigInt
    where
        I: Into<BigInt>,
    {
        int.into().clamp(self.min.clone(), self.max.clone())
    }
}

impl XCellTyped {
    pub fn as_integer(&self) -> Option<&IntegerDescription> {
        match self {
            XCellTyped::Integer(e) => Some(e),
            _ => None,
        }
    }
    pub fn is_integer(&self) -> bool {
        self.as_integer().is_some()
    }
}