use std::borrow::Cow;
use std::fmt::Display;
use serde::{Deserialize, Serialize};
use crate::api::server::ServerId;
use crate::api::{wrapper::Single, UnauthenticatedRequest};
use crate::{error::Error, AsyncRobot};
fn get_windows_config(server_number: ServerId) -> UnauthenticatedRequest<Single<Windows>> {
UnauthenticatedRequest::from(&format!(
"https://robot-ws.your-server.de/boot/{server_number}/windows"
))
}
fn enable_windows_config(
server_number: ServerId,
config: WindowsConfig,
) -> Result<UnauthenticatedRequest<Single<ActiveWindowsConfig>>, serde_html_form::ser::Error> {
UnauthenticatedRequest::from(&format!(
"https://robot-ws.your-server.de/boot/{server_number}/windows"
))
.with_method("POST")
.with_body(config)
}
fn disable_windows_config(
server_number: ServerId,
) -> UnauthenticatedRequest<Single<AvailableWindowsConfig>> {
UnauthenticatedRequest::from(&format!(
"https://robot-ws.your-server.de/boot/{server_number}/windows"
))
.with_method("DELETE")
}
fn get_last_windows_config(
server_number: ServerId,
) -> UnauthenticatedRequest<Single<ActiveWindowsConfig>> {
UnauthenticatedRequest::from(&format!(
"https://robot-ws.your-server.de/boot/{server_number}/windows/last"
))
}
impl AsyncRobot {
pub async fn get_windows_config(&self, server_number: ServerId) -> Result<Windows, Error> {
Ok(self.go(get_windows_config(server_number)).await?.0)
}
pub async fn get_last_windows_config(
&self,
server_number: ServerId,
) -> Result<ActiveWindowsConfig, Error> {
Ok(self.go(get_last_windows_config(server_number)).await?.0)
}
pub async fn enable_windows_config(
&self,
server_number: ServerId,
config: WindowsConfig,
) -> Result<ActiveWindowsConfig, Error> {
Ok(self
.go(enable_windows_config(server_number, config)?)
.await?
.0)
}
pub async fn disable_windows_config(
&self,
server_number: ServerId,
) -> Result<AvailableWindowsConfig, Error> {
Ok(self.go(disable_windows_config(server_number)).await?.0)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ActiveWindowsConfig {
#[serde(rename = "dist")]
pub distribution: WindowsDistribution,
#[serde(rename = "lang")]
pub language: String,
pub password: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct AvailableWindowsConfig {
#[serde(rename = "dist")]
pub distributions: Vec<WindowsDistribution>,
#[serde(rename = "lang")]
pub language: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum Windows {
Active(ActiveWindowsConfig),
Available(AvailableWindowsConfig),
}
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
pub struct WindowsConfig {
#[serde(rename = "dist")]
pub distribution: WindowsDistribution,
#[serde(rename = "lang")]
pub language: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WindowsDistribution(pub Cow<'static, str>);
impl From<String> for WindowsDistribution {
fn from(value: String) -> Self {
WindowsDistribution(Cow::from(value))
}
}
impl From<&'static str> for WindowsDistribution {
fn from(value: &'static str) -> Self {
WindowsDistribution(Cow::from(value))
}
}
impl Display for WindowsDistribution {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl PartialEq<str> for WindowsDistribution {
fn eq(&self, other: &str) -> bool {
self.0.eq(other)
}
}