use core::fmt::Display;
#[derive(Debug, Clone)]
pub struct SerialConfig {
pub(crate) port_name: String,
pub(crate) baud_rate: u32,
read_buffer_capacity: usize,
}
impl SerialConfig {
pub fn new(port_name: String, baud_rate: u32) -> Self {
let default_capacity = (baud_rate / 100).clamp(1024, 1024 * 8) as usize;
Self {
port_name,
baud_rate,
read_buffer_capacity: default_capacity,
}
}
pub fn with_read_buffer_capacity(mut self, capacity: usize) -> Self {
self.read_buffer_capacity = capacity;
self
}
pub fn buffer_capacity(&self) -> usize {
self.read_buffer_capacity
}
}
impl Display for SerialConfig {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "serial:{}:{}", self.port_name, self.baud_rate)
}
}