1use crate::{Listener, Listeners};
2use std::net::SocketAddr;
3use trillium_http::{HttpConfig, HttpContext, Swansong, TypeSet, type_set::entry::Entry};
4
5#[derive(Debug, Default)]
11pub struct Info(HttpContext);
12impl From<HttpContext> for Info {
13 fn from(value: HttpContext) -> Self {
14 Self(value)
15 }
16}
17impl From<Info> for HttpContext {
18 fn from(value: Info) -> Self {
19 value.0
20 }
21}
22
23impl AsRef<TypeSet> for Info {
24 fn as_ref(&self) -> &TypeSet {
25 self.0.as_ref()
26 }
27}
28impl AsMut<TypeSet> for Info {
29 fn as_mut(&mut self) -> &mut TypeSet {
30 self.0.as_mut()
31 }
32}
33
34impl Info {
35 pub fn tcp_socket_addr(&self) -> Option<&SocketAddr> {
38 self.shared_state()
39 }
40
41 pub fn listeners(&self) -> &[Listener] {
45 self.shared_state::<Listeners>().map_or(&[], |l| &l.0)
46 }
47
48 #[cfg(unix)]
51 pub fn unix_socket_addr(&self) -> Option<&std::os::unix::net::SocketAddr> {
52 self.shared_state()
53 }
54
55 pub fn shared_state<T: Send + Sync + 'static>(&self) -> Option<&T> {
57 self.0.shared_state().get()
58 }
59
60 pub fn insert_shared_state<T: Send + Sync + 'static>(&mut self, value: T) -> Option<T> {
62 self.0.shared_state_mut().insert(value)
63 }
64
65 pub fn shared_state_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T> {
67 self.0.shared_state_mut().get_mut()
68 }
69
70 pub fn shared_state_entry<T: Send + Sync + 'static>(&mut self) -> Entry<'_, T> {
72 self.0.shared_state_mut().entry()
73 }
74
75 #[must_use]
77 pub fn with_shared_state<T: Send + Sync + 'static>(mut self, value: T) -> Self {
78 self.insert_shared_state(value);
79 self
80 }
81
82 pub fn config(&self) -> &HttpConfig {
84 self.0.config()
85 }
86
87 pub fn config_mut(&mut self) -> &mut HttpConfig {
89 self.0.config_mut()
90 }
91
92 pub fn swansong(&self) -> &Swansong {
94 self.0.swansong()
95 }
96}