rust_mc_status/status/ext.rs
1//! [`StatusExt`] trait and Minecraft text formatting helpers.
2//!
3//! [`strip_formatting`] and [`truncate_str`] are pure string utilities defined
4//! in [`core::fmt`](crate::core::fmt) and re-exported here for convenience.
5
6pub use crate::core::fmt::{strip_formatting, truncate_str};
7
8/// Common interface shared by [`JavaServerStatus`](super::JavaServerStatus)
9/// and [`BedrockServerStatus`](super::BedrockServerStatus).
10///
11/// Provides access to the fields that exist on every successful ping result
12/// regardless of edition. Import this trait to call its methods:
13///
14/// ```rust,no_run
15/// use rust_mc_status::{McClient, StatusExt};
16///
17/// # #[tokio::main] async fn main() -> Result<(), rust_mc_status::McError> {
18/// let client = McClient::builder().build();
19/// let s = client.java("mc.hypixel.net").await?;
20///
21/// // These methods come from StatusExt:
22/// println!("{} ms", s.latency_ms() as u32);
23/// println!("{}", s.display_players()); // "21000/200000"
24/// println!("{}", s.ip());
25/// println!("{}", s.hostname());
26/// # Ok(()) }
27/// ```
28pub trait StatusExt {
29 /// Round-trip latency in milliseconds.
30 ///
31 /// `0.0` for responses served from the response cache
32 /// (check [`JavaServerStatus::is_cached`](super::JavaServerStatus::is_cached)).
33 fn latency_ms(&self) -> f64;
34
35 /// Resolved IP address of the server, e.g. `"172.65.197.160"`.
36 fn ip(&self) -> &str;
37
38 /// Original hostname as provided to the ping call, e.g. `"mc.hypixel.net"`.
39 fn hostname(&self) -> &str;
40
41 /// Port that was connected to, e.g. `25565`.
42 fn port(&self) -> u16;
43
44 /// `true` if the server responded successfully within the timeout.
45 fn is_online(&self) -> bool;
46
47 /// Formatted `"online/max"` player string, e.g. `"21000/200000"`.
48 ///
49 /// For edition-specific types use
50 /// [`JavaServerStatus::players_online`](super::JavaServerStatus::players_online) /
51 /// [`players_max`](super::JavaServerStatus::players_max) directly.
52 fn display_players(&self) -> String;
53}