Struct sip2::Message

source ·
pub struct Message { /* private fields */ }
Expand description

SIP message complete with message code, fixed fields, and fields.

Implementations§

source§

impl Message

source

pub fn new( spec: &'static Message, fixed_fields: Vec<FixedField>, fields: Vec<Field>, ) -> Self

source

pub fn from_code(msg_code: &str) -> Result<Message, Error>

source

pub fn from_ff_values( msg_code: &str, fixed_fields: &[&str], ) -> Result<Message, Error>

Creates a new message from a set of fixed field values.

Returns an error if the fixed field values provided are not the correct length for the specified message type.

source

pub fn from_values( msg_code: &str, fixed_fields: &[&str], fields: &[(&str, &str)], ) -> Result<Message, Error>

Create a new message from a list of fixed field and field string values.

source

pub fn add_field(&mut self, code: &str, value: &str)

Adds a Field to a message.

use sip2::{Message, Field};
use sip2::spec;

let mut msg = Message::new(
    &spec::M_LOGIN,
    vec![],
    vec![],
);

msg.add_field("ZZ", "ZZ is a value");
assert_eq!(msg.fields()[0].code(), "ZZ");
source

pub fn maybe_add_field(&mut self, code: &str, value: Option<&str>)

Adds a field to a SIP message if the provided value is not None.

source

pub fn remove_field(&mut self, code: &str, all: bool) -> usize

Remove a field by its code. If ‘all’ is true, remove all occurrences.

source

pub fn get_field_value(&self, code: &str) -> Option<&str>

Return the first value with the specified field code.

source

pub fn spec(&self) -> &'static Message

source

pub fn fields(&self) -> &Vec<Field>

source

pub fn fields_mut(&mut self) -> &mut Vec<Field>

source

pub fn fixed_fields(&self) -> &Vec<FixedField>

source

pub fn fixed_fields_mut(&mut self) -> &mut Vec<FixedField>

source

pub fn to_sip(&self) -> String

Create a SIP string of a message.

use sip2::{Message, Field, FixedField};
use sip2::spec;

let msg = Message::new(
    &spec::M_LOGIN,
    vec![
        FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
        FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ],
    vec![
        Field::new(spec::F_LOGIN_UID.code, "sip_username"),
        Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ]
);

assert_eq!(msg.to_sip(), "9300CNsip_username|COsip_password|");
source

pub fn to_sip_redacted(&self) -> String

Same as to_sip() but replaces the patron password ‘AD’ value with redacted text.

Useful for logging.

source

pub fn from_sip(text: &str) -> Result<Message, Error>

Turns a SIP string into a Message

Assumes the trailing message terminator character has been removed.

Message types and Fixed Field types must be known in advance (see sip2::spec), but Field’s do not necessarily have to match a known spec::Field. Any value of 3 or more characters will be treated as a valid field.

use sip2::{Message, Field, FixedField};
let sip_text = "9300CNsip_username|COsip_password|";
let msg = Message::from_sip(sip_text).unwrap();
assert_eq!(msg.spec().code, "93");
assert_eq!(msg.fields()[0].code(), "CN");
assert_eq!(msg.fields()[1].value(), "sip_password");
source§

impl Message

source

pub fn to_json_value(&self) -> JsonValue

Translate a SIP Message into a JSON object.

use sip2::{Message, Field, FixedField};
use sip2::spec;
use json;

let msg = Message::new(
    &spec::M_LOGIN,
    vec![
        FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
        FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ],
    vec![
        Field::new(spec::F_LOGIN_UID.code, "sip_username"),
        Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ]
);

let json_val = msg.to_json_value();
let expected = json::object!{
  "code":"93",
  "fixed_fields":["0","0"],
  "fields":[{"CN":"sip_username"},{"CO":"sip_password"}]};

assert_eq!(expected, json_val);
source

pub fn to_json(&self) -> String

Translate a SIP Message into a JSON string.

use sip2::{Message, Field, FixedField};
use sip2::spec;

let msg = Message::new(
    &spec::M_LOGIN,
    vec![
        FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
        FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ],
    vec![
        Field::new(spec::F_LOGIN_UID.code, "sip_username"),
        Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ]
);

let json_str = msg.to_json();

// Comparing JSON strings is nontrivial with hashes.
// Assume completion means success.  See to_json_value() for
// more rigorous testing.
assert_eq!(true, true);
source

pub fn from_json_value(json_value: JsonValue) -> Result<Message, SipJsonError>

Translate a JSON object into a SIP Message.

use sip2::{Message, Field, FixedField};
use sip2::spec;
use json;

let expected = Message::new(
    &spec::M_LOGIN,
    vec![
        FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
        FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ],
    vec![
        Field::new(spec::F_LOGIN_UID.code, "sip_username"),
        Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ]
);

let json_val = json::object!{
  "code":"93",
  "fixed_fields":["0","0"],
  "fields":[{"CN":"sip_username"},{"CO":"sip_password"}]};

let msg = Message::from_json_value(json_val).unwrap();

assert_eq!(expected, msg);
source

pub fn from_json(msg_json: &str) -> Result<Message, SipJsonError>

Translate a JSON string into a SIP Message.

use sip2::{Message, Field, FixedField};
use sip2::spec;
use json;

let expected = Message::new(
    &spec::M_LOGIN,
    vec![
        FixedField::new(&spec::FF_UID_ALGO, "0").unwrap(),
        FixedField::new(&spec::FF_PWD_ALGO, "0").unwrap(),
    ],
    vec![
        Field::new(spec::F_LOGIN_UID.code, "sip_username"),
        Field::new(spec::F_LOGIN_PWD.code, "sip_password"),
    ]
);

let json_str = r#"
  {
    "code":"93",
    "fixed_fields":["0","0"],
    "fields":[{"CN":"sip_username"},{"CO":"sip_password"}]
  }
"#;

let msg = Message::from_json(&json_str).unwrap();

assert_eq!(expected, msg);

Trait Implementations§

source§

impl Debug for Message

source§

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

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

impl Display for Message

Message display support for logging / debugging.

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 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> 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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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.