Struct Builder

Source
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

Source

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()?);
Source

pub fn command<S: ToString>(self, cmd: S) -> Builder

Set the command.

§Panics

Panics if cmd is empty.

Source

pub fn tag<SK: ToString, SV: ToString>(self, key: SK, value: SV) -> Builder

Set a tag.

§Panics

Panics if key is empty. value is allowed to be empty.

Source

pub fn prefix<SN, SU, SH>( self, name: SN, user: Option<SU>, host: Option<SH>, ) -> Builder
where SN: ToString, SU: ToString, SH: ToString,

Set a prefix name.

§Panics

Panics if name is empty, user or host == Some(“”) or user is some and host is none.

Source

pub fn param<S: ToString>(self, param: S) -> Builder

Add a param.

§Panics

Panics if param is empty.

Source

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.

Source

pub fn remove_param(self, index: usize) -> Builder

Source

pub fn trailing<S: ToString>(self, trailing: S) -> Builder

Source

pub fn build(self) -> Message

Create a Message instance and return if valid.

Trait Implementations§

Source§

impl Clone for Builder

Source§

fn clone(&self) -> Builder

Returns a duplicate 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 Builder

Source§

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

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

impl Default for Builder

Source§

fn default() -> Builder

Returns the “default value” for a type. Read more
Source§

impl FromStr for Builder

Source§

type Err = ParserError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Builder

Source§

fn eq(&self, other: &Builder) -> 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 TryFrom<Message> for Builder

Source§

type Error = ParserError

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

fn try_from(value: Message) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Builder

Source§

impl StructuralPartialEq for Builder

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.