summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Woolcock <paul@woolcock.us>2019-04-29 06:27:20 -0400
committerPaul Woolcock <paul@woolcock.us>2019-04-29 10:54:29 -0400
commit55d0f8fa2ab5f50ab237633f617270c38526df84 (patch)
tree88f469de8e50efb32e9bac4e92b8920f32dc39fc
parente322a14f29455368ef827fa4c211dbfecc8a2c43 (diff)
Add an `OwnedPage` that doesn't borrow the underlying client
-rw-r--r--src/page.rs119
1 files changed, 109 insertions, 10 deletions
diff --git a/src/page.rs b/src/page.rs
index 7733311..3a47ce6 100644
--- a/src/page.rs
+++ b/src/page.rs
@@ -7,16 +7,6 @@ use url::Url;
use http_send::HttpSend;
-/// Represents a single page of API results
-#[derive(Debug, Clone)]
-pub struct Page<'a, T: for<'de> Deserialize<'de>, H: 'a + HttpSend> {
- mastodon: &'a Mastodon<H>,
- next: Option<Url>,
- prev: Option<Url>,
- /// Initial set of items
- pub initial_items: Vec<T>,
-}
-
macro_rules! pages {
($($direction:ident: $fun:ident),*) => {
@@ -43,6 +33,77 @@ macro_rules! pages {
}
}
+/// Owned version of the `Page` struct in this module. Allows this to be more
+/// easily stored for later use
+///
+/// # Example
+///
+/// ```no_run
+/// # extern crate elefren;
+/// # use elefren::Mastodon;
+/// # use elefren::page::OwnedPage;
+/// # use elefren::http_send::HttpSender;
+/// # use elefren::entities::status::Status;
+/// # use std::cell::RefCell;
+/// # use elefren::prelude::*;
+/// # fn main() -> Result<(), elefren::Error> {
+/// # let data = Data {
+/// # base: "".into(),
+/// # client_id: "".into(),
+/// # client_secret: "".into(),
+/// # redirect: "".into(),
+/// # token: "".into(),
+/// # };
+/// struct HomeTimeline {
+/// client: Mastodon,
+/// page: RefCell<Option<OwnedPage<Status, HttpSender>>>,
+/// }
+/// let client = Mastodon::from(data);
+/// let home = client.get_home_timeline()?.to_owned();
+/// let tl = HomeTimeline {
+/// client,
+/// page: RefCell::new(Some(home)),
+/// };
+/// # Ok(())
+/// # }
+/// ```
+#[derive(Debug, Clone)]
+pub struct OwnedPage<T: for<'de> Deserialize<'de>, H: HttpSend> {
+ mastodon: Mastodon<H>,
+ next: Option<Url>,
+ prev: Option<Url>,
+ /// Initial set of items
+ pub initial_items: Vec<T>,
+}
+
+impl<T: for<'de> Deserialize<'de>, H: HttpSend> OwnedPage<T, H> {
+ pages! {
+ next: next_page,
+ prev: prev_page
+ }
+}
+
+impl<'a, T: for<'de> Deserialize<'de>, H: HttpSend> From<Page<'a, T, H>> for OwnedPage<T, H> {
+ fn from(page: Page<'a, T, H>) -> OwnedPage<T, H> {
+ OwnedPage {
+ mastodon: page.mastodon.clone(),
+ next: page.next,
+ prev: page.prev,
+ initial_items: page.initial_items,
+ }
+ }
+}
+
+/// Represents a single page of API results
+#[derive(Debug, Clone)]
+pub struct Page<'a, T: for<'de> Deserialize<'de>, H: 'a + HttpSend> {
+ mastodon: &'a Mastodon<H>,
+ next: Option<Url>,
+ prev: Option<Url>,
+ /// Initial set of items
+ pub initial_items: Vec<T>,
+}
+
impl<'a, T: for<'de> Deserialize<'de>, H: HttpSend> Page<'a, T, H> {
pages! {
next: next_page,
@@ -61,6 +122,44 @@ impl<'a, T: for<'de> Deserialize<'de>, H: HttpSend> Page<'a, T, H> {
}
impl<'a, T: Clone + for<'de> Deserialize<'de>, H: HttpSend> Page<'a, T, H> {
+ /// Returns an owned version of this struct that doesn't borrow the client
+ /// that created it
+ ///
+ /// # Example
+ ///
+ /// ```no_run
+ /// # extern crate elefren;
+ /// # use elefren::Mastodon;
+ /// # use elefren::page::OwnedPage;
+ /// # use elefren::http_send::HttpSender;
+ /// # use elefren::entities::status::Status;
+ /// # use std::cell::RefCell;
+ /// # use elefren::prelude::*;
+ /// # fn main() -> Result<(), elefren::Error> {
+ /// # let data = Data {
+ /// # base: "".into(),
+ /// # client_id: "".into(),
+ /// # client_secret: "".into(),
+ /// # redirect: "".into(),
+ /// # token: "".into(),
+ /// # };
+ /// struct HomeTimeline {
+ /// client: Mastodon,
+ /// page: RefCell<Option<OwnedPage<Status, HttpSender>>>,
+ /// }
+ /// let client = Mastodon::from(data);
+ /// let home = client.get_home_timeline()?.to_owned();
+ /// let tl = HomeTimeline {
+ /// client,
+ /// page: RefCell::new(Some(home)),
+ /// };
+ /// # Ok(())
+ /// # }
+ /// ```
+ pub fn to_owned(self) -> OwnedPage<T, H> {
+ OwnedPage::from(self)
+ }
+
/// Returns an iterator that provides a stream of `T`s
///
/// This abstracts away the process of iterating over each item in a page,