Struct RelLinkCollection

Source
pub struct RelLinkCollection(/* private fields */);
Expand description

§RelLinkCollection

§Adding new data to the collection

use hateoas::{HttpMethod, RelLink, RelLinkCollection};

let rel_vec = vec![
    RelLink::new( "foo","foo", HttpMethod::Get),
    RelLink::new( "bar","bar", HttpMethod::Get)
];
let rlc_check = RelLinkCollection::new(rel_vec);

let mut rlc = RelLinkCollection::default();
rlc.add(RelLink::new( "foo","foo", HttpMethod::Get));
rlc.add(RelLink::new( "bar","bar", HttpMethod::Get));

§Adding new data but data is overwritten

use hateoas::{HttpMethod, RelLink, RelLinkCollection};

let rel_vec = vec![
    RelLink::new( "foo","foo", HttpMethod::Get),
    RelLink::new( "bar","bar", HttpMethod::Get)
];
let mut rlc = RelLinkCollection::new(rel_vec);

let old_rel = rlc.add(RelLink::new( "foo","foo-bar", HttpMethod::Get));

assert_eq!(old_rel, Some(("foo", "foo", HttpMethod::Get).into()));
use hateoas::{HttpMethod, RelLink, RelLinkCollection};

let rel_vec = vec![
    RelLink::new( "foo","foo", HttpMethod::Get),
    RelLink::new( "bar","bar", HttpMethod::Get)
];
let mut rlc = RelLinkCollection::new(rel_vec);

let rel = rlc.get("foo");

assert_eq!(rel, Some(&("foo", "foo", HttpMethod::Get).into()));
use hateoas::{HttpMethod, RelLink, RelLinkCollection};

let rel_vec = vec![
    RelLink::new( "foo","foo", HttpMethod::Get),
    RelLink::new( "bar","bar", HttpMethod::Get)
];
let mut rlc = RelLinkCollection::new(rel_vec);

let mut rel = rlc.get_mut("foo");

assert_eq!(rel, Some(&mut ("foo", "foo", HttpMethod::Get).into()));

rel.map(|t| *t = ("foo-bar", "foo-bar", HttpMethod::Connect).into());

let updated_rel = rlc.get("foo-bar");

assert_eq!(updated_rel, Some(&("foo-bar", "foo-bar", HttpMethod::Connect).into()));

Implementations§

Source§

impl RelLinkCollection

Source

pub fn new(v_rel: Vec<RelLink>) -> Self

§Create Collection

Create new Collection with complete Vec, this allows to set all the elements for the collection in one go using a Vec.

use hateoas::{RelLink, RelLinkCollection};

let collection = RelLinkCollection::new(vec![]);
let vec_col: Vec<RelLink> = collection.into();

assert_eq!(vec_col, vec![]);
Source

pub fn get(&self, rel: &str) -> Option<&RelLink>

Getting a RelLink from the collection by rel id.

use hateoas::{RelLinkCollection, HttpMethod, RelLink};

let collection = RelLinkCollection::new(vec![("rel_id", "/rel_path/", HttpMethod::Get).into()]);

assert_eq!(collection.get("rel_id"), Some(&RelLink::new("rel_id", "/rel_path/", HttpMethod::Get)));
Source

pub fn get_mut(&mut self, rel: &str) -> Option<&mut RelLink>

Getting a RelLink from the collection by rel id.

use hateoas::{RelLinkCollection, HttpMethod, RelLink};

let mut collection = RelLinkCollection::new(vec![("rel_id", "/rel_path/", HttpMethod::Get).into()]);
let mut rel_from_collection = collection.get_mut("rel_id");
rel_from_collection.map(|t| *t = RelLink::new("rel_id_2", "/rel_path_2/", HttpMethod::Connect));

assert_eq!(collection.get("rel_id_2"), Some(&RelLink::new("rel_id_2", "/rel_path_2/", HttpMethod::Connect)));
Source

pub fn has(&self, rel: &str) -> bool

Checking if a rel_id already exists in the collection.

use hateoas::{RelLinkCollection, HttpMethod, RelLink};

let mut collection = RelLinkCollection::new(vec![("rel_id", "/rel_path/", HttpMethod::Get).into()]);

assert_eq!(collection.has("rel_id"), true);
assert_eq!(collection.has("rel_id_2"), false);
Source

pub fn add<I: Into<RelLink>>(&mut self, rel: I) -> Option<RelLink>

Adding a link to the collection of RelLinks, if the link already exists it will insert the new data and return the old.

use hateoas::{RelLinkCollection, HttpMethod, RelLink};

let mut collection = RelLinkCollection::new(vec![("rel_id", "/rel_path/", HttpMethod::Get).into()]);
let old_data = collection.add(("rel_id", "/rel_path_2/", HttpMethod::Connect));

assert_eq!(old_data, Some(RelLink::new("rel_id", "/rel_path/", HttpMethod::Get)));
assert_eq!(collection.get("rel_id"), Some(&RelLink::new("rel_id", "/rel_path_2/", HttpMethod::Connect)));

Trait Implementations§

Source§

impl Debug for RelLinkCollection

Source§

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

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

impl Default for RelLinkCollection

Source§

fn default() -> RelLinkCollection

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for RelLinkCollection

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<I: Into<RelLink>> From<I> for RelLinkCollection

Source§

fn from(r: I) -> Self

Converts to this type from the input type.
Source§

impl From<RelLinkCollection> for Vec<RelLink>

Source§

fn from(col: RelLinkCollection) -> Self

Converts to this type from the input type.
Source§

impl<I: Into<RelLink>> From<Vec<I>> for RelLinkCollection

Source§

fn from(v_rel: Vec<I>) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for RelLinkCollection

Source§

fn eq(&self, other: &RelLinkCollection) -> 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 Serialize for RelLinkCollection

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 StructuralPartialEq for RelLinkCollection

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, 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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,