pub struct Message { /* private fields */ }
Expand description
SIP message complete with message code, fixed fields, and fields.
Implementations§
source§impl Message
impl Message
pub fn new( spec: &'static Message, fixed_fields: Vec<FixedField>, fields: Vec<Field>, ) -> Self
pub fn from_code(msg_code: &str) -> Result<Message, Error>
sourcepub fn from_ff_values(
msg_code: &str,
fixed_fields: &[&str],
) -> Result<Message, Error>
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.
sourcepub fn from_values(
msg_code: &str,
fixed_fields: &[&str],
fields: &[(&str, &str)],
) -> Result<Message, Error>
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.
sourcepub fn add_field(&mut self, code: &str, value: &str)
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");
sourcepub fn maybe_add_field(&mut self, code: &str, value: Option<&str>)
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.
sourcepub fn remove_field(&mut self, code: &str, all: bool) -> usize
pub fn remove_field(&mut self, code: &str, all: bool) -> usize
Remove a field by its code. If ‘all’ is true, remove all occurrences.
sourcepub fn get_field_value(&self, code: &str) -> Option<&str>
pub fn get_field_value(&self, code: &str) -> Option<&str>
Return the first value with the specified field code.
pub fn spec(&self) -> &'static Message
pub fn fields(&self) -> &Vec<Field>
pub fn fields_mut(&mut self) -> &mut Vec<Field>
pub fn fixed_fields(&self) -> &Vec<FixedField>
pub fn fixed_fields_mut(&mut self) -> &mut Vec<FixedField>
sourcepub fn to_sip(&self) -> String
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|");
sourcepub fn to_sip_redacted(&self) -> String
pub fn to_sip_redacted(&self) -> String
Same as to_sip() but replaces the patron password ‘AD’ value with redacted text.
Useful for logging.
sourcepub fn from_sip(text: &str) -> Result<Message, Error>
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
impl Message
sourcepub fn to_json_value(&self) -> JsonValue
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);
sourcepub fn to_json(&self) -> String
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);
sourcepub fn from_json_value(json_value: JsonValue) -> Result<Message, SipJsonError>
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);
sourcepub fn from_json(msg_json: &str) -> Result<Message, SipJsonError>
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);