use crate::MaybeOwned;
pub trait FromIrcMessage<'a>: Sized {
type Error;
fn from_irc(msg: IrcMessage<'a>) -> Result<Self, Self::Error>;
fn into_inner(self) -> MaybeOwned<'a>;
}
pub trait IntoIrcMessage<'a>: Sized + 'a
where
Self: FromIrcMessage<'a>,
{
fn into_irc(self) -> IrcMessage<'a>;
}
impl<'a, T: 'a> IntoIrcMessage<'a> for T
where
T: FromIrcMessage<'a>,
{
fn into_irc(self) -> IrcMessage<'a> {
IrcMessage::parse(self.into_inner()).expect("identity conversion")
}
}
mod message;
pub use message::IrcMessage;
mod prefix;
pub use prefix::{Prefix, PrefixIndex};
pub(crate) mod tags;
pub use tags::{Tags, TagsIter};
mod tag_indices;
pub use tag_indices::TagIndices;
mod error;
pub use error::MessageError;
mod parser;
pub use parser::IrcParserIter;
pub fn parse(input: &str) -> IrcParserIter<'_> {
IrcParserIter::new(input)
}
pub fn parse_one(input: &str) -> Result<(usize, IrcMessage<'_>), MessageError> {
const CRLF: &str = "\r\n";
let pos = input
.find(CRLF)
.ok_or(MessageError::IncompleteMessage { pos: 0 })?
+ CRLF.len();
let next = &input[..pos];
let done = next.len() == input.len();
let msg = IrcMessage::parse(crate::MaybeOwned::Borrowed(next))?;
Ok((if done { 0 } else { pos }, msg))
}