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 }))
|
2024-11-04 21:06:01 +01:00
|
|
|
.map_err(|err| ServicePointError::IoError {
|
|
|
|
error: err.to_string(),
|
|
|
|
})
|
2024-11-03 11:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[uniffi::constructor]
|
|
|
|
pub fn new_fake() -> Arc<Self> {
|
2024-11-04 21:06:01 +01:00
|
|
|
Arc::new(Self {
|
|
|
|
actual: servicepoint::Connection::Fake,
|
|
|
|
})
|
2024-11-03 11:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(&self, command: Arc<Command>) -> Result<(), ServicePointError> {
|
2024-11-04 21:06:01 +01:00
|
|
|
self.actual.send(command.actual.clone()).map_err(|err| {
|
|
|
|
ServicePointError::IoError {
|
|
|
|
error: format!("{err:?}"),
|
|
|
|
}
|
|
|
|
})
|
2024-11-02 23:15:54 +01:00
|
|
|
}
|
|
|
|
}
|