Enum rmoods_backend::reddit::model::listing::KindContainer
source · pub enum KindContainer {
Listing(Box<RedditListing>),
More(Box<MoreData>),
T1(Box<Comment>),
T2(Box<UserInfo>),
T3(Box<Post>),
T4(Box<Value>),
T5(Box<SubredditInfo>),
T6(Box<Value>),
}
Expand description
Represents a generic Reddit data container.
Reddit API uses a special structure to represent varying types of object in a uniform way. This struct is actually the heart of the Reddit response parsing.
§How does it work?
It’s kind of a declarative way to parse the response. The KindContainer struct described the universal structure of every Reddit response. Reddit uses the following structure to let us know what type of data to expect:
{
"kind" : "t1",
"data" : {
<Comment data>
}
}
So, we have a kind
field and the corresponding data
field, which has an object of the type indicated by kind
.
Serde provides a convenient macro, that lets us describe how our enum should be handled in serialization/deserialization:
#[serde(tag = "kind", content = "data")]
enum Foo {}
When used like in the example above, this macro lets us represent our exact structure: object tagged by kind
with the content in data
.
Content is the value embedded in the enum variant, eg. Box<RedditListing>
.
Each field uses the #[serde(rename = "foo")]
macro, to tell Serde to look for that given name during its operations.
This paired with the previously described tagging, lets us use the KindContainer struct to recrusively, automatically and reliably parse all Reddit responses.
§Example
Probably not accurate execution order, written just to understand the process.
- We tell serde to deserialize some JSON data into KindContainer.
- Serde looks for the
kind
field. - Let’s assume
kind: "t1"
. - Since
kind
is"t1"
, serde knows that the enum variant it encountered is KindContainer::T1(Box) - Serde finds the
data
field and treats it as the content of type Box - Box is transparent to serde, so it parses the
data
field as a Comment
§Recursive case
For the KindContainer::Listing, this works recursively, since RedditListing is basically a list of KindContainers.
Variants§
Listing(Box<RedditListing>)
More(Box<MoreData>)
T1(Box<Comment>)
T2(Box<UserInfo>)
T3(Box<Post>)
T4(Box<Value>)
T5(Box<SubredditInfo>)
T6(Box<Value>)
Trait Implementations§
source§impl Clone for KindContainer
impl Clone for KindContainer
source§fn clone(&self) -> KindContainer
fn clone(&self) -> KindContainer
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for KindContainer
impl Debug for KindContainer
source§impl<'de> Deserialize<'de> for KindContainer
impl<'de> Deserialize<'de> for KindContainer
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for KindContainer
impl RefUnwindSafe for KindContainer
impl Send for KindContainer
impl Sync for KindContainer
impl Unpin for KindContainer
impl UnwindSafe for KindContainer
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more