pub mod careersavegame;
pub mod stats;
use std::{
borrow::Cow,
fmt
};
#[derive(Debug, Clone, Copy)]
pub enum Format {
Json,
Xml
}
impl fmt::Display for Format {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>
) -> fmt::Result {
let ext = match self {
Self::Json => "json",
Self::Xml => "xml"
};
write!(f, "{ext}")
}
}
#[derive(Debug, Clone, Copy)]
pub enum Filename {
CareerSavegame,
Vehicles,
Economy
}
impl fmt::Display for Filename {
fn fmt(
&self,
f: &mut fmt::Formatter<'_>
) -> fmt::Result {
let file = match self {
Self::CareerSavegame => "careerSavegame",
Self::Vehicles => "vehicles",
Self::Economy => "economy"
};
write!(f, "{file}")
}
}
pub struct EndpointBuilder {
ip: Cow<'static, str>,
code: Cow<'static, str>,
format: Format
}
impl EndpointBuilder {
pub fn new(
ip: &str,
code: &str
) -> Self {
let ip = ip.strip_prefix("http://").expect("failed to strip http protocol");
Self {
ip: Cow::Owned(ip.into()),
code: Cow::Owned(code.into()),
format: Format::Json
}
}
pub fn format(
mut self,
format: Format
) -> Self {
self.format = format;
self
}
pub fn build(self) -> Endpoint { Endpoint::new(self.ip, self.code, self.format) }
}
pub struct Endpoint {
base_url: Cow<'static, str>,
code: Cow<'static, str>,
format: Format
}
impl Endpoint {
fn new(
ip: Cow<'static, str>,
code: Cow<'static, str>,
format: Format
) -> Self {
Self {
base_url: Cow::Owned(format!("http://{ip}/feed")),
code,
format
}
}
pub fn stats(&self) -> String { format!("{}/dedicated-server-stats.{}?code={}", self.base_url, self.format, self.code) }
pub fn savegame(
&self,
filename: Filename
) -> String {
format!("{}/dedicated-server-savegame.html?code={}&file={filename}", self.base_url, self.code)
}
}