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§
§source: Option<Source>
§command: String
Implementations§
Source§impl Message
impl Message
Sourcepub fn new(
tags: HashMap<String, String>,
source: Option<Source>,
command: String,
params: Vec<String>,
) -> Result<Self, Error>
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 theIRCv3
message.source
- Optioncontaining optionally an instance of the Source
struct for the source component of theIRCv3
message.command
- String containing the non-optional command for theIRCv3
message.params
- Veccontaining 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}"),
};
Sourcepub fn params(&self) -> Vec<String>
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.
Sourcepub fn build_token(message: Self) -> Result<String, Error>
pub fn build_token(message: Self) -> Result<String, Error>
Builds an IRC message token from a Message
struct.
§Arguments
message
- AMessage
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}")
}
}
Sourcepub fn get_param(&self, index: usize) -> Result<&str, IncomingIrcEventError>
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 typeIncomingIrcEventError
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}")
}
}
Sourcepub fn from(response: String) -> Result<Self, Error>
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§
impl Eq for Message
impl StructuralPartialEq for Message
Auto Trait Implementations§
impl Freeze for Message
impl RefUnwindSafe for Message
impl Send for Message
impl Sync for Message
impl Unpin for Message
impl UnwindSafe for Message
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more