laddu_core/utils/
enums.rs

1use std::{fmt::Display, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4
5use crate::LadduError;
6
7/// Standard reference frames for angular analyses.
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
9pub enum Frame {
10    /// The helicity frame, obtained by setting the $`z`$-axis equal to the boost direction from
11    /// the center-of-momentum to the rest frame of the resonance in question and the $`y`$-axis
12    /// perpendicular to the production plane.
13    Helicity,
14    /// The Gottfried-Jackson frame, obtained by setting the $`z`$-axis proportional to the beam's
15    /// direction in the rest frame of the resonance in question and the $`y`$-axis perpendicular
16    /// to the production plane.
17    GottfriedJackson,
18}
19impl Display for Frame {
20    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
21        match self {
22            Frame::Helicity => write!(f, "Helicity"),
23            Frame::GottfriedJackson => write!(f, "Gottfried-Jackson"),
24        }
25    }
26}
27impl FromStr for Frame {
28    type Err = LadduError;
29
30    fn from_str(s: &str) -> Result<Self, Self::Err> {
31        match s.to_lowercase().as_str() {
32            "helicity" | "hx" | "hel" => Ok(Self::Helicity),
33            "gottfriedjackson" | "gottfried jackson" | "gj" | "gottfried-jackson" => {
34                Ok(Self::GottfriedJackson)
35            }
36            _ => Err(LadduError::ParseError {
37                name: s.to_string(),
38                object: "Frame".to_string(),
39            }),
40        }
41    }
42}
43
44/// A simple enum describing a binary sign.
45#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
46pub enum Sign {
47    /// A positive indicator.
48    Positive,
49    /// A negative indicator.
50    Negative,
51}
52impl Display for Sign {
53    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
54        match self {
55            Sign::Positive => write!(f, "+"),
56            Sign::Negative => write!(f, "-"),
57        }
58    }
59}
60
61impl FromStr for Sign {
62    type Err = LadduError;
63
64    fn from_str(s: &str) -> Result<Self, Self::Err> {
65        match s.to_lowercase().as_ref() {
66            "+" | "plus" | "pos" | "positive" => Ok(Self::Positive),
67            "-" | "minus" | "neg" | "negative" => Ok(Self::Negative),
68            _ => Err(LadduError::ParseError {
69                name: s.to_string(),
70                object: "Sign".to_string(),
71            }),
72        }
73    }
74}
75
76/// An enum for Mandelstam variables
77#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
78pub enum Channel {
79    /// s-channel
80    S,
81    /// t-channel
82    T,
83    /// u-channel
84    U,
85}
86
87impl Display for Channel {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        match self {
90            Channel::S => write!(f, "s"),
91            Channel::T => write!(f, "t"),
92            Channel::U => write!(f, "u"),
93        }
94    }
95}
96
97impl FromStr for Channel {
98    type Err = LadduError;
99
100    fn from_str(s: &str) -> Result<Self, Self::Err> {
101        match s.to_lowercase().as_ref() {
102            "s" => Ok(Self::S),
103            "t" => Ok(Self::T),
104            "u" => Ok(Self::U),
105            _ => Err(LadduError::ParseError {
106                name: s.to_string(),
107                object: "Channel".to_string(),
108            }),
109        }
110    }
111}
112
113#[cfg(test)]
114mod tests {
115    use super::*;
116    use std::str::FromStr;
117
118    #[test]
119    fn enum_displays() {
120        assert_eq!(format!("{}", Frame::Helicity), "Helicity");
121        assert_eq!(format!("{}", Frame::GottfriedJackson), "Gottfried-Jackson");
122        assert_eq!(format!("{}", Sign::Positive), "+");
123        assert_eq!(format!("{}", Sign::Negative), "-");
124        assert_eq!(format!("{}", Channel::S), "s");
125        assert_eq!(format!("{}", Channel::T), "t");
126        assert_eq!(format!("{}", Channel::U), "u");
127    }
128
129    #[test]
130    fn enum_from_str() {
131        assert_eq!(Frame::from_str("Helicity").unwrap(), Frame::Helicity);
132        assert_eq!(Frame::from_str("HX").unwrap(), Frame::Helicity);
133        assert_eq!(Frame::from_str("HEL").unwrap(), Frame::Helicity);
134        assert_eq!(
135            Frame::from_str("GottfriedJackson").unwrap(),
136            Frame::GottfriedJackson
137        );
138        assert_eq!(Frame::from_str("GJ").unwrap(), Frame::GottfriedJackson);
139        assert_eq!(
140            Frame::from_str("Gottfried-Jackson").unwrap(),
141            Frame::GottfriedJackson
142        );
143        assert_eq!(
144            Frame::from_str("Gottfried Jackson").unwrap(),
145            Frame::GottfriedJackson
146        );
147        assert_eq!(Sign::from_str("+").unwrap(), Sign::Positive);
148        assert_eq!(Sign::from_str("pos").unwrap(), Sign::Positive);
149        assert_eq!(Sign::from_str("plus").unwrap(), Sign::Positive);
150        assert_eq!(Sign::from_str("Positive").unwrap(), Sign::Positive);
151        assert_eq!(Sign::from_str("-").unwrap(), Sign::Negative);
152        assert_eq!(Sign::from_str("minus").unwrap(), Sign::Negative);
153        assert_eq!(Sign::from_str("neg").unwrap(), Sign::Negative);
154        assert_eq!(Sign::from_str("Negative").unwrap(), Sign::Negative);
155        assert_eq!(Channel::from_str("S").unwrap(), Channel::S);
156        assert_eq!(Channel::from_str("s").unwrap(), Channel::S);
157        assert_eq!(Channel::from_str("T").unwrap(), Channel::T);
158        assert_eq!(Channel::from_str("t").unwrap(), Channel::T);
159        assert_eq!(Channel::from_str("U").unwrap(), Channel::U);
160        assert_eq!(Channel::from_str("u").unwrap(), Channel::U);
161    }
162}