fabaccess-bffh/bffhd/capnp/resources.rs

49 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2024-05-14 11:05:39 +02:00
use crate::capnp::resource::Machine;
use crate::resources::search::ResourcesHandle;
use crate::resources::Resource;
use crate::session::SessionHandle;
use crate::RESOURCES;
use api::resources_capnp::resources::{GetByNameParams, GetByNameResults, Server as ResourcesServer};
use capnp::capability::Promise;
use capnp::Error;
use capnp_rpc::pry;
use tracing::Span;
const TARGET: &str = "bffh::api::machinesystem";
#[derive(Clone)]
pub struct Machines {
span: Span,
session: SessionHandle,
resources: ResourcesHandle,
}
impl Machines {
pub fn new(session: SessionHandle) -> Self {
let span = tracing::info_span!(
target: TARGET,
parent: &session.span,
"MachineSystem",
);
// FIXME no unwrap bad
Self {
span,
session,
resources: RESOURCES.get().unwrap().clone(),
}
}
}
impl ResourcesServer for Machines {
fn get_by_name(&mut self, params: GetByNameParams, mut results: GetByNameResults) -> Promise<(), Error> {
let params = pry!(pry!(params.get()).get_name());
if let Some(r) = self.resources.get_by_id(params) {
let mut builder = results.get().init_ok();
let machine = Machine::new(self.session.clone(), r.clone());
machine.fill_resource(&mut builder);
}
Promise::ok(())
}
}