use std::fmt;
use std::hash::{Hash, Hasher};
use semq_sys as sys;
use crate::error::{check, new_error, Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Operator {
Orbit = 0,
Phase = 1,
Quant = 2,
}
impl Operator {
pub fn name(self) -> &'static str {
match self {
Operator::Orbit => "orbit",
Operator::Phase => "phase",
Operator::Quant => "quant",
}
}
pub fn parameter_name(self) -> &'static str {
match self {
Operator::Orbit => "scale",
Operator::Phase => "sectors",
Operator::Quant => "bins",
}
}
pub(crate) fn as_raw(self) -> u32 {
self as u32
}
fn from_raw(op: u32) -> Option<Operator> {
match op {
sys::SEMQ_ORBIT => Some(Operator::Orbit),
sys::SEMQ_PHASE => Some(Operator::Phase),
sys::SEMQ_QUANT => Some(Operator::Quant),
_ => None,
}
}
}
impl fmt::Display for Operator {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
type Constructor =
unsafe extern "C" fn(u32, u32, *mut sys::semq_config_t, *mut sys::semq_error_t) -> u32;
#[derive(Clone, Copy)]
pub struct CodecConfig {
raw: sys::semq_config_t,
bytes: [u8; 13],
operator: Operator,
}
impl CodecConfig {
pub const DEFAULT_SCALE: u32 = 50;
pub fn orbit(dim: u32, scale: u32) -> Result<CodecConfig> {
Self::make(sys::semq_config_orbit, dim, scale, "config_orbit")
}
pub fn orbit_default(dim: u32) -> Result<CodecConfig> {
Self::orbit(dim, Self::DEFAULT_SCALE)
}
pub fn phase(dim: u32, sectors: u32) -> Result<CodecConfig> {
Self::make(sys::semq_config_phase, dim, sectors, "config_phase")
}
pub fn quant(dim: u32, bins: u32) -> Result<CodecConfig> {
Self::make(sys::semq_config_quant, dim, bins, "config_quant")
}
fn make(ctor: Constructor, dim: u32, p1: u32, operation: &'static str) -> Result<CodecConfig> {
let mut raw = sys::semq_config_t {
op: 0,
dim: 0,
p1: 0,
p2: 0,
};
let mut err = new_error();
let status = unsafe { ctor(dim, p1, &mut raw, &mut err) };
check(status, &err, operation)?;
Self::from_raw(raw, operation)
}
pub fn from_bytes(bytes: &[u8; 13]) -> Result<CodecConfig> {
let mut raw = sys::semq_config_t {
op: 0,
dim: 0,
p1: 0,
p2: 0,
};
let mut err = new_error();
let status = unsafe { sys::semq_config_from_bytes(bytes.as_ptr(), &mut raw, &mut err) };
check(status, &err, "config_from_bytes")?;
Self::from_raw(raw, "config_from_bytes")
}
pub(crate) fn from_raw(
raw: sys::semq_config_t,
operation: &'static str,
) -> Result<CodecConfig> {
let mut err = new_error();
let status = unsafe { sys::semq_config_validate(&raw, &mut err) };
check(status, &err, operation)?;
let operator = Operator::from_raw(raw.op).ok_or_else(|| {
Error::native(
operation,
sys::SEMQ_ERR_INTERNAL,
None,
None,
"validated config has an unknown operator",
)
})?;
let mut bytes = [0u8; 13];
unsafe { sys::semq_config_to_bytes(&raw, bytes.as_mut_ptr()) };
Ok(CodecConfig {
raw,
bytes,
operator,
})
}
pub(crate) fn as_raw(&self) -> &sys::semq_config_t {
&self.raw
}
pub fn to_bytes(self) -> [u8; 13] {
self.bytes
}
pub fn operator(self) -> Operator {
self.operator
}
pub fn dim(self) -> u32 {
self.raw.dim
}
pub fn rule_revision(self) -> u32 {
self.raw.p2
}
pub fn parameter(self) -> u32 {
self.raw.p1
}
pub fn scale(self) -> Option<u32> {
(self.operator == Operator::Orbit).then_some(self.raw.p1)
}
pub fn sectors(self) -> Option<u32> {
(self.operator == Operator::Phase).then_some(self.raw.p1)
}
pub fn bins(self) -> Option<u32> {
(self.operator == Operator::Quant).then_some(self.raw.p1)
}
pub fn bytes_per_vector(self) -> u32 {
unsafe { sys::semq_config_bytes_per_vector(&self.raw) }
}
pub fn units_per_row(self) -> u32 {
unsafe { sys::semq_config_units_per_row(&self.raw) }
}
pub fn max_magnitude(self) -> Option<f32> {
(self.operator == Operator::Quant)
.then(|| unsafe { sys::semq_config_max_magnitude(&self.raw) })
}
}
impl PartialEq for CodecConfig {
fn eq(&self, other: &CodecConfig) -> bool {
self.bytes == other.bytes
}
}
impl Eq for CodecConfig {}
impl Hash for CodecConfig {
fn hash<H: Hasher>(&self, state: &mut H) {
self.bytes.hash(state);
}
}
impl fmt::Display for CodecConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}(dim={}, {}={})",
self.operator().name(),
self.dim(),
self.operator().parameter_name(),
self.parameter()
)
}
}
impl fmt::Debug for CodecConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("CodecConfig")
.field("operator", &self.operator.name())
.field("dim", &self.raw.dim)
.field(self.operator.parameter_name(), &self.raw.p1)
.field("rule_revision", &self.raw.p2)
.finish()
}
}