aboutsummaryrefslogtreecommitdiff
path: root/azalea-protocol/src/lib.rs
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2021-12-15 23:10:55 -0600
committermat <github@matdoes.dev>2021-12-15 23:10:55 -0600
commit9642558f8f8d983a7087f15d68be8cf07a85f0c2 (patch)
tree5f0a967f005cd5db510a13ab290c8ad6669b25aa /azalea-protocol/src/lib.rs
parent72aefe871ca4983431b1a0b707b472e73ffea836 (diff)
downloadazalea-drasl-9642558f8f8d983a7087f15d68be8cf07a85f0c2.tar.xz
azalea
Diffstat (limited to 'azalea-protocol/src/lib.rs')
-rw-r--r--azalea-protocol/src/lib.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/azalea-protocol/src/lib.rs b/azalea-protocol/src/lib.rs
new file mode 100644
index 00000000..684add45
--- /dev/null
+++ b/azalea-protocol/src/lib.rs
@@ -0,0 +1,56 @@
+//! This lib is responsible for parsing Minecraft packets.
+
+use std::net::IpAddr;
+use std::str::FromStr;
+
+pub mod connect;
+pub mod mc_buf;
+pub mod packets;
+pub mod read;
+pub mod resolver;
+pub mod write;
+
+#[derive(Debug)]
+pub struct ServerAddress {
+ pub host: String,
+ pub port: u16,
+}
+
+#[derive(Debug)]
+pub struct ServerIpAddress {
+ pub ip: IpAddr,
+ pub port: u16,
+}
+
+// impl try_from for ServerAddress
+impl<'a> TryFrom<&'a str> for ServerAddress {
+ type Error = String;
+
+ /// Convert a Minecraft server address (host:port, the port is optional) to a ServerAddress
+ fn try_from(string: &str) -> Result<Self, Self::Error> {
+ if string.is_empty() {
+ return Err("Empty string".to_string());
+ }
+ let mut parts = string.split(':');
+ let host = parts.next().ok_or("No host specified")?.to_string();
+ // default the port to 25565
+ let port = parts.next().unwrap_or("25565");
+ let port = u16::from_str(port).map_err(|_| "Invalid port specified")?;
+ Ok(ServerAddress { host, port })
+ }
+}
+
+pub async fn connect(address: ServerAddress) -> Result<(), Box<dyn std::error::Error>> {
+ let resolved_address = resolver::resolve_address(&address).await;
+ println!("Resolved address: {:?}", resolved_address);
+ Ok(())
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn it_works() {
+ let result = 2 + 2;
+ assert_eq!(result, 4);
+ }
+}