Skip to content

Commit

Permalink
fun with continuations
Browse files Browse the repository at this point in the history
  • Loading branch information
nick42d committed Sep 1, 2024
1 parent f9c64ee commit 8808c90
Showing 1 changed file with 54 additions and 31 deletions.
85 changes: 54 additions & 31 deletions ytmapi-rs/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,49 +183,72 @@ pub mod album {

// For future use.
pub mod continuations {
use super::{BasicSearch, GetQuery, PostMethod, PostQuery, Query, QueryMethod, SearchQuery};
use crate::{
auth::AuthToken,
parse::{ParseFrom, ProcessedResult},
Result,
};

use super::{BasicSearch, PostMethod, PostQuery, Query, SearchQuery};
use async_stream::{stream, try_stream};
use std::borrow::Cow;
use tokio_stream::Stream;

pub struct GetContinuationsQuery<Q> {
continuation_params: String,
query: Q,
}
impl<'a> ParseFrom<GetContinuationsQuery<SearchQuery<'a, BasicSearch>>> for () {
fn parse_from(
_: ProcessedResult<GetContinuationsQuery<SearchQuery<'a, BasicSearch>>>,
) -> crate::Result<Self> {
todo!()
}
trait Continuable {
fn get_continuation_params(&self) -> Option<String>;
}
// TODO: Output type
impl<'a, A: AuthToken> Query<A> for GetContinuationsQuery<SearchQuery<'a, BasicSearch>>
trait StreamingQuery<A: AuthToken>: Query<A>
where
SearchQuery<'a, BasicSearch>: Query<A>,
Self::Output: Continuable,
{
type Output = ();
type Method = PostMethod;
fn stream(
&self,
client: &crate::client::Client,
tok: &A,
) -> impl Stream<Item = Result<Self::Output>> {
try_stream! {
let first_res: Self::Output = Self::Method::call(self, client, tok).await?.process()?.parse_into()?;
let first_cont_pars = Continuable::get_continuation_params(&first_res);
yield first_res;
if let Some(first_cont_pars) = first_cont_pars {
let query = GetContinuationsQuery {
continuation_params: first_cont_pars,
query: self
};
let next = <GetContinuationsQuery<'_, _> as Query<A>>::Method::call(&query, client, tok).await?.process()?.parse_into()?;
}
}
}
}
impl<'a> PostQuery for GetContinuationsQuery<SearchQuery<'a, BasicSearch>>

pub struct GetContinuationsQuery<'a, Q> {
continuation_params: String,
query: &'a Q,
}
// TODO: Output type
impl<'a, Q: Query<A>, A: AuthToken> Query<A> for GetContinuationsQuery<'a, Q>
where
SearchQuery<'a, BasicSearch>: PostQuery,
Q::Output: ParseFrom<Self>,
Q::Method: QueryMethod<Self, A, <Q as Query<A>>::Output>,
{
fn header(&self) -> serde_json::Map<String, serde_json::Value> {
self.query.header()
}
fn path(&self) -> &str {
self.query.path()
}
fn params(&self) -> Option<Cow<str>> {
Some(Cow::Borrowed(&self.continuation_params))
}
}
impl<Q> GetContinuationsQuery<Q> {
pub fn new(c_params: String, query: Q) -> GetContinuationsQuery<Q> {
type Output = Q::Output;
type Method = Q::Method;
}
// impl<'a> PostQuery for GetContinuationsQuery<SearchQuery<'a, BasicSearch>>
// where
// SearchQuery<'a, BasicSearch>: PostQuery,
// {
// fn header(&self) -> serde_json::Map<String, serde_json::Value> {
// self.query.header()
// }
// fn path(&self) -> &str {
// self.query.path()
// }
// fn params(&self) -> Option<Cow<str>> {
// Some(Cow::Borrowed(&self.continuation_params))
// }
// }
impl<'a, Q> GetContinuationsQuery<'a, Q> {
pub fn new(c_params: String, query: &'a Q) -> GetContinuationsQuery<'a, Q> {
GetContinuationsQuery {
continuation_params: c_params,
query,
Expand Down

0 comments on commit 8808c90

Please sign in to comment.