fabaccess-bffh/bffhd/capnp/machinesystem.rs

81 lines
2.3 KiB
Rust
Raw Permalink Normal View History

2022-05-05 15:50:44 +02:00
use crate::capnp::machine::Machine;
use crate::resources::search::ResourcesHandle;
use crate::resources::Resource;
2022-03-12 17:31:53 +01:00
use crate::session::SessionHandle;
2022-05-05 15:50:44 +02:00
use crate::RESOURCES;
use api::machinesystem_capnp::machine_system::info;
2022-03-12 17:31:53 +01:00
use capnp::capability::Promise;
2022-03-13 17:29:21 +01:00
use capnp_rpc::pry;
2021-12-06 21:53:42 +01:00
2022-03-13 17:29:21 +01:00
#[derive(Clone)]
pub struct Machines {
2022-03-12 17:31:53 +01:00
session: SessionHandle,
2022-03-13 17:29:21 +01:00
resources: ResourcesHandle,
2021-12-06 21:53:42 +01:00
}
impl Machines {
2022-03-12 17:31:53 +01:00
pub fn new(session: SessionHandle) -> Self {
2022-03-13 20:33:26 +01:00
// FIXME no unwrap bad
2022-05-05 15:50:44 +02:00
Self {
session,
resources: RESOURCES.get().unwrap().clone(),
}
2021-12-06 21:53:42 +01:00
}
}
2022-03-12 17:31:53 +01:00
impl info::Server for Machines {
fn get_machine_list(
&mut self,
_: info::GetMachineListParams,
2022-03-13 17:29:21 +01:00
mut result: info::GetMachineListResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-05-05 15:50:44 +02:00
let machine_list: Vec<(usize, &Resource)> = self
.resources
.list_all()
2022-03-13 17:29:21 +01:00
.into_iter()
.filter(|resource| resource.visible(&self.session))
.enumerate()
.collect();
let mut builder = result.get().init_machine_list(machine_list.len() as u32);
for (i, m) in machine_list {
let resource = m.clone();
2022-03-15 20:00:43 +01:00
let mbuilder = builder.reborrow().get(i as u32);
2022-03-13 17:29:21 +01:00
Machine::build(self.session.clone(), resource, mbuilder);
}
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
2022-03-13 17:29:21 +01:00
2022-03-12 17:31:53 +01:00
fn get_machine(
&mut self,
2022-03-13 17:29:21 +01:00
params: info::GetMachineParams,
mut result: info::GetMachineResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-03-13 17:29:21 +01:00
let params = pry!(params.get());
let id = pry!(params.get_id());
if let Some(resource) = self.resources.get_by_id(id) {
2022-03-15 20:00:43 +01:00
let builder = result.get();
Machine::optional_build(self.session.clone(), resource.clone(), builder);
2022-03-13 17:29:21 +01:00
}
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
2022-03-13 17:29:21 +01:00
2022-03-12 17:31:53 +01:00
fn get_machine_u_r_n(
&mut self,
2022-03-13 17:29:21 +01:00
params: info::GetMachineURNParams,
mut result: info::GetMachineURNResults,
2022-03-12 17:31:53 +01:00
) -> Promise<(), ::capnp::Error> {
2022-03-13 17:29:21 +01:00
let params = pry!(params.get());
let urn = pry!(params.get_urn());
if let Some(resource) = self.resources.get_by_urn(urn) {
2022-03-15 20:00:43 +01:00
let builder = result.get();
Machine::optional_build(self.session.clone(), resource.clone(), builder);
2022-03-13 17:29:21 +01:00
}
Promise::ok(())
2022-03-12 17:31:53 +01:00
}
}