wrap and rename ALL the types
This commit is contained in:
parent
b9fc06117e
commit
051dbfabea
18 changed files with 577 additions and 480 deletions
|
@ -9,38 +9,24 @@ use crate::Packet;
|
|||
///
|
||||
/// # Examples
|
||||
/// ```rust
|
||||
/// # use servicepoint::{Command, Connection};
|
||||
/// let connection = servicepoint::UdpConnection::open("172.23.42.29:2342")
|
||||
/// let connection = servicepoint::Connection::open("172.23.42.29:2342")
|
||||
/// .expect("connection failed");
|
||||
/// connection.send(Command::Clear);
|
||||
/// connection.send(servicepoint::Command::Clear)
|
||||
/// .expect("send failed");
|
||||
/// ```
|
||||
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<Packet>) -> bool;
|
||||
pub enum Connection {
|
||||
/// A real connection using the UDP protocol
|
||||
Udp(UdpSocket),
|
||||
/// A fake connection for testing that does not actually send anything
|
||||
Fake,
|
||||
}
|
||||
|
||||
/// A real connection using the UDP protocol
|
||||
pub struct UdpConnection {
|
||||
socket: UdpSocket,
|
||||
#[derive(Debug)]
|
||||
pub enum SendError {
|
||||
IoError(std::io::Error),
|
||||
}
|
||||
|
||||
impl UdpConnection {
|
||||
impl Connection {
|
||||
/// 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.
|
||||
|
@ -58,24 +44,37 @@ impl UdpConnection {
|
|||
info!("connecting to {addr:?}");
|
||||
let socket = UdpSocket::bind("0.0.0.0:0")?;
|
||||
socket.connect(addr)?;
|
||||
Ok(Self { socket })
|
||||
Ok(Self::Udp(socket))
|
||||
}
|
||||
}
|
||||
|
||||
impl Connection for UdpConnection {
|
||||
fn send(&self, packet: impl Into<Packet>) -> bool {
|
||||
/// 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
|
||||
/// # let connection = servicepoint::Connection::Fake;
|
||||
/// // turn off all pixels on display
|
||||
/// connection.send(servicepoint::Command::Clear)
|
||||
/// .expect("send failed");
|
||||
/// ```
|
||||
pub fn send(&self, packet: impl Into<Packet>) -> Result<(), SendError> {
|
||||
let packet = packet.into();
|
||||
debug!("sending {packet:?}");
|
||||
let data: Vec<u8> = packet.into();
|
||||
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<Packet>) -> bool {
|
||||
true
|
||||
match self {
|
||||
Connection::Udp(socket) => {
|
||||
socket
|
||||
.send(&data)
|
||||
.map_err(move |io_err| SendError::IoError(io_err))
|
||||
.map(move |_| ()) // ignore Ok value
|
||||
}
|
||||
Connection::Fake => Ok(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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, UdpConnection};
|
||||
pub use crate::connection::Connection;
|
||||
pub use crate::data_ref::DataRef;
|
||||
pub use crate::grid::Grid;
|
||||
pub use crate::origin::{Origin, Pixels, Tiles};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue