logprob/
errors.rs

1use std::error::Error;
2/// An error for when a [`LogProb`](super::LogProb) is passed a value that isn't negative.
3#[derive(Copy, Clone, PartialEq, Eq, Debug)]
4pub struct FloatIsNanOrPositive;
5
6impl Error for FloatIsNanOrPositive {}
7
8impl std::fmt::Display for FloatIsNanOrPositive {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        write!(f, "LogProb constructed with positive or NaN value")
11    }
12}
13
14/// An error for when a [`LogProb`](super::LogProb)  is passed a value that isn't negative.
15#[derive(Copy, Clone, PartialEq, Eq, Debug)]
16pub struct ProbabilitiesSumToGreaterThanOne;
17
18impl Error for ProbabilitiesSumToGreaterThanOne {}
19
20impl std::fmt::Display for ProbabilitiesSumToGreaterThanOne {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        write!(f, "The sum is greater than 1.0 (improper distribution)")
23    }
24}
25
26impl From<FloatIsNanOrPositive> for ProbabilitiesSumToGreaterThanOne {
27    fn from(_value: FloatIsNanOrPositive) -> Self {
28        ProbabilitiesSumToGreaterThanOne
29    }
30}
31
32/// An error for when a [`Softmax`] is passed a value that is NaN or infinity.
33#[derive(Copy, Clone, PartialEq, Eq, Debug)]
34pub struct FloatIsNanOrPositiveInfinity;
35
36impl Error for FloatIsNanOrPositiveInfinity {}
37
38impl std::fmt::Display for FloatIsNanOrPositiveInfinity {
39    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40        write!(f, "LogProb constructed with positive or NaN value")
41    }
42}