openlegends_client/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
use std::{
    io::{Read, Result, Write},
    net::TcpStream,
};

pub struct Client {
    stream: TcpStream,
}

impl Client {
    pub fn new(host: &str, port: i8) -> Result<Self> {
        Ok(Self {
            stream: TcpStream::connect(format!("{}:{}", host, port))?,
        })
    }

    pub fn status(&mut self) -> Result<String> {
        self.stream.write_all(b"status")?;

        let mut buffer = [0; 1024];
        let bytes_read = self.stream.read(&mut buffer)?;

        Ok(String::from_utf8_lossy(&buffer[..bytes_read]).to_string())
    }
}