use crate::value::Value;
#[cfg(feature = "serialization")]
use serde::Serialize;
use std::convert::From;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Bin {
pub name: String,
pub value: Value,
}
impl Bin {
pub const fn new(name: String, val: Value) -> Self {
Bin { name, value: val }
}
}
impl AsRef<Bin> for Bin {
fn as_ref(&self) -> &Self {
self
}
}
#[macro_export]
macro_rules! as_bin {
($bin_name:expr, None) => {{
$crate::Bin::new($bin_name.into(), $crate::Value::Nil)
}};
($bin_name:expr, $val:expr) => {{
$crate::Bin::new($bin_name.into(), $crate::Value::from($val))
}};
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serialization", derive(Serialize))]
pub enum Bins {
All,
None,
Some(Vec<String>),
}
impl Bins {
pub const fn is_all(&self) -> bool {
matches!(*self, Bins::All)
}
pub const fn is_none(&self) -> bool {
matches!(*self, Bins::None)
}
}
impl From<&[&str]> for Bins {
fn from(bins: &[&str]) -> Self {
let bins = bins.iter().copied().map(String::from).collect();
Bins::Some(bins)
}
}
impl<const COUNT: usize> From<[&str; COUNT]> for Bins {
fn from(bins: [&str; COUNT]) -> Self {
let bins = bins.iter().copied().map(String::from).collect();
Bins::Some(bins)
}
}
#[cfg(test)]
mod tests {
use super::{Bins, From};
#[test]
fn into_bins() {
let bin_names = vec!["a".to_string(), "b".to_string(), "c".to_string()];
let expected = Bins::Some(bin_names);
assert_eq!(expected, Bins::from(["a", "b", "c"]));
}
}