servicepoint/crates/servicepoint_binding_uniffi/src/connection.rs

37 lines
957 B
Rust
Raw Normal View History

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(),
})
2024-11-03 11:12:40 +01:00
}
#[uniffi::constructor]
pub fn new_fake() -> Arc<Self> {
Arc::new(Self {
actual: servicepoint::Connection::Fake,
})
2024-11-03 11:12:40 +01:00
}
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
}
}