plux_rs/variable/
var_type.rs

1use std::fmt::Display;
2
3use serde::{Deserialize, Serialize};
4
5/// Represents the type of a variable in the plugin system.
6///
7/// VariableType defines all supported data types that can be passed between
8/// plugins and the host application. It provides a type-safe way to represent
9/// different kinds of data.
10///
11/// # Variants
12///
13/// * `Let` - Unspecified type (default)
14/// * `Int` - Integer types (signed/unsigned with various sizes)
15/// * `Float` - Floating point types (f32, f64)
16/// * `Bool` - Boolean values
17/// * `Char` - Unicode characters
18/// * `String` - UTF-8 strings
19/// * `List` - Lists/arrays of variables
20///
21/// # Examples
22///
23/// ```rust
24/// use plux_rs::variable::VariableType;
25///
26/// // Create specific types
27/// let int_type = VariableType::I32;
28/// let string_type = VariableType::String;
29/// let list_type = VariableType::List;
30///
31/// // Use in function arguments
32/// let arg = plux_rs::function::Arg::new("count", VariableType::I32);
33/// ```
34#[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
35pub enum VariableType {
36    /// Unspecified or default type
37    #[default]
38    Let,
39    /// Integer types (signed/unsigned)
40    Int(VariableIntType),
41    /// Floating point types
42    Float(VariableFloatType),
43    /// Boolean values
44    Bool,
45    /// Unicode characters
46    Char,
47    /// UTF-8 strings
48    String,
49    /// Lists/arrays of variables
50    List,
51}
52
53/// Represents integer types with size and signedness information.
54///
55/// VariableIntType distinguishes between signed and unsigned integer types
56/// of various sizes supported by the system.
57#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
58pub enum VariableIntType {
59    /// Signed integer types
60    Signed(VariableSignedIntType),
61    /// Unsigned integer types
62    Unsigned(VariableUnsignedIntType),
63}
64
65/// Represents signed integer types of various sizes.
66///
67/// This enum defines all supported signed integer types in the system.
68/// I32 is the default type.
69#[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
70pub enum VariableSignedIntType {
71    /// 8-bit signed integer
72    I8,
73    /// 16-bit signed integer
74    I16,
75    /// 32-bit signed integer (default)
76    #[default]
77    I32,
78    /// 64-bit signed integer
79    I64,
80}
81
82/// Represents unsigned integer types of various sizes.
83///
84/// This enum defines all supported unsigned integer types in the system.
85/// U32 is the default type.
86#[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
87pub enum VariableUnsignedIntType {
88    /// 8-bit unsigned integer
89    U8,
90    /// 16-bit unsigned integer
91    U16,
92    /// 32-bit unsigned integer (default)
93    #[default]
94    U32,
95    /// 64-bit unsigned integer
96    U64,
97}
98
99/// Represents floating point types of various sizes.
100///
101/// This enum defines all supported floating point types in the system.
102/// F32 is the default type.
103#[derive(Default, Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd)]
104pub enum VariableFloatType {
105    /// 32-bit floating point (default)
106    #[default]
107    F32,
108    /// 64-bit floating point
109    F64,
110}
111
112impl VariableType {
113    /// 8-bit signed integer type
114    pub const I8: VariableType =
115        VariableType::Int(VariableIntType::Signed(VariableSignedIntType::I8));
116    /// 16-bit signed integer type
117    pub const I16: VariableType =
118        VariableType::Int(VariableIntType::Signed(VariableSignedIntType::I16));
119    /// 32-bit signed integer type
120    pub const I32: VariableType =
121        VariableType::Int(VariableIntType::Signed(VariableSignedIntType::I32));
122    /// 64-bit signed integer type
123    pub const I64: VariableType =
124        VariableType::Int(VariableIntType::Signed(VariableSignedIntType::I64));
125    /// 8-bit unsigned integer type
126    pub const U8: VariableType =
127        VariableType::Int(VariableIntType::Unsigned(VariableUnsignedIntType::U8));
128    /// 16-bit unsigned integer type
129    pub const U16: VariableType =
130        VariableType::Int(VariableIntType::Unsigned(VariableUnsignedIntType::U16));
131    /// 32-bit unsigned integer type
132    pub const U32: VariableType =
133        VariableType::Int(VariableIntType::Unsigned(VariableUnsignedIntType::U32));
134    /// 64-bit unsigned integer type
135    pub const U64: VariableType =
136        VariableType::Int(VariableIntType::Unsigned(VariableUnsignedIntType::U64));
137    /// 32-bit floating point type
138    pub const F32: VariableType = VariableType::Float(VariableFloatType::F32);
139    /// 64-bit floating point type
140    pub const F64: VariableType = VariableType::Float(VariableFloatType::F64);
141}
142
143impl Default for VariableIntType {
144    fn default() -> Self {
145        Self::Signed(Default::default())
146    }
147}
148
149impl Display for VariableType {
150    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151        match self {
152            Self::Int(t) => write!(f, "{t}"),
153            Self::Float(t) => write!(f, "{t}"),
154            ty => write!(f, "{ty:?}"),
155        }
156    }
157}
158
159impl Display for VariableIntType {
160    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
161        match self {
162            Self::Signed(t) => write!(f, "{t}"),
163            Self::Unsigned(t) => write!(f, "{t}"),
164        }
165    }
166}
167
168impl Display for VariableFloatType {
169    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170        write!(f, "{self:?}")
171    }
172}
173
174impl Display for VariableSignedIntType {
175    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
176        write!(f, "{self:?}")
177    }
178}
179
180impl Display for VariableUnsignedIntType {
181    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182        write!(f, "{self:?}")
183    }
184}