Enum SipJsonError

Source
pub enum SipJsonError {
    SerializeError(Error),
    DeserializeError(Error),
    InvalidPath(String),
    InvalidQuery(String),
    TypeConversionError(String),
    Other(String),
}
Expand description

§JSON Representation and Access Layer for SIP Types

This module provides a comprehensive JSON-based interface for working with SIP types, enabling powerful, flexible access to SIP message data.

§Overview

The SIP JSON module offers several ways to interact with SIP messages:

  1. Path-based access - Direct access via dot notation (e.g., headers.From.display_name)
  2. Query-based access - Complex searches using JSONPath-like syntax (e.g., $..display_name)
  3. Object conversion - Convert between SIP types and JSON structures
  4. Convenience traits - Helper methods for common SIP header access patterns

§Core Components

  • SipValue - A JSON-like value representing any SIP data
  • SipJsonExt - Extension trait providing JSON operations on SIP types
  • SipMessageJson - Convenience trait for common SIP headers
  • path module - Functions for path-based access
  • query module - Functions for query-based access

§Path Access vs. Query Access

  • Path access is direct and specific - use when you know exactly what you’re looking for
  • Query access is flexible and powerful - use when searching for patterns or exploring

§Basic Usage Examples

§Path-based Access

use rvoip_sip_core::prelude::*;
use rvoip_sip_core::json::SipJsonExt;

let request = RequestBuilder::invite("sip:bob@example.com").unwrap()
    .from("Alice", "sip:alice@example.com", Some("tag12345"))
    .to("Bob", "sip:bob@example.com", None)
    .build();

// Simple path access with Option return
if let Some(from_display) = request.path("headers.From.display_name") {
    println!("From display name: {}", from_display);
}

// Direct string access with default value
let to_display = request.path_str_or("headers.To.display_name", "Unknown");
let from_tag = request.path_str_or("headers.From.params[0].Tag", "No tag");

§Query-based Access

use rvoip_sip_core::prelude::*;
use rvoip_sip_core::json::SipJsonExt;

let request = RequestBuilder::invite("sip:bob@example.com").unwrap()
    .from("Alice", "sip:alice@example.com", Some("tag12345"))
    .to("Bob", "sip:bob@example.com", Some("tag6789"))
    .via("pc33.atlanta.com", "UDP", Some("z9hG4bK776asdhds"))
    .build();

// Find all display names in the message
let display_names = request.query("$..display_name");
for name in &display_names {
    println!("Found display name: {}", name);
}

// Find all tags anywhere in the message
let tags = request.query("$..Tag");
println!("Found {} tags", tags.len());

// Complex queries are also possible
let branches = request.query("$.headers.Via[*].params[?(@.Branch)]");
for branch in &branches {
    println!("Via branch: {}", branch);
}

§Using SIP Message Helpers

use rvoip_sip_core::prelude::*;
use rvoip_sip_core::json::ext::SipMessageJson;

let request = RequestBuilder::invite("sip:bob@example.com").unwrap()
    .from("Alice", "sip:alice@example.com", Some("tag12345"))
    .to("Bob", "sip:bob@example.com", None)
    .build();

// Use helper methods for common headers
let from_uri = request.from_uri()?;
let from_tag = request.from_tag()?;
let call_id = request.call_id()?;

println!("Call-ID: {}", call_id);
println!("From URI: {}", from_uri);
println!("From tag: {}", from_tag);

// Methods return None when data isn't present
if let Some(to_tag) = request.to_tag() {
    println!("Dialog is established (to_tag present)");
} else {
    println!("Dialog not yet established (no to_tag)");
}

§Converting to/from JSON

use rvoip_sip_core::json::{SipJsonExt, SipValue, SipJsonError};
use rvoip_sip_core::prelude::*;
use rvoip_sip_core::types::sip_request::Request;
use std::error::Error;

let request = RequestBuilder::invite("sip:bob@example.com").unwrap()
    .from("Alice", "sip:alice@example.com", Some("tag12345"))
    .build();

// Convert to JSON string with proper error handling
let json_str = match request.to_json_string() {
    Ok(s) => s,
    Err(e) => return Err(Box::new(e)),
};
println!("JSON representation: {}", json_str);

// Convert to pretty-printed JSON string
let pretty_json = match request.to_json_string_pretty() {
    Ok(s) => s,
    Err(e) => return Err(Box::new(e)),
};
println!("Pretty JSON:\n{}", pretty_json);

// Create a new request from the JSON string
let new_request = match Request::from_json_str(&json_str) {
    Ok(r) => r,
    Err(e) => return Err(Box::new(e)),
};

// Convert to a SipValue for direct manipulation
let value = match request.to_sip_value() {
    Ok(v) => v,
    Err(e) => return Err(Box::new(e)),
};

// Access fields on the SipValue directly
let method = value.get_path("method").and_then(|v| v.as_str());
assert_eq!(method, Some("Invite"));

§When to Use Each Approach

  • Typed Headers API: When type safety is critical (production code)
  • Path Accessors: For direct, simple access to known fields
  • Query Interface: For complex searches or exploring message structure
  • SipMessageJson methods: For common SIP headers with a concise API
  • Direct SipValue manipulation: For advanced JSON operations

Error type for JSON operations.

This enum represents the various errors that can occur during JSON operations on SIP messages.

§Examples

use rvoip_sip_core::json::{SipJsonExt, SipJsonError};
use std::error::Error;

// Function that demonstrates SipJsonError handling
fn show_error_handling() {
    // Using Result with SipJsonError
    let result = "not_valid_json".parse::<serde_json::Value>()
        .map_err(|e| SipJsonError::DeserializeError(e));

    match result {
        Ok(_) => println!("Successfully parsed"),
        Err(SipJsonError::DeserializeError(e)) => println!("Deserialization error: {}", e),
        Err(e) => println!("Other error: {}", e),
    }
}

Variants§

§

SerializeError(Error)

Error during serialization

§

DeserializeError(Error)

Error during deserialization

§

InvalidPath(String)

Invalid path provided

§

InvalidQuery(String)

Invalid query provided

§

TypeConversionError(String)

Type conversion error

§

Other(String)

Other errors

Trait Implementations§

Source§

impl Debug for SipJsonError

Source§

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

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

impl Display for SipJsonError

Source§

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

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

impl Error for SipJsonError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

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§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V