use std::error::Error;
use std::sync::Once;
use async_trait::async_trait;
use log::LevelFilter;
use simplelog::{ColorChoice, CombinedLogger, Config, TerminalMode, TermLogger};
use tokio_test::io::{Builder, Mock};
use crate::{DEFAULT_PROTOCOL, OpenRGB, OpenRGBError};
use crate::protocol::{OpenRGBReadableStream, OpenRGBStream, OpenRGBWritableStream};
impl OpenRGBReadableStream for Mock {}
impl OpenRGBWritableStream for Mock {}
impl OpenRGBStream for Mock {}
static INIT_ONCE: Once = Once::new();
pub fn setup() -> Result<(), Box<dyn Error>> {
INIT_ONCE.call_once(|| CombinedLogger::init(vec![TermLogger::new(
LevelFilter::Info,
Config::default(),
TerminalMode::default(),
ColorChoice::Auto,
)]).expect("failed initializing logger"));
Ok(())
}
#[async_trait]
pub trait OpenRGBMockBuilder<S: OpenRGBStream> {
async fn to_client(&mut self) -> Result<OpenRGB<S>, OpenRGBError>;
fn negotiate_default_protocol(&mut self) -> &mut Self;
fn negotiate_protocol(&mut self, protocol: u32) -> &mut Self;
}
#[async_trait]
impl OpenRGBMockBuilder<Mock> for Builder {
async fn to_client(&mut self) -> Result<OpenRGB<Mock>, OpenRGBError> {
OpenRGB::new(self.build()).await
}
fn negotiate_default_protocol(&mut self) -> &mut Self {
self.negotiate_protocol(DEFAULT_PROTOCOL)
}
fn negotiate_protocol(&mut self, protocol: u32) -> &mut Self {
self
.write(b"ORGB") .write(&0_u32.to_le_bytes()) .write(&40_u32.to_le_bytes()) .write(&4_u32.to_le_bytes()) .write(&DEFAULT_PROTOCOL.to_le_bytes())
.read(b"ORGB") .read(&0_u32.to_le_bytes()) .read(&40_u32.to_le_bytes()) .read(&4_u32.to_le_bytes()) .read(&protocol.to_le_bytes()) }
}