1pub mod fidl;
11
12pub(crate) mod request;
13pub(crate) mod watch;
14
15pub(crate) struct Scoped<T>(pub T);
20
21pub(crate) mod registration {
22 use super::fidl;
23 use crate::base::Dependency;
24 use crate::job::source::Seeder;
25 use fuchsia_component::server::{ServiceFsDir, ServiceObjLocal};
26 use std::collections::HashSet;
27
28 pub enum Registrar {
32 Fidl(fidl::Register),
33 #[cfg(test)]
35 Test(Box<dyn FnOnce()>),
36 #[cfg(test)]
37 TestWithSeeder(Box<dyn FnOnce(&Seeder)>),
38 }
39
40 impl Registrar {
41 pub fn register(
44 self,
45 job_seeder: &Seeder,
46 service_dir: &mut ServiceFsDir<'_, ServiceObjLocal<'_, ()>>,
47 ) {
48 match self {
49 Registrar::Fidl(register_fn) => {
50 register_fn(job_seeder, service_dir);
51 }
52 #[cfg(test)]
53 Registrar::Test(register_fn) => {
54 register_fn();
55 }
56 #[cfg(test)]
57 Registrar::TestWithSeeder(register_fn) => {
58 register_fn(job_seeder);
59 }
60 }
61 }
62 }
63
64 pub struct Registrant {
66 interface: String,
68 registrar: Registrar,
70 dependencies: HashSet<Dependency>,
73 }
74
75 impl Registrant {
76 pub(crate) fn new(
77 interface: String,
78 registrar: Registrar,
79 dependencies: HashSet<Dependency>,
80 ) -> Registrant {
81 Registrant { interface, registrar, dependencies }
82 }
83
84 pub(crate) fn get_dependencies(&self) -> &HashSet<Dependency> {
85 &self.dependencies
86 }
87
88 pub(crate) fn get_interface(&self) -> &String {
89 &self.interface
90 }
91
92 pub(crate) fn register(
93 self,
94 job_seeder: &Seeder,
95 service_dir: &mut ServiceFsDir<'_, ServiceObjLocal<'_, ()>>,
96 ) {
97 self.registrar.register(job_seeder, service_dir);
98 }
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::registration::{Registrant, Registrar};
105 use crate::base::{Dependency, Entity, SettingType};
106 use crate::job::source::Seeder;
107 use crate::message::base::MessengerType;
108 use crate::service;
109 use assert_matches::assert_matches;
110 use fuchsia_component::server::ServiceFs;
111
112 #[fuchsia::test(allow_stalls = false)]
113 async fn test_registration() {
114 let (tx, rx) = futures::channel::oneshot::channel::<()>();
115 let dependency = Dependency::Entity(Entity::Handler(SettingType::Unknown));
116 let registrant = Registrant::new(
117 "Registrar::Test".to_string(),
118 Registrar::Test(Box::new(move || {
119 assert!(tx.send(()).is_ok());
120 })),
121 [dependency].into(),
122 );
123
124 let mut fs = ServiceFs::new_local();
125
126 assert!(registrant.get_dependencies().contains(&dependency));
128
129 let delegate = service::MessageHub::create_hub();
130 let job_manager_signature = delegate
131 .create(MessengerType::Unbound)
132 .await
133 .expect("messenger should be created")
134 .0
135 .get_signature();
136 let job_seeder = Seeder::new(&delegate, job_manager_signature).await;
137
138 registrant.register(&job_seeder, &mut fs.root_dir());
140
141 assert_matches!(rx.await, Ok(()));
143 }
144}