Struct CoapUri

Source
pub struct CoapUri { /* private fields */ }
Expand description

Representation of a URI for CoAP requests, responses or proxy URIs.

See https://datatracker.ietf.org/doc/html/rfc7252#section-6 for a description of how a URI should look like.

§Examples

The easiest way to instantiate a request or location CoAP URI is by parsing a string (either using the FromStr implementation or using CoapUri::try_from_str):

use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = "coap://example.com:4711/foo/bar?answer=42".parse()?;

assert_eq!(uri.scheme(), Some(CoapUriScheme::Coap));
assert_eq!(uri.host(), Some("example.com".as_bytes()));
assert_eq!(uri.port(), Some(4711));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(!uri.is_proxy());

Alternatively, a CoapUri may be constructed from its parts using CoapUri::new or CoapUri::new_relative or from a Url (requires the url feature), refer to the method level documentation for more information.

If you want to create a proxy URI, refer to the method-level documentation CoapUri::new_proxy, CoapUri::try_from_str_proxy or CoapUri::try_from_url_proxy.

§Note on URI Length Limits

Due to the specified limits of CoAP option lengths, the URI path components, query components, and hostnames for a URI must not exceed 255 bytes each, i.e. a full path with more than 255 bytes is fine, but each individual path segment must be smaller than 255 bytes.

For proxy URIs, there is a length limit of 255 bytes for the scheme. As we use the Uri-* options for encoding proxy URIs instead of the Proxy-Uri option (as specified in RFC 7252, section 5.10.2), the above limits regarding path and query components also apply here.

Implementations§

Source§

impl CoapUri

Source

pub fn new( scheme: CoapUriScheme, host: &[u8], port: u16, path: Option<&[u8]>, query: Option<&[u8]>, ) -> Result<CoapUri, UriParsingError>

Creates a new CoapUri for use as a request or location URI from its constituent parts.

§Errors

May fail if the provided fields do not represent a valid relative URI or if the arguments exceed maximum lengths (see the struct level documentation).

§Examples
use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = CoapUri::new(
    CoapUriScheme::Coap,
    "example.com".as_bytes(),
    4711,
    Some("/foo/bar".as_bytes()),
    Some("?answer=42".as_bytes())
)?;

assert_eq!(uri.scheme(), Some(CoapUriScheme::Coap));
assert_eq!(uri.host(), Some("example.com".as_bytes()));
assert_eq!(uri.port(), Some(4711));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(!uri.is_proxy());
Source

pub fn new_proxy( scheme: CoapUriScheme, host: &[u8], port: u16, path: Option<&[u8]>, query: Option<&[u8]>, ) -> Result<CoapUri, UriParsingError>

Creates a new CoapUri for use as a proxy URI from its constituent parts.

§Errors

May fail if the provided fields do not represent a valid relative URI or if the arguments exceed maximum lengths (see the struct level documentation).

§Examples
use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = CoapUri::new_proxy(
    CoapUriScheme::Coap,
    "example.com".as_bytes(),
    4711,
    Some("/foo/bar".as_bytes()),
    Some("?answer=42".as_bytes())
)?;

assert_eq!(uri.scheme(), Some(CoapUriScheme::Coap));
assert_eq!(uri.host(), Some("example.com".as_bytes()));
assert_eq!(uri.port(), Some(4711));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(uri.is_proxy());
Source

pub fn new_relative( path: Option<&[u8]>, query: Option<&[u8]>, ) -> Result<CoapUri, UriParsingError>

Attempts to convert the provided path and query into a relative CoapUri suitable as a request/location URI.

§Errors

May fail if the provided path and query do not represent a valid relative URI or if the arguments exceed maximum lengths (see the struct level documentation).

§Examples
use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = CoapUri::new_relative(
    Some("/foo/bar".as_bytes()),
    Some("?answer=42".as_bytes())
)?;

assert_eq!(uri.scheme(), None);
assert_eq!(uri.host(), None);
assert_eq!(uri.port(), Some(5683));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(!uri.is_proxy());
Source

pub fn try_from_str(uri_str: &str) -> Result<CoapUri, UriParsingError>

Attempts to convert the provided uri_str into a CoapUri suitable as a request/location URI.

§Errors

May fail if the provided uri_str is not a valid URI or if the URI components exceed maximum lengths (see the struct level documentation).

§Examples
use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = CoapUri::try_from_str("coap://example.com:4711/foo/bar?answer=42")?;

assert_eq!(uri.scheme(), Some(CoapUriScheme::Coap));
assert_eq!(uri.host(), Some("example.com".as_bytes()));
assert_eq!(uri.port(), Some(4711));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(!uri.is_proxy());
Source

pub fn try_from_str_proxy(uri_str: &str) -> Result<CoapUri, UriParsingError>

Attempts to convert the provided uri_str into a CoapUri suitable as a proxy URI.

§Errors

May fail if the provided uri_str is not a valid proxy URI or if the URI components exceed maximum lengths (see the struct level documentation).

§Examples
use libcoap_rs::error::UriParsingError;
use libcoap_rs::types::{CoapUri, CoapUriScheme};

let uri: CoapUri = CoapUri::try_from_str_proxy("coap://example.com:4711/foo/bar?answer=42")?;

assert_eq!(uri.scheme(), Some(CoapUriScheme::Coap));
assert_eq!(uri.host(), Some("example.com".as_bytes()));
assert_eq!(uri.port(), Some(4711));
assert_eq!(uri.path(), Some("foo/bar".as_bytes()));
assert_eq!(uri.query(), Some("answer=42".as_bytes()));
assert!(uri.is_proxy());
Source

pub fn try_from_url(url: &Url) -> Result<CoapUri, UriParsingError>

Attempts to convert a Url into a CoapUri.

§Errors

May fail if the provided Url is not a valid URI supported by libcoap or if the URI components exceed maximum lengths (see the struct level documentation).

Source

pub fn try_from_url_proxy(url: &Url) -> Result<CoapUri, UriParsingError>

Attempts to convert a Url into a proxy CoapUri.

§Errors

May fail if the provided Url is not a valid proxy URI supported by libcoap or if the URI components exceed maximum lengths (see the struct level documentation).

Source

pub fn scheme(&self) -> Option<CoapUriScheme>

Returns the scheme part of this URI.

Source

pub fn host(&self) -> Option<&[u8]>

Returns the host part of this URI.

Source

pub fn port(&self) -> Option<UriPort>

Returns the port of this URI (if provided).

Source

pub fn path(&self) -> Option<&[u8]>

Returns the URI path part of this URI.

Source

pub fn query(&self) -> Option<&[u8]>

Returns the host part of this URI.

Source

pub fn is_proxy(&self) -> bool

Returns whether this URI is a proxy URI.

Source

pub fn into_options(self) -> Vec<CoapOption>

Converts the given URI into a Vec of CoapOptions that can be added to a crate::message::CoapMessage.

Source

pub fn as_raw_uri(&self) -> &coap_uri_t

Provides a reference to the raw [coap_uri_t] struct represented by this CoapUri.

Note that while obtaining this struct and reading the fields is safe (which is why this method is safe), modifying the referenced URI parts by (unsafely) dereferencing and mutating the const pointers inside is not.

Source

pub unsafe fn from_raw_uri( raw_uri: *const coap_uri_t, is_proxy: bool, ) -> CoapUri

Converts the given raw_uri to a new CoapUri instance.

This method will create a copy of the provided URI, i.e. raw_uri will remain valid and not be owned by the created CoapUri instance.

§Safety

The provided raw_uri must point to a valid instance of [coap_uri_t]. In particular, the provided pointers for the URI components must also be valid.

§Panics

Panics if the provided raw_uri is null or the provided URI contains a null byte.

Trait Implementations§

Source§

impl Clone for CoapUri

Source§

fn clone(&self) -> Self

Returns a copy 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 CoapUri

Source§

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

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

impl Display for CoapUri

Source§

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

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

impl FromStr for CoapUri

Source§

type Err = UriParsingError

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 CoapUri

Source§

fn eq(&self, other: &Self) -> 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<&Url> for CoapUri

Source§

type Error = UriParsingError

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

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

Performs the conversion.
Source§

impl Eq for CoapUri

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> 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.
§

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

§

fn vzip(self) -> V

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T