python/
object.rs

1// std imports
2use std::fmt;
3
4// crate imports
5use crate::List;
6use crate::Int;
7use crate::Float;
8use crate::_String;
9use crate::Char;
10use crate::Bool;
11
12
13/// the supreme _Object trait
14/// that its derived types should
15/// implement like all the __functions__ from python
16pub trait _Object {
17    /// python repr(object)
18    fn __repr__(&self) -> String;
19    /// python str(object)
20    fn __str__(&self) -> String;
21}
22
23
24// use int::Int;
25// use float::Float;
26// use string::SString;
27// use _char::Char;
28// use list::Lits
29
30
31/// supreme enum
32pub enum Object {
33    /// char object
34    Char(Char),
35    /// int32 object
36    Int32(Int<i32>),
37    /// int64 object
38    Int64(Int<i64>),
39    /// int128 object
40    // Int128(Int<i128>),
41    /// float32 object
42    Float32(Float<f32>),
43    /// float64 object
44    Float64(Float<f64>),
45    /// String object
46    String(_String),
47    /// List object
48    List(List),
49    /// Bool object
50    Bool(Bool),
51}
52
53
54impl fmt::Display for Object {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        // why there is &* ?
57        // it works without both..
58        match &*self {
59            Object::Char(_char) => write!(f, "{}", _char),
60            Object::Int32(_int) => write!(f, "{}", _int),
61            Object::Int64(_int) => write!(f, "{}", _int),
62            Object::Float32(_float) => write!(f, "{}", _float),
63            Object::Float64(_float) => write!(f, "{}", _float),
64            Object::String(_string) => write!(f, "{}", _string),
65            Object::List(_list) => write!(f, "{}", _list),
66            Object::Bool(_bool) => write!(f, "{}", _bool),
67        }
68    }
69}
70
71impl fmt::Debug for Object {
72    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73        // why there is &* ?
74        // it works without both..
75        match &*self {
76            Object::Char(_char) => write!(f, "{:?}", _char),
77            Object::Int32(_int) => write!(f, "{:?}", _int),
78            Object::Int64(_int) => write!(f, "{:?}", _int),
79            Object::Float32(_float) => write!(f, "{:?}", _float),
80            Object::Float64(_float) => write!(f, "{:?}", _float),
81            Object::String(_string) => write!(f, "{:?}", _string),
82            Object::List(_list) => write!(f, "{:?}", _list),
83            Object::Bool(_bool) => write!(f, "{:?}", _bool),
84        }
85    }
86}