anyscript_compiler/types/
int.rs

1use std::ffi::CString;
2use crate::alias::*;
3use crate::types::prelude::*;
4use std::hash::Hash;
5use std::fmt::{Display, Formatter, Result as FmtResult};
6use std::any::TypeId;
7use std::str::FromStr;
8
9#[derive(Clone)]
10pub struct Int<T1> {
11    pub integral: IntAlias,
12    pub size: IntAlias,
13    pub original: T1,
14    pub type_id: String,
15}
16
17impl<T1: 'static + Default + Display + Eq + Hash + Clone + Send + Sync> Default for Int<T1> {
18    fn default() -> Self {
19        Self::new()
20    }
21}
22
23impl<T1: 'static + Default + Display + Eq + Hash + Clone + Send + Sync> Int<T1> {
24    ///
25    /// # Create a new instance of the class
26    ///
27    /// ```rust
28    /// pub fn new() -> Self {
29    ///     let n = T1::default();
30    ///     Int {
31    ///         integral: 0,
32    ///         size: 0,
33    ///         original: n,
34    ///         type_id: String::new(),
35    ///     }
36    /// }
37    /// ```
38    /// ---
39    pub fn new() -> Self {
40        let n = T1::default();
41        Int {
42            integral: 0,
43            size: 0,
44            original: n,
45            type_id: String::new(),
46        }
47    }
48
49    ///
50    /// # Reset the class
51    ///
52    /// ```rust
53    /// pub fn reset(&mut self) {
54    ///     self.integral = 0;
55    ///     self.size = 0;
56    ///     self.type_id = String::new();
57    ///     self.original = T1::default();
58    /// }
59    /// ```
60    /// ---
61    pub fn reset(&mut self) {
62        self.integral = 0;
63        self.size = 0;
64        self.original = T1::default();
65        self.type_id = String::new();
66    }
67
68    ///
69    /// # Check Valid
70    ///
71    /// ```rust
72    /// pub fn validate(&self) -> (bool, String) {
73    ///     if TypeId::of::<T1>() == TypeId::of::<IntAlias>() {
74    ///         return (true, "int".to_string());
75    ///     } else if TypeId::of::<T1>() == TypeId::of::<FloatAlias>() {
76    ///         return (true, "float".to_string());
77    ///     } else if TypeId::of::<T1>() == TypeId::of::<UIntAlias>() {
78    ///         return (true, "uint".to_string());
79    ///     } else if TypeId::of::<T1>() == TypeId::of::<&str>() || TypeId::of::<T1>() == TypeId::of::<String>() {
80    ///         let value = self.original.to_string();
81    ///         if value.parse::<IntAlias>().is_ok() {
82    ///             return (true, "int".to_string());
83    ///         } else if value.parse::<FloatAlias>().is_ok() {
84    ///             return (true, "float".to_string());
85    ///         } else if value.parse::<UIntAlias>().is_ok() {
86    ///             return (true, "uint".to_string());
87    ///         } else {
88    ///             return (false, "invalid string".to_string());
89    ///         }
90    ///     } else if TypeId::of::<T1>() == TypeId::of::<CString>() {
91    ///         let c_str = self.original.to_string();
92    ///         if c_str.parse::<IntAlias>().is_ok() {
93    ///             return (true, "int".to_string());
94    ///         } else if c_str.parse::<FloatAlias>().is_ok() {
95    ///             return (true, "float".to_string());
96    ///         } else if c_str.parse::<UIntAlias>().is_ok() {
97    ///             return (true, "uint".to_string());
98    ///         } else {
99    ///             return (false, "invalid CString".to_string());
100    ///         }
101    ///     } else if TypeId::of::<T1>() == TypeId::of::<List<T1>>() {
102    ///         let _raw: List<T1> = List::new(); // Explicit type annotation
103    ///         let _type = _raw.get_type();
104    ///         return (true, _type);
105    ///     }
106    ///     (false, "unsupported type".to_string())
107    /// }
108    /// ```
109    /// ---
110    pub fn validate(&self) -> (bool, String) {
111        if TypeId::of::<T1>() == TypeId::of::<IntAlias>() {
112            return (true, "int".to_string());
113        } else if TypeId::of::<T1>() == TypeId::of::<FloatAlias>() {
114            return (true, "float".to_string());
115        } else if TypeId::of::<T1>() == TypeId::of::<UIntAlias>() {
116            return (true, "uint".to_string());
117        } else if TypeId::of::<T1>() == TypeId::of::<&str>() || TypeId::of::<T1>() == TypeId::of::<String>() {
118            let value = self.original.to_string();
119            if value.parse::<IntAlias>().is_ok() {
120                return (true, "int".to_string());
121            } else if value.parse::<FloatAlias>().is_ok() {
122                return (true, "float".to_string());
123            } else if value.parse::<UIntAlias>().is_ok() {
124                return (true, "uint".to_string());
125            } else {
126                return (false, "invalid string".to_string());
127            }
128        } else if TypeId::of::<T1>() == TypeId::of::<CString>() {
129            let c_str = self.original.to_string();
130            if c_str.parse::<IntAlias>().is_ok() {
131                return (true, "int".to_string());
132            } else if c_str.parse::<FloatAlias>().is_ok() {
133                return (true, "float".to_string());
134            } else if c_str.parse::<UIntAlias>().is_ok() {
135                return (true, "uint".to_string());
136            } else {
137                return (false, "invalid CString".to_string());
138            }
139        } else if TypeId::of::<T1>() == TypeId::of::<List<T1>>() {
140            let _raw: List<T1> = List::new(); // Explicit type annotation
141            let _type = _raw.get_type();
142            return (true, _type);
143        }
144        (false, "unsupported type".to_string())
145    }
146
147    /// # Convert Integral to Float, with Optional set to 32 bit or 64 bit
148    /// 
149    /// ```rust
150    /// pub fn to_float(&mut self, bit32: Option<bool>) {
151    ///     let if_32 = bit32.unwrap_or(true); // Default is true
152    ///     if if_32 {
153    ///         self.integral = self.integral as f32 as IntAlias;
154    ///     } else {
155    ///         self.integral = self.integral as f64 as IntAlias;
156    ///     }
157    /// }
158    /// ```
159    pub fn to_float(&mut self, bit32: Option<bool>) {
160        let if_32 = bit32.unwrap_or(true); // Default is true
161        if if_32 {
162            self.integral = self.integral as f32 as IntAlias;
163        } else {
164            self.integral = self.integral as f64 as IntAlias;
165        }
166    }
167
168    /// # Convert to String
169    /// 
170    /// ```rust
171    /// pub fn to_str(&self) -> String {
172    ///     self.integral.to_string()
173    /// }
174    /// ```
175    pub fn to_str(&self) -> String {
176        self.integral.to_string()
177    }
178
179    /// # Convert to an CString
180    /// 
181    /// ```rust
182    /// pub fn to_cstring(&self) -> CString {
183    ///     let _nstring = self.to_str();
184    ///     let _cstr = CString::new(_nstring).unwrap();
185    ///     _cstr
186    /// }
187    /// ```
188    pub fn to_cstring(&self) -> CString {
189        let _nstring = self.to_str();
190        
191        CString::new(_nstring).unwrap()
192    }
193
194    /// # Convert from String
195    /// 
196    /// ```rust
197    /// pub fn from_string(s: &str) -> Result<Self, <IntAlias as FromStr>::Err> {
198    ///     let integral = s.parse::<IntAlias>()?;
199    ///     Ok(Int {
200    ///         integral,
201    ///         size: 0,
202    ///         original: T1::default(),
203    ///         type_id: String::new(),
204    ///     })
205    /// }
206    /// ```
207    pub fn from_string(s: &str) -> Result<Self, <IntAlias as FromStr>::Err> {
208        let integral = s.parse::<IntAlias>()?;
209        Ok(Int {
210            integral,
211            size: 0,
212            original: T1::default(),
213            type_id: String::new(),
214        })
215    }
216}
217
218impl<T1: 'static + Default + Display + Eq + Hash + Clone + Send + Sync> Display for Int<T1> {
219    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
220        write!(f, "{}", self.integral)
221    }
222}