2024-11-03 11:12:40 +01:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use crate::command::Command;
|
|
|
|
use crate::errors::ServicePointError;
|
2024-11-02 23:15:54 +01:00
|
|
|
|
|
|
|
#[derive(uniffi::Object)]
|
|
|
|
pub struct Connection {
|
|
|
|
actual: servicepoint::Connection,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[uniffi::export]
|
|
|
|
impl Connection {
|
|
|
|
#[uniffi::constructor]
|
2024-11-03 11:12:40 +01:00
|
|
|
pub fn new(host: String) -> Result<Arc<Self>, ServicePointError> {
|
2024-11-02 23:15:54 +01:00
|
|
|
servicepoint::Connection::open(host)
|
2024-11-03 11:12:40 +01:00
|
|
|
.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:?}") })
|
2024-11-02 23:15:54 +01:00
|
|
|
}
|
|
|
|
}
|