use crate::{
actors::scanmgr::{error::Error, messages::InvokeScan, reply::ScanResult, ScanMgr},
userscript_api::{
include::{LuaExternalError, LuaUserDataRef},
ApiObject,
},
};
use kameo::actor::WeakActorRef;
use mlua::UserData;
pub struct ScanMgrApi(WeakActorRef<ScanMgr>);
impl ScanMgrApi {
#[must_use]
pub fn new(scanmgr: WeakActorRef<ScanMgr>) -> Self {
Self(scanmgr)
}
}
impl UserData for ScanMgrApi {
fn add_methods<M: mlua::UserDataMethods<Self>>(methods: &mut M) {
methods.add_async_method(
"scan",
|_, this: LuaUserDataRef<ScanMgrApi>, ()| async move {
let Some(scanmgr) = this.0.upgrade() else {
return Err(Error::NoScanMgr.into_lua_err());
};
let results: Vec<ScanResult> = scanmgr
.ask(InvokeScan)
.await
.map_err(LuaExternalError::into_lua_err)?;
Ok(results)
},
);
}
}
impl ApiObject for ScanMgrApi {
fn name(&self) -> &'static str {
"scanmgr"
}
}