arcs_ctf_yaml_parser/deploy/
error.rs

1use std::fmt::Display;
2
3use crate::structs::ValueType;
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum ExposeError {
7    Missing,
8    BadFormat(String),
9    BadParts {
10        data: String,
11        port: bool,
12        protocol: bool,
13    }
14}
15impl Display for ExposeError {
16    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17        match self {
18            Self::Missing => write!(f, "You must specify what port and protocol to expose."),
19            Self::BadFormat(s) => write!(f, "The `expose` value must follos the format <port>/<protocol>. `{s}` does not match. (Protocols are `udp` and `tcp`)"),
20            Self::BadParts {
21                data,
22                port,
23                protocol,
24            } => if *port && *protocol {
25                write!(f, "The port must be a number and the protocol must be `tcp` or `udp`. ({data} was recieved)")
26            } else if *port {
27                write!(f, "The port must be a number. ({data} was recieved)")
28            } else {
29                write!(f, "The protocol must be `tcp` or `udp`. ({data} was recieved)")
30            },
31        }
32    }
33}
34
35#[derive(Debug, Clone, PartialEq)]
36pub enum BuildError {
37    BadType(ValueType),
38    NotPath(String),
39    NotRelative(std::path::PathBuf),
40}
41
42impl Display for BuildError {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        match self {
45            Self::BadType(t) => writeln!(f, "Build should be a relative path, not {t}."),
46            Self::NotPath(s) => writeln!(f, "Build should be a VALID relative path. \"{s}\" is not a valid path."),
47            Self::NotRelative(p) => writeln!(f, "Build should be a RELATIVE path. \"{}\" is not a relative path.", p.display()),
48        }
49    }
50}
51
52#[derive(Debug, Clone, PartialEq)]
53pub enum DeploymentTargetOptionsError {
54    BadBaseType(ValueType),
55    Parts {
56        expose: Option<ExposeError>,
57        replicas_invalid: Option<ValueType>, 
58        build: Option<BuildError>,   
59    }
60}
61
62impl Display for DeploymentTargetOptionsError {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        match self {
65            Self::BadBaseType(t) => writeln!(f, "A target should be a map with values for `expose` and (optionally) `replicas`, not {t}."),
66            Self::Parts {
67                expose,
68                replicas_invalid,
69                build,
70            } => {
71                writeln!(f, "There were issues with certain parts of this target:")?;
72                if let Some(expose_error) = expose {
73                    writeln!(f, "            {expose_error}")?;
74                }
75                if let Some(invalid_type) = replicas_invalid {
76                    writeln!(f, "            `replicas` should be a number from 1 - 255, not {invalid_type}.")?;
77                }
78                if let Some(build) = build {
79                    writeln!(f, "            {build}.")?;
80                }
81                Ok(())
82            }
83        }
84    }
85}
86
87#[derive(Debug, Clone, PartialEq)]
88pub enum DeployOptionsError {
89    Parts {
90        web: Box<Option<DeploymentTargetOptionsError>>,
91        admin: Box<Option<DeploymentTargetOptionsError>>,
92        nc: Box<Option<DeploymentTargetOptionsError>>,
93    },
94    BadBaseType(ValueType),
95}
96impl Display for DeployOptionsError {
97    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98        match self {
99            Self::BadBaseType(t) => writeln!(f, "Deploy should be a map of `web`, `admin`, and `nc`, not {t}."),
100            Self::Parts {
101                web,
102                admin,
103                nc
104            } => {
105                writeln!(f, "There were issues with certain deployment targets:")?;
106                if let Some(web  ) = &**web   { writeln!(f, "        web:   {web}")?;   }
107                if let Some(admin) = &**admin { writeln!(f, "        admin: {admin}")?; }
108                if let Some(nc   ) = &**nc    { writeln!(f, "        nc:    {nc}")?;    }
109                Ok(())
110            }
111        }
112    }
113}