Add example usage and fix some documentation nits

This commit is contained in:
TheDaemoness 2023-03-23 14:20:51 -07:00
parent 1077e66242
commit 541b639f1a
No known key found for this signature in database
GPG Key ID: 6023AE3B887F8E29
3 changed files with 24 additions and 7 deletions

View File

@ -19,7 +19,9 @@ fn tag_encode(input: &str) -> String {
impl Line { impl Line {
#[allow(clippy::doc_markdown)] #[allow(clippy::doc_markdown)]
/// Format `self` in to a byte string by [RFC1459] and [IRCv3] protocol rules. /// Format `self` into a byte string by [RFC1459] and [IRCv3] protocol rules.
///
/// The returned byte string is NOT suffixed with a CRLF.
/// ///
/// [RFC1459]: https://www.rfc-editor.org/rfc/rfc1459#section-2.3 /// [RFC1459]: https://www.rfc-editor.org/rfc/rfc1459#section-2.3
/// [IRCv3]: https://ircv3.net/specs/extensions/message-tags.html /// [IRCv3]: https://ircv3.net/specs/extensions/message-tags.html

View File

@ -1,3 +1,16 @@
//! ## usage
//!
//! ### tokenisation
//!
//! ```
//! let bytes = b"@id=123 :jess!~jess@hostname PRIVMSG #chat :hello there!";
//! let line = irctokens::Line::tokenise(bytes).unwrap();
//! println!("{:?}", line.tags);
//! println!("{:?}", line.source);
//! println!("{}", line.command);
//! println!("{:?}", line.arguments);
//! ```
mod format; mod format;
mod obj; mod obj;
pub mod tokenise; pub mod tokenise;

View File

@ -7,15 +7,15 @@ const TAG_STOP: [&[u8]; 2] = [b"", b"="];
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
/// An empty byte array was passed to the tokeniser /// An empty byte array was passed to the tokeniser.
Empty, Empty,
/// A line is invalid if it has no `COMMAND` (e.g. `PRIVMSG`) /// A line is invalid if it has no `COMMAND` (e.g. `PRIVMSG`).
MissingCommand, MissingCommand,
/// Commands must be ascii encoded /// Commands must be ascii encoded.
CommandDecode, CommandDecode,
/// Message tag keys must be utf8 encoded /// Message tag keys must be utf8 encoded.
TagKeyDecode, TagKeyDecode,
/// Message tag values must be utf8 encoded /// Message tag values must be utf8 encoded.
TagValueDecode, TagValueDecode,
} }
@ -50,6 +50,8 @@ impl Line {
#[allow(clippy::doc_markdown)] #[allow(clippy::doc_markdown)]
/// Attempt to tokenise a byte string by [RFC1459] and [IRCv3] protocol rules. /// Attempt to tokenise a byte string by [RFC1459] and [IRCv3] protocol rules.
/// ///
/// Expects a byte string that does NOT contain a trailing CRLF.
///
/// [RFC1459]: https://www.rfc-editor.org/rfc/rfc1459#section-2.3 /// [RFC1459]: https://www.rfc-editor.org/rfc/rfc1459#section-2.3
/// [IRCv3]: https://ircv3.net/specs/extensions/message-tags.html /// [IRCv3]: https://ircv3.net/specs/extensions/message-tags.html
pub fn tokenise(mut line: &[u8]) -> Result<Self, Error> { pub fn tokenise(mut line: &[u8]) -> Result<Self, Error> {
@ -102,10 +104,10 @@ impl Line {
} }
} }
/// Implementation that simply calls [`Line::tokenise()`].
impl TryFrom<&[u8]> for Line { impl TryFrom<&[u8]> for Line {
type Error = Error; type Error = Error;
/// Utility function for [`Line::tokenise()`]
fn try_from(value: &[u8]) -> Result<Self, Self::Error> { fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
Self::tokenise(value) Self::tokenise(value)
} }