lace_cc/feature/
component.rs

1use std::convert::TryFrom;
2
3use lace_stats::rv::dist::{Bernoulli, Categorical, Gaussian, Poisson};
4
5#[derive(Clone, Debug)]
6/// A column mixture component
7pub enum Component {
8    /// Binary, Bernoulli component
9    Binary(Bernoulli),
10    /// Continuous, Gaussian component
11    Continuous(Gaussian),
12    /// Categorical, Discrete-Dirichlet component
13    Categorical(Categorical),
14    /// Count/Poisson component
15    Count(Poisson),
16}
17
18macro_rules! impl_from_traits {
19    ($inner: ty, $variant: ident) => {
20        impl From<$inner> for Component {
21            fn from(inner: $inner) -> Self {
22                Component::$variant(inner)
23            }
24        }
25
26        impl TryFrom<Component> for $inner {
27            type Error = String;
28            fn try_from(cpnt: Component) -> Result<Self, Self::Error> {
29                match cpnt {
30                    Component::$variant(inner) => Ok(inner),
31                    _ => Err(String::from("Cannot convert Component")),
32                }
33            }
34        }
35    };
36    ($inner: ident) => {
37        impl_from_traits!($inner, $inner);
38    };
39}
40
41impl_from_traits!(Gaussian, Continuous);
42impl_from_traits!(Poisson, Count);
43impl_from_traits!(Bernoulli, Binary);
44impl_from_traits!(Categorical);