send command with connection
This commit is contained in:
parent
07a1b8810c
commit
0dc1394935
11 changed files with 299 additions and 122 deletions
|
@ -1,20 +1,37 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
#[uniffi::export]
|
||||
trait Command: Send + Sync {}
|
||||
use crate::errors::ServicePointError;
|
||||
|
||||
#[derive(uniffi::Object)]
|
||||
pub struct Clear {
|
||||
actual: servicepoint::Command,
|
||||
pub struct Command {
|
||||
pub(crate)actual: servicepoint::Command
|
||||
}
|
||||
|
||||
fn actual_into_arc(actual: servicepoint::Command) -> Arc<Command> {
|
||||
Arc::new(Command { actual })
|
||||
}
|
||||
#[uniffi::export]
|
||||
impl Command for Clear {}
|
||||
|
||||
#[uniffi::export]
|
||||
impl Clear {
|
||||
impl Command {
|
||||
#[uniffi::constructor]
|
||||
pub fn new() -> Arc<Self> {
|
||||
let actual = servicepoint::Command::Clear;
|
||||
Arc::new(Clear { actual })
|
||||
pub fn clear() -> Arc<Self> {
|
||||
actual_into_arc(servicepoint::Command::Clear)
|
||||
}
|
||||
|
||||
#[uniffi::constructor]
|
||||
pub fn brightness(brightness: u8) -> Result<Arc<Self>, ServicePointError> {
|
||||
servicepoint::Brightness::try_from(brightness)
|
||||
.map_err(move |value| ServicePointError::InvalidBrightness{value})
|
||||
.map(servicepoint::Command::Brightness)
|
||||
.map(actual_into_arc)
|
||||
}
|
||||
|
||||
#[uniffi::constructor]
|
||||
pub fn fade_out() -> Arc<Self> {
|
||||
actual_into_arc(servicepoint::Command::FadeOut)
|
||||
}
|
||||
|
||||
#[uniffi::constructor]
|
||||
pub fn hard_reset() -> Arc<Self> {
|
||||
actual_into_arc(servicepoint::Command::HardReset)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,23 +1,29 @@
|
|||
use std::{sync::Arc};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::command::Command;
|
||||
use crate::errors::ServicePointError;
|
||||
|
||||
#[derive(uniffi::Object)]
|
||||
pub struct Connection {
|
||||
actual: servicepoint::Connection,
|
||||
}
|
||||
|
||||
#[derive(uniffi::Error, thiserror::Error, Debug)]
|
||||
pub enum ConnectionError {
|
||||
#[error("An IO error occured: {error}")]
|
||||
IOError {
|
||||
error: String}
|
||||
}
|
||||
|
||||
#[uniffi::export]
|
||||
impl Connection {
|
||||
#[uniffi::constructor]
|
||||
pub fn new(host: String) -> Result<Arc<Self>, ConnectionError> {
|
||||
pub fn new(host: String) -> Result<Arc<Self>, ServicePointError> {
|
||||
servicepoint::Connection::open(host)
|
||||
.map(|actual|Arc::new(Connection { actual}) )
|
||||
.map_err(|err| ConnectionError::IOError { error: err.to_string()})
|
||||
.map(|actual| Arc::new(Connection { actual }))
|
||||
.map_err(|err| ServicePointError::IOError { error: err.to_string() })
|
||||
}
|
||||
|
||||
#[uniffi::constructor]
|
||||
pub fn new_fake() -> Arc<Self> {
|
||||
Arc::new(Self { actual: servicepoint::Connection::Fake })
|
||||
}
|
||||
|
||||
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
|
||||
self.actual.send(command.actual.clone())
|
||||
.map_err(|err| ServicePointError::IOError { error: format!("{err:?}") })
|
||||
}
|
||||
}
|
||||
|
|
8
crates/servicepoint_binding_uniffi/src/errors.rs
Normal file
8
crates/servicepoint_binding_uniffi/src/errors.rs
Normal file
|
@ -0,0 +1,8 @@
|
|||
|
||||
#[derive(uniffi::Error, thiserror::Error, Debug)]
|
||||
pub enum ServicePointError {
|
||||
#[error("An IO error occurred: {error}")]
|
||||
IOError {error: String},
|
||||
#[error("The specified brightness value {value} is out of range")]
|
||||
InvalidBrightness {value:u8},
|
||||
}
|
|
@ -2,3 +2,4 @@ uniffi::setup_scaffolding!();
|
|||
|
||||
mod command;
|
||||
mod connection;
|
||||
mod errors;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue