diff --git a/crates/servicepoint/src/connection.rs b/crates/servicepoint/src/connection.rs index 456ae5d..76945c3 100644 --- a/crates/servicepoint/src/connection.rs +++ b/crates/servicepoint/src/connection.rs @@ -9,17 +9,38 @@ use crate::Packet; /// /// # Examples /// ```rust -/// # use servicepoint::Command; -/// let connection = servicepoint::Connection::open("172.23.42.29:2342") +/// # use servicepoint::{Command, Connection}; +/// let connection = servicepoint::UdpConnection::open("172.23.42.29:2342") /// .expect("connection failed"); -/// connection.send(Command::Clear) -/// .expect("send failed"); +/// connection.send(Command::Clear); /// ``` -pub struct Connection { +pub trait Connection { + /// Send something packet-like to the display. Usually this is in the form of a Command. + /// + /// # Arguments + /// + /// - `packet`: the packet-like to send + /// + /// returns: true if packet was sent, otherwise false + /// + /// # Examples + /// + /// ```rust + /// # use servicepoint::{Command, Connection }; + /// # let connection = servicepoint::UdpConnection::open("172.23.42.29:2342") + /// # .expect("connection failed"); + /// // turn off all pixels on display + /// connection.send(Command::Clear); + /// ``` + fn send(&self, packet: impl Into) -> bool; +} + +/// A real connection using the UDP protocol +pub struct UdpConnection { socket: UdpSocket, } -impl Connection { +impl UdpConnection { /// Open a new UDP socket and connect to the provided host. /// /// Note that this is UDP, which means that the open call can succeed even if the display is unreachable. @@ -39,37 +60,22 @@ impl Connection { socket.connect(addr)?; Ok(Self { socket }) } +} - /// Send something packet-like to the display. Usually this is in the form of a Command. - /// - /// # Arguments - /// - /// - `packet`: the packet-like to send - /// - /// returns: Ok if packet was sent, otherwise socket error - /// - /// # Errors - /// - /// Any errors produced while sending using the underlying socket. - /// - /// # Examples - /// - /// ```rust - /// # use servicepoint::{Command, CompressionCode, Grid, PixelGrid}; - /// # let connection = servicepoint::Connection::open("172.23.42.29:2342") - /// # .expect("connection failed"); - /// // turn off all pixels on display - /// connection.send(Command::Clear) - /// .expect("send failed"); - /// ``` - pub fn send( - &self, - packet: impl Into, - ) -> Result<(), std::io::Error> { +impl Connection for UdpConnection { + fn send(&self, packet: impl Into) -> bool { let packet = packet.into(); debug!("sending {packet:?}"); let data: Vec = packet.into(); - self.socket.send(&data)?; - Ok(()) + self.socket.send(&data).is_err() + } +} + +/// A fake connection for testing that does not actually send anything +pub struct NoopConnection; + +impl Connection for NoopConnection { + fn send(&self, packet: impl Into) -> bool { + true } } diff --git a/crates/servicepoint/src/lib.rs b/crates/servicepoint/src/lib.rs index d71e1fa..a8956bb 100644 --- a/crates/servicepoint/src/lib.rs +++ b/crates/servicepoint/src/lib.rs @@ -39,7 +39,7 @@ use bitvec::prelude::{BitVec, Msb0}; pub use crate::brightness::{Brightness, BrightnessGrid}; pub use crate::command::{Command, Cp437Grid, Offset}; pub use crate::compression_code::CompressionCode; -pub use crate::connection::Connection; +pub use crate::connection::{Connection, UdpConnection}; pub use crate::data_ref::DataRef; pub use crate::grid::Grid; pub use crate::origin::{Origin, Pixels, Tiles};