#[derive(Debug, Clone, PartialEq)]
pub enum ResponseValue {
String(String),
Int(i64),
Float(f64),
Bool(bool),
ChosenVariant(usize),
ChosenVariants(Vec<usize>),
StringList(Vec<String>),
IntList(Vec<i64>),
FloatList(Vec<f64>),
}
impl ResponseValue {
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(s) => Some(s),
_ => None,
}
}
pub fn as_int(&self) -> Option<i64> {
match self {
Self::Int(i) => Some(*i),
_ => None,
}
}
pub fn as_float(&self) -> Option<f64> {
match self {
Self::Float(f) => Some(*f),
_ => None,
}
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(b) => Some(*b),
_ => None,
}
}
pub fn as_chosen_variant(&self) -> Option<usize> {
match self {
Self::ChosenVariant(idx) => Some(*idx),
_ => None,
}
}
pub fn as_chosen_variants(&self) -> Option<&[usize]> {
match self {
Self::ChosenVariants(indices) => Some(indices),
_ => None,
}
}
pub fn as_string_list(&self) -> Option<&[String]> {
match self {
Self::StringList(list) => Some(list),
_ => None,
}
}
pub fn as_int_list(&self) -> Option<&[i64]> {
match self {
Self::IntList(list) => Some(list),
_ => None,
}
}
pub fn as_float_list(&self) -> Option<&[f64]> {
match self {
Self::FloatList(list) => Some(list),
_ => None,
}
}
pub fn type_name(&self) -> &'static str {
match self {
Self::String(_) => "String",
Self::Int(_) => "Int",
Self::Float(_) => "Float",
Self::Bool(_) => "Bool",
Self::ChosenVariant(_) => "ChosenVariant",
Self::ChosenVariants(_) => "ChosenVariants",
Self::StringList(_) => "StringList",
Self::IntList(_) => "IntList",
Self::FloatList(_) => "FloatList",
}
}
}
impl From<String> for ResponseValue {
fn from(s: String) -> Self {
Self::String(s)
}
}
impl From<&str> for ResponseValue {
fn from(s: &str) -> Self {
Self::String(s.to_string())
}
}
impl From<i64> for ResponseValue {
fn from(i: i64) -> Self {
Self::Int(i)
}
}
impl From<i32> for ResponseValue {
fn from(i: i32) -> Self {
Self::Int(i64::from(i))
}
}
impl From<f64> for ResponseValue {
fn from(f: f64) -> Self {
Self::Float(f)
}
}
impl From<bool> for ResponseValue {
fn from(b: bool) -> Self {
Self::Bool(b)
}
}
impl From<Vec<usize>> for ResponseValue {
fn from(indices: Vec<usize>) -> Self {
Self::ChosenVariants(indices)
}
}
impl From<Vec<String>> for ResponseValue {
fn from(list: Vec<String>) -> Self {
Self::StringList(list)
}
}
impl From<Vec<i64>> for ResponseValue {
fn from(list: Vec<i64>) -> Self {
Self::IntList(list)
}
}
impl From<Vec<f64>> for ResponseValue {
fn from(list: Vec<f64>) -> Self {
Self::FloatList(list)
}
}