Struct Message

Source
pub struct Message {
    pub tags: HashMap<String, String>,
    pub source: Option<Source>,
    pub command: String,
    /* private fields */
}
Expand description

Represents a valid IRCv3 compliant message.

Fields§

§tags: HashMap<String, String>§source: Option<Source>§command: String

Implementations§

Source§

impl Message

Source

pub fn new( tags: HashMap<String, String>, source: Option<Source>, command: String, params: Vec<String>, ) -> Result<Self, Error>

Creates a new instance of Message.

§Arguments
  • tags - HashMap<String, String> containing the tags for the IRCv3 message.
  • source- Option containing optionally an instance of the Source struct for the source component of the IRCv3 message.
  • command - String containing the non-optional command for the IRCv3 message.
  • params - Vec containing the parameters for the IRCv3 message.
§Returns
  • Result<Self, Error> - A result containing either an instantiated Message struct, or on failure an error of type Error with more details.
§Errors

Will return Error if a parameter is invalid as defined by the check_param function.

§Examples
use cowirc::parser::{Message, Source};
use std::collections::HashMap;

let mut tags: HashMap<String, String> = HashMap::new();
tags.insert(String::from("tag1"), String::from("value1"));
tags.insert(String::from("tag2"), String::from("value2"));
let source = Source { username: String::from("nick"), nickname: String::from("ident"), hostmask: String::from("host") };
let command = String::from("PRIVMSG");
let params = vec![String::from("#channel"), String::from("Hello, world!")];
    let response = Message::new(tags, Some(source), command, params);
    match response {
        Ok(response) => println!("Constructed Message struct: {:?}", response),
        Err(e) => eprintln!("error: could not construct Message struct: {e}"),
    };
Source

pub fn params(&self) -> Vec<String>

Gets the value for the private params field.

§Returns
  • Vec<String> - the current value of the params field in the struct.
Source

pub fn build_token(message: Self) -> Result<String, Error>

Builds an IRC message token from a Message struct.

§Arguments
  • message - A Message struct containing the IRC message.
§Returns
  • Result<String, Error> - A result containing either a valid IRC message, or on failure an error of type Error with more details.
§Errors

Will return Error if params are invalid as defined by IRCv3.

§Examples
use cowirc::parser::{Message, Source};
use std::collections::HashMap;

let mut tags: HashMap<String, String> = HashMap::new();
tags.insert(String::from("tag1"), String::from("value1"));
tags.insert(String::from("tag2"), String::from("value2"));

let source = Some(Source {
    nickname: String::from("nick"),
    username: String::from("ident"),
    hostmask: String::from("host"),
});

let command = String::from("PRIVMSG");
let params = vec![String::from("#channel"), String::from("Hello, world!")];

let message = Message::new(tags, source, command, params);

match Message::build_token(message.unwrap()) {
    Ok(irc_message) => {
        println!("Valid IRC message: {irc_message}")
    }
    Err(e) => {
        eprintln!("error: invalid IRC message struct provided: {e}")
    }
}
Source

pub fn get_param(&self, index: usize) -> Result<&str, IncomingIrcEventError>

Retrieves a parameter at a specified index.

§Arguments
  • index - The index of the parameter to retrieve.
§Returns
  • Result<&str, IncomingIrcEventError> - A result containing either a reference to the parameter if it exists or an error of type IncomingIrcEventError with more details.
§Errors

Will return IncomingIrcEventError if the parameter attempting to be accessed does not exist.

§Examples
use cowirc::Message;
use std::collections::HashMap;
use cowirc::parser::Source;

let mut tags: HashMap<String, String> = HashMap::new();
tags.insert(String::from("tag1"), String::from("value1"));
tags.insert(String::from("tag2"), String::from("value2"));

let source = Some(Source {
    nickname: String::from("nick"),
    username: String::from("ident"),
    hostmask: String::from("host"),
});

let command = String::from("PRIVMSG");
let params = vec![String::from("#channel"), String::from("Hello, world!")];

let response = Message::new(tags, source, command, params);

match response.unwrap().get_param(0) {
    Ok(param) => {
        println!("First parameter: {param}")
    }
    Err(e) => {
        eprintln!("error: could not get first parameter: {e}")
    }
}
Source

pub fn from(response: String) -> Result<Self, Error>

Parse an IRC message and return a struct containing the parsed message data.

§Arguments
  • response - A string containing the IRC message to be parsed.
§Returns
  • Result<Self, Error> - A result containing either the parsed message as a (Message) struct or on failure an error of type Error with more details.
§Errors

Will return Error if the provided input response is deemed invalid by the parser, as defined by IRCv3.

§Examples
use cowirc::parser::{Message, Source};
use std::collections::HashMap;

let response = "@tag1=value1;tag2=value2 :nick!ident@host PRIVMSG #channel :Hello, world!";
match Message::from(response.to_string()) {
    Ok(parsed_message) => {
        let mut tags: HashMap<String, String> = HashMap::new();
        tags.insert(String::from("tag1"), String::from("value1"));
        tags.insert(String::from("tag2"), String::from("value2"));
        let source = Some(Source {
            nickname: String::from("nick"),
            username: String::from("ident"),
            hostmask: String::from("host"),
        });
        let command = String::from("PRIVMSG");
        let params = vec![String::from("#channel"), String::from("Hello, world!")];

        let expected_message = Message::new(
            tags,
            source,
            command,
            params,
        );

        assert_eq!(parsed_message, expected_message.unwrap());
    }
    Err(e) => {
        eprintln!("error: response \"{response}\" is invalid: {e}")
    }
}

Trait Implementations§

Source§

impl Clone for Message

Source§

fn clone(&self) -> Message

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Message

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Message

Source§

fn eq(&self, other: &Message) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Message

Source§

impl StructuralPartialEq for Message

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.