chore: move value_type.rs to common module
This commit is contained in:
1 parent
c573263f48
commit
7786ee7ff3
3 files changed
+4
-4
No files matched your search
@@ -1,11 +1,10 @@
|
||||
pub mod multi_entry_data;
|
||||
pub mod value_type;
|
||||
pub mod multi_entry;
|
||||
mod multi_entry_member;
|
||||
|
||||
pub use crate::common::value_type::*;
|
||||
pub use self::{
|
||||
value_type::*,
|
||||
multi_entry_data::*,
|
||||
multi_entry::*,
|
||||
multi_entry_data::*,
|
||||
multi_entry_member::*,
|
||||
};
|
||||
@@ -1,77 +0,0 @@
|
||||
use serde::{Serialize, Deserialize};
|
||||
use std::fmt::Display;
|
||||
|
||||
/// Defines a set of possible input types. They allow for the type and value of every data member
|
||||
/// to be stored within a single `HashMap` or `Vec`. This means that even though all the data is
|
||||
/// created as a single type, the values are still able to be type safe.
|
||||
#[derive(PartialEq, Clone, Serialize, Deserialize, Debug)]
|
||||
pub enum ValueType {
|
||||
/// Indicates a `String` value.
|
||||
String(Option<String>),
|
||||
/// Indicates an `i32` value.
|
||||
Number(Option<String>),
|
||||
/// Indicates a `Vec<T>`, where `T` can be any other `ValueType`.
|
||||
List(Option<Vec<ValueType>>),
|
||||
}
|
||||
|
||||
impl ValueType {
|
||||
/// Returns true if this is a `String`.
|
||||
pub fn is_string(&self) -> bool {
|
||||
if let ValueType::String(_) = self { true } else { false }
|
||||
}
|
||||
|
||||
/// Attempts to get this value as a `String` type. This is different from the `to_string` method.
|
||||
pub fn as_string(&self) -> Result<Option<String>, Self> {
|
||||
if let ValueType::String(v) = self {
|
||||
Ok(v.clone())
|
||||
} else {
|
||||
Err(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this is a `Number`.
|
||||
pub fn is_number(&self) -> bool {
|
||||
if let ValueType::Number(_) = self { true } else { false }
|
||||
}
|
||||
|
||||
/// Attempts to get this value as an `i32`.
|
||||
pub fn as_number(&self) -> Result<Option<String>, Self> {
|
||||
if let ValueType::Number(v) = self {
|
||||
Ok(v.clone())
|
||||
} else {
|
||||
Err(self.clone())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns true if this is a `List`.
|
||||
pub fn is_list(&self) -> bool {
|
||||
if let ValueType::List(_) = self { true } else { false }
|
||||
}
|
||||
|
||||
/// Attempts to get this value as a `Vec<ValueType>`. Each of the values inside the list must
|
||||
/// also be converted later, as these values are any of the possible types in the `ValueType`.
|
||||
pub fn as_list(&self) -> Result<Option<Vec<ValueType>>, Self> {
|
||||
if let ValueType::List(v) = self {
|
||||
Ok(v.clone())
|
||||
} else {
|
||||
Err(self.clone())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ValueType {
|
||||
fn default() -> Self {
|
||||
ValueType::String(Some(String::new()))
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ValueType {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let str = match self {
|
||||
ValueType::String(s) => s.clone().unwrap_or("".to_owned()),
|
||||
ValueType::Number(n) => n.clone().unwrap_or("".to_owned()),
|
||||
ValueType::List(l) => format!("{l:?}"),
|
||||
};
|
||||
write!(f, "{}", str)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user