pub struct Builder { /* private fields */ }
Expand description
A Message Builder for a simpler generation of a message instead of building a string first.
§Examples
To build a simple message from scratch:
use irc_rust::Message;
use std::error::Error;
let message = Message::builder("CMD")
.tag("key1", "value1")
.tag("key2", "value2")
.prefix("name", Some("user"), Some("host"))
.param("param1").param("param2")
.trailing("trailing")
.build();
let mut tags = message.tags()?;
let (key, value) = tags.next().unwrap()?;
println!("{}={}", key, value); // Prints 'key1=value1'
To alter an existing message:
use irc_rust::Message;
use std::error::Error;
use irc_rust::builder::Builder;
let message = Message::from("@key=value :name!user@host CMD param1 :trailing!").to_builder()?
.tag("key", "value2")
.param("param2")
.param("param4")
.set_param(1, "param3")
.build();
// Or
let message: Message = "@key=value :name!user@host CMD param1 :trailing!".parse::<Builder>()?
.tag("key", "value2")
.param("param2")
.param("param4")
.set_param(1, "param3")
.build();
assert_eq!(message.to_string(), "@key=value2 :name!user@host CMD param1 param3 param4 :trailing!");
Ok(())
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn new<S: ToString>(command: S) -> Self
pub fn new<S: ToString>(command: S) -> Self
Creates a new empty builder.
§Usage
use irc_rust::builder::Builder;
let message = Builder::new("CMD").build();
assert_eq!("CMD", message.command()?);
Sourcepub fn prefix<SN, SU, SH>(
self,
name: SN,
user: Option<SU>,
host: Option<SH>,
) -> Builder
pub fn prefix<SN, SU, SH>( self, name: SN, user: Option<SU>, host: Option<SH>, ) -> Builder
Set a prefix name.
§Panics
Panics if name is empty, user or host == Some(“”) or user is some and host is none.
Sourcepub fn set_param<S: ToString>(self, index: usize, param: S) -> Builder
pub fn set_param<S: ToString>(self, index: usize, param: S) -> Builder
Set a param at the given index. If the index is below 0, it won’t be set. If index >= length of the existing parameters it will be added to the end but not set as trailing. This doesn’t allow to set the trailing parameter.
§Panics
Panics if param is empty.
pub fn remove_param(self, index: usize) -> Builder
pub fn trailing<S: ToString>(self, trailing: S) -> Builder
Trait Implementations§
impl Eq for Builder
impl StructuralPartialEq for Builder
Auto Trait Implementations§
impl Freeze for Builder
impl RefUnwindSafe for Builder
impl Send for Builder
impl Sync for Builder
impl Unpin for Builder
impl UnwindSafe for Builder
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