humpback-dds/src/translation.rs

28 lines
837 B
Rust
Raw Normal View History

2020-09-15 12:17:42 +08:00
use scpi::prelude::*;
use scpi::Context;
use scpi::error::Result;
use arrayvec::{ArrayVec};
pub trait MqttScpiTranslator {
fn run_with_mqtt<FMT: Formatter>(&mut self, s: &[u8], response: &mut FMT) -> Result<()>;
}
impl<'a, T: Device> MqttScpiTranslator for Context<'a, T> {
fn run_with_mqtt<FMT>(&mut self, s: &[u8], response: &mut FMT) -> Result<()>
where
FMT: Formatter,
{
let mut array_vec = ArrayVec::<[u8; 1024]>::new();
for i in s.into_iter() {
if *i == b'/' {
array_vec.try_push(b'/')
.map_err(|_| ErrorCode::OutOfMemory)?;
} else {
array_vec.try_push(*i)
.map_err(|_| ErrorCode::OutOfMemory)?;
}
}
self.run(array_vec.as_slice(), response)
}
}