Add some docs for titanirc_types
Diff
titanirc-types/src/lib.rs | 20 ++++++++++++++------
titanirc-types/src/primitives.rs | 2 ++
titanirc-types/src/replies.rs | 19 +++++++++++++++++++
3 files changed, 35 insertions(+), 6 deletions(-)
@@ -29,6 +29,9 @@ macro_rules! define_commands {
),* $(,)?
) => {
paste::paste! {
#[derive(Debug)]
pub enum Command<'a> {
$([<$name:camel>]([<$name:camel Command>]<'a>)),*
@@ -37,15 +40,16 @@ macro_rules! define_commands {
$(const [<$name _BYTES>]: &[u8] = stringify!($name).as_bytes();)*
impl Command<'_> {
pub fn parse(input: Bytes) -> Result<Option<Self>, nom::Err<NomError<BytesWrapper>>> {
let input = BytesWrapper::from(input);
let mut input = BytesWrapper::from(input);
let input = if let Ok((input, _)) = parse_optional_source(input.clone()) {
input
} else {
input
};
if let Ok((input_source_stripped, _)) = parse_optional_source(input.clone()) {
input = input_source_stripped;
}
let (params, command) = take_till(|c| c == b' ')(input)?;
@@ -56,6 +60,7 @@ macro_rules! define_commands {
}
}
impl std::fmt::Display for Command<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
@@ -72,6 +77,7 @@ macro_rules! define_commands {
}
impl [<$name:camel Command>]<'_> {
#[allow(unused_variables)]
pub fn parse(rest: BytesWrapper) -> Result<Self, nom::Err<nom::error::Error<BytesWrapper>>> {
$(
@@ -88,6 +94,8 @@ macro_rules! define_commands {
}
}
impl std::fmt::Display for [<$name:camel Command>]<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fmt.write_str(stringify!($name))?;
@@ -18,6 +18,8 @@ pub trait PrimitiveParser {
Self: Sized;
}
#[derive(Debug, From)]
pub enum BytesCow<'a> {
Owned(Bytes),
@@ -3,21 +3,37 @@
use crate::{primitives::*, Command};
use std::fmt::Write;
#[derive(Debug)]
pub enum Source<'a> {
User(Nick<'a>), Server,
}
impl<'a> From<Nick<'a>> for Source<'a> {
fn from(other: Nick<'a>) -> Self {
Self::User(other)
}
}
#[derive(Debug, derive_more::From)]
pub enum ServerMessage<'a> {
Reply(Reply<'a>),
Command(Source<'a>, Command<'a>), Ping,
Pong,
}
impl ServerMessage<'_> {
pub fn write(self, server_name: &str, client_username: &str, dst: &mut bytes::BytesMut) {
match self {
Self::Reply(reply) => write!(
@@ -48,6 +64,7 @@ macro_rules! define_replies {
$name:ident$(($($arg:ident$(<$($gen:tt),+>)?),*))? = $num:expr $(=> $msg:expr)?
),* $(,)?
) => {
#[derive(Debug)]
#[allow(clippy::pub_enum_variant_names)]
pub enum Reply<'a> {
@@ -56,6 +73,7 @@ macro_rules! define_replies {
)*
}
impl std::fmt::Display for Reply<'_> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
paste::paste! {
@@ -67,6 +85,7 @@ macro_rules! define_replies {
}
impl Reply<'_> {
#[must_use]
pub fn code(&self) -> &'static str {
paste::paste! {