mittens_engine/engine/ecs/component/
http_server.rs1use super::Component;
2use crate::engine::ecs::component::ce_helpers::*;
3use crate::engine::ecs::{ComponentId, IntentValue, SignalEmitter};
4
5#[derive(Debug, Clone, Default)]
6pub struct HttpServerComponent {
7 pub bind_addr: String,
8 pub enabled: bool,
9 component: Option<ComponentId>,
10}
11
12impl HttpServerComponent {
13 pub fn new() -> Self {
14 Self {
15 bind_addr: String::new(),
16 enabled: true,
17 component: None,
18 }
19 }
20
21 pub fn bind(bind_addr: impl Into<String>) -> Self {
22 Self::new().with_bind_addr(bind_addr)
23 }
24
25 pub fn with_bind_addr(mut self, bind_addr: impl Into<String>) -> Self {
26 self.bind_addr = bind_addr.into();
27 self
28 }
29
30 pub fn with_enabled(mut self, enabled: bool) -> Self {
31 self.enabled = enabled;
32 self
33 }
34}
35
36impl Component for HttpServerComponent {
37 fn name(&self) -> &'static str {
38 "http_server"
39 }
40
41 fn set_id(&mut self, component: ComponentId) {
42 self.component = Some(component);
43 }
44
45 fn as_any(&self) -> &dyn std::any::Any {
46 self
47 }
48
49 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
50 self
51 }
52
53 fn init(&mut self, emit: &mut dyn SignalEmitter, component: ComponentId) {
54 emit.push_intent_now(
55 component,
56 IntentValue::RegisterHttpServer {
57 component_ids: vec![component],
58 },
59 );
60 }
61
62 fn to_mms_ast(
63 &self,
64 _world: &crate::engine::ecs::World,
65 ) -> crate::scripting::ast::ComponentExpression {
66 let mut ce = if self.bind_addr.is_empty() {
67 ce("HttpServer")
68 } else {
69 ce_call("HttpServer", "bind", vec![s(&self.bind_addr)])
70 };
71 if !self.enabled {
72 ce = ce.with_call("enabled", vec![b(false)]);
73 }
74 ce
75 }
76}