Struct sn_api::resolver::SafeUrl

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

Represents a SafeUrl

A SafeUrl can be in one of two formats: nrs or xor. aka: nrsurl or xorurl

Here is a breakdown of how name terminology is used.

case 1: safe://a.b.shinything:

public_name() –> a.b.shinything top_name() –> shinything sub_names() –> a.b

case 2: safe://shinything:

public_name() –> shinything top_name() –> shinything sub_names() –> None

case 3: safe://a.b.hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc

public_name() –> a.b.hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc top_name() –> hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc sub_names() –> a.b

case 4: safe://hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc public_name() –> hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc top_name() –> hnyynyzhjjjatqkfkjux8maaojtj8r59aphcnue6a11qgecpcebidkywmybnc sub_names() –> None

Implementations§

source§

impl SafeUrl

This implementation performs semi-rigorous validation, when parsing a URL using ::from_url(), ::from_xorurl(), or ::from_nrsurl().

However setters and new() do not enforce all the rules and using them with invalid input can result in serializing invalid URLs. GIGO.

As such, it is recommended to check validity by calling SafeUrl::validate() after instantiating or modifying.

source

pub fn new( address: DataAddress, nrs_name: Option<&str>, type_tag: u64, content_type: ContentType, path: Option<&str>, sub_names: Option<Vec<String>>, query_string: Option<&str>, fragment: Option<&str>, content_version: Option<VersionHash> ) -> Result<Self, Error>

Instantiates a new SafeUrl

Performs some basic validation checks, however it is possible to create invalid urls using this method.

Arguments

  • xor_name - XorName hash
  • nrs_name - complete nrs name, or None for xorurl
  • type_tag - type tag
  • data_type - DataType
  • content_type - ContentType
  • path - must already be percent-encoded if Some. leading ‘/’ optional.
  • xorurl_sub_names - sub_names. ignored if nrs_name is present.
  • query_string - must already be percent-encoded, without ? separator
  • fragment - url fragment, without # separator
  • content_version - overrides value of “?v” in query-string if not None.
source

pub fn is_media_type_supported(media_type: &str) -> bool

A non-member utility function to check if a media-type is currently supported by XOR-URL encoding

source

pub fn from_url(url: &str) -> Result<Self, Error>

Parses a safe url into SafeUrl

Arguments
  • url - either nrsurl or xorurl
source

pub fn from_nrsurl(nrsurl: &str) -> Result<Self, Error>

Parses an NRS SafeUrl into SafeUrl

Arguments
  • nrsurl - an nrsurl.
source

pub fn from_xorurl(xorurl: &str) -> Result<Self, Error>

Parses a XorUrl into SafeUrl

Arguments
  • xorurl - an xorurl.
source

pub fn from_safekey(xor_name: XorName) -> Result<Self, Error>

source

pub fn from_bytes( address: XorName, content_type: ContentType ) -> Result<Self, Error>

source

pub fn from_register( xor_name: XorName, type_tag: u64, content_type: ContentType ) -> Result<Self, Error>

source

pub fn scheme(&self) -> &str

The url scheme. Only ‘safe’ scheme is presently supported.

source

pub fn encoding_version(&self) -> u64

returns encoding version of xorurl

source

pub fn data_type(&self) -> DataType

returns SAFE data type

source

pub fn content_type(&self) -> ContentType

returns SAFE content type

source

pub fn set_content_type( &mut self, content_type: ContentType ) -> Result<(), Error>

sets the SAFE content type

source

pub fn xorname(&self) -> XorName

returns XorName

source

pub fn address(&self) -> DataAddress

returns address

source

pub fn xorurl_public_name(&self) -> String

returns public_name portion of xorurl using the default xorurl encoding.

public_name means sub_names + top_name

useful for retrieving xorurl name associated with an nrsurl.

For a different encoding, see name_to_base()

source

pub fn public_name(&self) -> &str

The public_name in url. Either nrs_name or xor_name.

eg a.b.name –> a.b.name

source

pub fn top_name(&self) -> &str

returns top name of name field.

eg: a.b.name –> name

source

pub fn sub_names(&self) -> &str

returns sub_names

eg: a.b.name –> a.b

source

pub fn sub_names_vec(&self) -> &[String]

returns sub_names in an array slice

eg: a.b.name –> &[“a”, “b”]

source

pub fn set_sub_names(&mut self, sub_names: &str) -> Result<(), Error>

sets sub_names portion of URL

source

pub fn type_tag(&self) -> u64

returns XorUrl type tag

source

pub fn path(&self) -> &str

returns path portion of URL, percent encoded (unmodified).

source

pub fn path_decoded(&self) -> Result<String, Error>

returns path portion of URL, percent decoded

source

pub fn set_path(&mut self, path: &str)

sets path portion of URL

input string must not be percent-encoded. The encoding is done internally.

leading slash is automatically added if necessary.

source

pub fn content_version(&self) -> Option<VersionHash>

gets content version

This is a shortcut method for getting the “?v=” query param.

source

pub fn set_content_version(&mut self, version: Option<VersionHash>)

sets content version

This is a shortcut method for setting the “?v=” query param.

Arguments
  • version - u64 representing value of ?v=<val>
source

pub fn set_query_key( &mut self, key: &str, val: Option<&str> ) -> Result<(), Error>

sets or unsets a key/val pair in query string.

if val is Some, then key=val will be set in query string. If there is more than one instance of key in query string, there will be only one after this call. If val is None, then the key will be removed from query string.

To set key without any value, pass Some<“”> as the val.

val should not be percent-encoded. That is done internally.

Arguments
  • key - name of url query string var
  • val - an option representing the value, or none.
source

pub fn set_query_string(&mut self, query: &str) -> Result<(), Error>

sets query string.

If the query string contains ?v=<version> then it will take effect as if set_content_version() had been called.

Arguments
  • query - percent-encoded key/val pairs.
source

pub fn query_string(&self) -> &str

Retrieves query string

This contains the percent-encoded key/value pairs as seen in a url.

source

pub fn query_string_with_separator(&self) -> String

Retrieves query string, with ? separator if non-empty.

source

pub fn query_pairs(&self) -> Vec<(String, String)>

Retrieves all query pairs, percent-decoded.

source

pub fn query_key(&self, key: &str) -> Vec<String>

Queries a key from the query string.

Can return 0, 1, or many values because a given key may exist 0, 1, or many times in a URL query-string.

source

pub fn query_key_last(&self, key: &str) -> Option<String>

returns the last matching key from a query string.

eg in safe://name?color=red&age=5&color=green&color=blue blue would be returned when key is “color”.

source

pub fn query_key_first(&self, key: &str) -> Option<String>

returns the first matching key from a query string.

eg in safe://name?color=red&age=5&color=green&color=blue red would be returned when key is “color”.

source

pub fn set_fragment(&mut self, fragment: String)

sets url fragment

source

pub fn fragment(&self) -> &str

Retrieves url fragment, without # separator

source

pub fn fragment_with_separator(&self) -> String

Retrieves url fragment, with # separator if non-empty.

source

pub fn is_nrsurl(&self) -> bool

returns true if an NrsUrl, false if an XorUrl

source

pub fn is_xorurl(&self) -> bool

returns true if an XorUrl, false if an NrsUrl

source

pub fn url_type(&self) -> &UrlType

returns type of this url.

for type of the linked content, see ::content_type()

source

pub fn to_xorurl_string(&self) -> String

serializes the URL to an XorUrl string.

This function may be called on an NrsUrl and the corresponding XorUrl will be returned.

source

pub fn to_nrsurl_string(&self) -> Option<String>

serializes the URL to an NrsUrl string.

This function returns None when is_nrsurl() is false.

source

pub fn name_to_base(&self, base: XorUrlBase, include_subnames: bool) -> String

serializes name portion of xorurl using a particular base encoding.

source

pub fn url_percent_decode(s: &str) -> Result<String, Error>

Utility function to perform url percent decoding.

source

pub fn url_percent_encode(s: &str) -> String

Utility function to perform url percent encoding.

source

pub fn validate(&self) -> Result<(), Error>

Validates that a SafeUrl instance can be parsed correctly.

SafeUrl::from_url() performs rigorous validation, however setters and new() do not enforce all the rules

This routine enables a caller to easily validate that the present instance passes all validation checks

source

pub fn encode(&self, base: XorUrlBase) -> String

serializes entire xorurl using a particular base encoding.

Trait Implementations§

source§

impl Clone for SafeUrl

source§

fn clone(&self) -> SafeUrl

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 SafeUrl

source§

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

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

impl<'de> Deserialize<'de> for SafeUrl

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for SafeUrl

source§

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

serializes the URL to a string.

an NrsUrl will be serialized in NrsUrl form. an XorUrl will be serialized in XorUrl form.

See also:

  • ::to_xorurl_string()
  • ::to_nrs_url_string()
source§

impl Hash for SafeUrl

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for SafeUrl

source§

fn cmp(&self, other: &SafeUrl) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<SafeUrl> for SafeUrl

source§

fn eq(&self, other: &SafeUrl) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<SafeUrl> for SafeUrl

source§

fn partial_cmp(&self, other: &SafeUrl) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for SafeUrl

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for SafeUrl

source§

impl StructuralEq for SafeUrl

source§

impl StructuralPartialEq for SafeUrl

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> Twhere Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.

§

impl<T> Pipe for Twhere T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> Rwhere Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> Rwhere Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

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

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
§

impl<A> Actor for Awhere A: Ord + Clone + Hash,

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

§

impl<N> NodeIdT for Nwhere N: Eq + Ord + Clone + Debug + Hash + Send + Sync,

§

impl<T> Proposition for Twhere T: Ord + Clone + Debug + Serialize,