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