use std::ops::Deref;
use super::*;
use crate::*;
use serde::{Deserialize, Serialize};
use snafu::{ResultExt, ensure};
type Result<T> = std::result::Result<T, SiggenError>;
#[cfg_attr(
feature = "python-bindings",
gen_stub_pyclass_complex_enum,
pyclass(from_py_object)
)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum SourceDescriptor {
Silence {},
WhiteNoise {
interrupt_time: Option<InterruptTime>,
},
PinkNoise {
interrupt_time: Option<InterruptTime>,
rolloffPoint: StrictlyPositive,
},
Sine {
frequency: Positive,
},
Sweep {
settings: SweepSettings,
},
}
impl Default for SourceDescriptor {
fn default() -> Self {
Self::Silence {}
}
}
impl SourceDescriptor {
pub fn newSweep(
fl: Flt,
fu: Flt,
sweep_time: Flt,
sweep_type: SweepType,
quiet_time: Option<Flt>,
amplitude_modulation: Option<Vec<(Positive, Positive)>>,
) -> Result<Self> {
let params = SweepSettings::new(
fl,
fu,
sweep_time,
sweep_type,
quiet_time,
amplitude_modulation,
)?;
Ok(SourceDescriptor::Sweep { settings: params })
}
pub fn newWhiteNoise(interrupt_period: Option<Flt>) -> Result<Self> {
let interrupt_time = if let Some(interrupt_period) = interrupt_period {
let t = interrupt_period
.try_into()
.context(ParameterOutOfRangeSnafu {
parameter: "interrupt_period",
})?;
Some(InterruptTime { interrupt_time: t })
} else {
None
};
Ok(SourceDescriptor::WhiteNoise { interrupt_time })
}
}
#[cfg_attr(feature = "python-bindings", gen_stub_pyclass, pyclass(from_py_object))]
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct InterruptTime {
interrupt_time: Bounded<1, 100>,
}
#[cfg(feature = "python-bindings")]
#[cfg_attr(feature = "python-bindings", gen_stub_pymethods, pymethods)]
impl InterruptTime {
#[new]
fn new_py(interrupt_time: Flt) -> PyResult<Self> {
let interrupt_time = interrupt_time
.try_into()
.context(ParameterOutOfRangeSnafu {
parameter: "interrupt_time",
})?;
Ok(Self { interrupt_time })
}
}
impl Deref for InterruptTime {
type Target = Flt;
fn deref(&self) -> &Self::Target {
&self.interrupt_time
}
}
impl From<InterruptTime> for Flt {
fn from(val: InterruptTime) -> Self {
*val.interrupt_time
}
}