ableos_userland/libraries/std/src/object/types.rs

49 lines
1013 B
Rust

use core::fmt::Debug;
use core::fmt::Display;
use alloc::string::{String, ToString};
use super::type_of;
#[derive(PartialEq, Eq, Hash, Debug, Clone)]
pub enum Types {
Null,
Byte(u8),
String(String),
}
impl Types {
pub fn new<T: Debug + Display>(value: T) -> Self {
let type_of = type_of(&value);
match type_of {
"&str" => return Self::String(value.to_string()),
"alloc::string::String" => return Self::String(value.to_string()),
type_name => {
panic!(
"
Invalid type: {}
{}",
value, type_name
);
}
}
}
}
impl From<u8> for Types {
fn from(value: u8) -> Self {
Self::Byte(value)
}
}
impl From<String> for Types {
fn from(value: String) -> Self {
Self::String(value)
}
}
impl From<&str> for Types {
fn from(value: &str) -> Self {
Self::String(value.to_string())
}
}