From e59b6b8c935f0fc71d34c5f2dbfff02365dd9203 Mon Sep 17 00:00:00 2001 From: TheDaemoness Date: Thu, 23 Mar 2023 21:38:27 -0700 Subject: [PATCH] Add some basic trait impls for Line and Error --- src/obj.rs | 2 +- src/tokenise.rs | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/obj.rs b/src/obj.rs index fa085db..68e0106 100644 --- a/src/obj.rs +++ b/src/obj.rs @@ -3,7 +3,7 @@ use std::collections::BTreeMap; /// A struct representing all the constituent pieces of an RFC1459/IRCv3 protocol line. /// /// `@tagkey=tagvalue :source COMMAND arg1 arg2 :arg3 with space` -#[derive(Debug)] +#[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct Line { /// [Message tags] of an IRC line. /// [`None`] if no message tags were present. diff --git a/src/tokenise.rs b/src/tokenise.rs index 1d4b326..bae829b 100644 --- a/src/tokenise.rs +++ b/src/tokenise.rs @@ -5,7 +5,7 @@ use super::Line; const TAG_STOP: [&[u8]; 2] = [b"", b"="]; -#[derive(Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Error { /// An empty byte array was passed to the tokeniser. Empty, @@ -19,6 +19,20 @@ pub enum Error { TagValueDecode, } +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Error::Empty => write!(f, "empty slice passed to tokeniser"), + Error::MissingCommand => write!(f, "missing command"), + Error::CommandDecode => write!(f, "commands must be ascii encoded"), + Error::TagKeyDecode => write!(f, "message tag keys must be utf8 encoded"), + Error::TagValueDecode => write!(f, "message tag values must be utf8 encoded"), + } + } +} + +impl std::error::Error for Error {} + fn tag_decode(input: &str) -> String { let mut escaped = false; let mut output = String::with_capacity(input.len());