const SOH: u8 = 0x1;
const DEFAULT_MAX_MESSAGE_SIZE: usize = 0xffff;
pub trait Configure: Clone + Default {
#[inline]
fn separator(&self) -> u8 {
SOH
}
#[inline]
fn max_message_size(&self) -> Option<usize> {
Some(DEFAULT_MAX_MESSAGE_SIZE)
}
#[inline]
fn verify_checksum(&self) -> bool {
true
}
#[inline]
fn should_decode_associative(&self) -> bool {
true
}
}
#[derive(Debug, Copy, Clone)]
pub struct Config {
separator: u8,
max_message_size: Option<usize>,
verify_checksum: bool,
should_decode_associative: bool,
}
impl Config {
pub fn set_separator(&mut self, separator: u8) {
self.separator = separator;
self.verify_checksum = separator == SOH;
}
pub fn set_max_message_size(&mut self, max_message_size: Option<usize>) {
self.max_message_size = max_message_size;
}
pub fn set_verify_checksum(&mut self, verify: bool) {
self.verify_checksum = verify;
}
pub fn set_decode_assoc(&mut self, should: bool) {
self.should_decode_associative = should;
}
}
impl Configure for Config {
#[inline]
fn separator(&self) -> u8 {
self.separator
}
#[inline]
fn verify_checksum(&self) -> bool {
self.verify_checksum
}
#[inline]
fn max_message_size(&self) -> Option<usize> {
self.max_message_size
}
#[inline]
fn should_decode_associative(&self) -> bool {
self.should_decode_associative
}
}
impl Default for Config {
fn default() -> Self {
Self {
max_message_size: Some(DEFAULT_MAX_MESSAGE_SIZE),
separator: SOH,
verify_checksum: true,
should_decode_associative: true,
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn config_separator_is_soh_by_default() {
assert_eq!(Config::default().separator(), 0x1);
}
#[test]
fn config_separator_can_be_changed() {
let config = &mut Config::default();
config.set_separator(b'|');
assert_eq!(config.separator(), b'|');
config.set_separator(b'^');
assert_eq!(config.separator(), b'^');
config.set_separator(0x1);
assert_eq!(config.separator(), 0x1);
}
#[test]
fn config_verifies_checksum_by_default() {
assert_eq!(Config::default().verify_checksum(), true);
}
#[test]
fn config_checksum_verification_can_be_changed() {
let mut config = Config::default();
config.set_verify_checksum(false);
assert_eq!(config.verify_checksum(), false);
config.set_verify_checksum(true);
assert_eq!(config.verify_checksum(), true);
}
}