1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
use std::future::Future;

use crate::device::command::Command;
use crate::error::Error;


pub trait Out: In {
	fn send(&self, data: &[u8]) -> impl Future<Output = Result<usize, Error>>;

	fn send_cmd(&self, cmd: Command) -> impl Future<Output = Result<usize, Error>> {
		async move {
			let mut pre = 0;
			if !matches!(cmd, Command::Echo { .. }) {
				use crate::device::command::Switch;

				trace!("send cmd: echo off");
				let echo = Command::Echo { value: Switch::Off };
				pre = self.send(echo.with_break().as_bytes()).await?;
			}

			trace!("send cmd: {cmd}");
			let sent = self.send(cmd.with_break().as_bytes()).await?;
			Ok(pre + sent)
		}
	}
}

pub trait In {}

pub trait Interface: Out {}
impl<T: In + Out> Interface for T {}