1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
   Appellation: server <module>
   Creator: FL03 <jo3mccain@icloud.com>
   Description:
       ... Summary ...
*/
use async_trait::async_trait;

/// Outline the available power-based states for servers
pub enum ServerState {
    Off,
    On,
}

impl Default for ServerState {
    fn default() -> Self {
        Self::Off
    }
}

/// Outline a typical server object
#[async_trait]
pub trait IServer<As = ServerState> {
    fn address(&self, host: crate::HostPiece, port: crate::PortPiece) -> std::net::SocketAddr {
        std::net::SocketAddr::from((host, port))
    }
    async fn client(&self) -> Result<axum::Router, scsys::BoxError>
        where
            Self: Sized;
    async fn run(&mut self, host: crate::HostPiece, port: crate::PortPiece) -> crate::AxumServer
        where
            Self: Sized + Sync,
    {
        axum::Server::bind(&self.address(host, port))
            .serve(self.client().await.ok().unwrap().into_make_service())
    }
}

#[derive(Clone, Debug, Hash, PartialEq, serde::Deserialize, serde::Serialize)]
pub struct Server {
    pub host: [u8; 4],
    pub port: u16,
}

impl Server {
    pub fn new(host: [u8; 4], port: u16) -> Self {
        Self { host, port }
    }
    pub fn extract_host_from(breakpoint: char, host: String) -> crate::HostPiece {
        scsys::extract::Extractor::new(breakpoint, host)
            .extract()
            .try_into()
            .ok()
            .unwrap()
    }
}

impl Default for Server {
    fn default() -> Self {
        Self::new([0, 0, 0, 0], 8080)
    }
}