autd3_core/modulation/
error.rs

1use crate::firmware::SamplingConfigError;
2
3#[derive(Debug, PartialEq, Clone)]
4/// An error occurred during modulation calculation.
5pub struct ModulationError {
6    msg: String,
7}
8
9impl core::fmt::Display for ModulationError {
10    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
11        write!(f, "{}", self.msg)
12    }
13}
14
15impl core::error::Error for ModulationError {}
16
17impl ModulationError {
18    /// Creates a new [`ModulationError`].
19    #[must_use]
20    pub fn new(msg: impl ToString) -> Self {
21        Self {
22            msg: msg.to_string(),
23        }
24    }
25}
26
27impl From<SamplingConfigError> for ModulationError {
28    fn from(e: SamplingConfigError) -> Self {
29        Self::new(e)
30    }
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    #[test]
38    fn display() {
39        let err = ModulationError::new("test error");
40        assert_eq!(format!("{}", err), "test error");
41    }
42
43    #[test]
44    fn from_sampling_config_error() {
45        let sce = SamplingConfigError::FreqInvalid(1 * crate::common::Hz);
46        let me: ModulationError = sce.into();
47        assert_eq!(me.msg, sce.to_string());
48    }
49}