use std::future::{Future, IntoFuture};
use std::pin::Pin;
use std::time::Duration;
use super::McClient;
use crate::error::McError;
use crate::models::ServerEdition;
use crate::status::{BedrockServerStatus, JavaServerStatus};
pub struct JavaPingBuilder {
client: McClient,
address: String,
timeout: Option<Duration>,
}
impl JavaPingBuilder {
pub(crate) fn new(client: McClient, address: impl Into<String>) -> Self {
Self {
client,
address: address.into(),
timeout: None,
}
}
pub fn timeout(mut self, duration: Duration) -> Self {
self.timeout = Some(duration);
self
}
}
impl IntoFuture for JavaPingBuilder {
type Output = Result<JavaServerStatus, McError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let client = match self.timeout {
Some(t) => { let mut c = self.client.clone(); c.timeout = t; c },
None => self.client.clone(),
};
client.ping_java_inner(&self.address).await
})
}
}
pub struct BedrockPingBuilder {
client: McClient,
address: String,
timeout: Option<Duration>,
}
impl BedrockPingBuilder {
pub(crate) fn new(client: McClient, address: impl Into<String>) -> Self {
Self {
client,
address: address.into(),
timeout: None,
}
}
pub fn timeout(mut self, duration: Duration) -> Self {
self.timeout = Some(duration);
self
}
}
impl IntoFuture for BedrockPingBuilder {
type Output = Result<BedrockServerStatus, McError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let client = match self.timeout {
Some(t) => { let mut c = self.client.clone(); c.timeout = t; c },
None => self.client.clone(),
};
client.ping_bedrock_inner(&self.address).await
})
}
}
pub struct ServerPingBuilder {
client: McClient,
address: String,
edition: ServerEdition,
timeout: Option<Duration>,
}
impl ServerPingBuilder {
pub(crate) fn new(
client: McClient,
address: impl Into<String>,
edition: ServerEdition,
) -> Self {
Self {
client,
address: address.into(),
edition,
timeout: None,
}
}
pub fn timeout(mut self, duration: Duration) -> Self {
self.timeout = Some(duration);
self
}
pub async fn is_online(self) -> bool {
self.await.is_ok()
}
}
impl IntoFuture for ServerPingBuilder {
type Output = Result<crate::models::ServerStatus, McError>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move {
let client = match self.timeout {
Some(t) => { let mut c = self.client.clone(); c.timeout = t; c },
None => self.client.clone(),
};
match self.edition {
ServerEdition::Java => {
client.ping_java_inner(&self.address).await.map(|s| s.0)
}
ServerEdition::Bedrock => {
client.ping_bedrock_inner(&self.address).await.map(|s| s.0)
}
}
})
}
}