1use std::sync::Arc;
4
5use boson_runtime::Boson;
6use serde_json::Value as JsonValue;
7
8use crate::auth::{require_admin_auth_from_env, AdminAuth};
9use crate::error::BosonAxumError;
10
11pub type HttpEnqueueActorProvider = Arc<dyn Fn() -> JsonValue + Send + Sync>;
13
14#[derive(Clone)]
18pub struct BosonState {
19 pub boson: Arc<Boson>,
21 pub admin_auth: Option<Arc<dyn AdminAuth>>,
23 pub require_admin_auth: bool,
25 pub http_enqueue_actor: Option<HttpEnqueueActorProvider>,
27}
28
29impl BosonState {
30 #[must_use]
43 pub fn new(boson: Arc<Boson>) -> Self {
44 Self {
45 boson,
46 admin_auth: None,
47 require_admin_auth: require_admin_auth_from_env(),
48 http_enqueue_actor: None,
49 }
50 }
51
52 #[must_use]
54 pub fn builder(boson: Arc<Boson>) -> BosonStateBuilder {
55 BosonStateBuilder {
56 boson,
57 admin_auth: None,
58 require_admin_auth: require_admin_auth_from_env(),
59 http_enqueue_actor: None,
60 }
61 }
62
63 #[must_use]
65 pub fn enqueue_actor_json(&self) -> JsonValue {
66 if let Some(ref provider) = self.http_enqueue_actor {
67 return provider();
68 }
69 boson_core::default_http_enqueue_actor()
70 }
71}
72
73pub struct BosonStateBuilder {
75 boson: Arc<Boson>,
76 admin_auth: Option<Arc<dyn AdminAuth>>,
77 require_admin_auth: bool,
78 http_enqueue_actor: Option<HttpEnqueueActorProvider>,
79}
80
81impl BosonStateBuilder {
82 #[must_use]
84 pub fn admin_auth(mut self, auth: Arc<dyn AdminAuth>) -> Self {
85 self.admin_auth = Some(auth);
86 self
87 }
88
89 #[must_use]
91 pub const fn require_admin_auth(mut self, require: bool) -> Self {
92 self.require_admin_auth = require;
93 self
94 }
95
96 #[must_use]
98 pub fn http_enqueue_actor(
99 mut self,
100 provider: impl Fn() -> JsonValue + Send + Sync + 'static,
101 ) -> Self {
102 self.http_enqueue_actor = Some(Arc::new(provider));
103 self
104 }
105
106 pub fn build(self) -> Result<BosonState, BosonAxumError> {
113 if self.require_admin_auth && self.admin_auth.is_none() {
114 return Err(BosonAxumError::MissingAdminAuth);
115 }
116 Ok(BosonState {
117 boson: self.boson,
118 admin_auth: self.admin_auth,
119 require_admin_auth: self.require_admin_auth,
120 http_enqueue_actor: self.http_enqueue_actor,
121 })
122 }
123}