feat(dev): spike to see how delegate can be work

To avoid an additional type parameter, we will use dynamic dispatch
for the delegate.

Having function overrides at some point seems like an excercise better
left for version 1.1 ;)
This commit is contained in:
Sebastian Thiel
2015-03-09 11:18:44 +01:00
parent 678b6929ca
commit 432faa275f
3 changed files with 91 additions and 5 deletions

View File

@@ -1,6 +1,5 @@
use std::marker::MarkerTrait;
use std::io::{Read, Seek};
use std::borrow::BorrowMut;
use oauth2;
use hyper;
@@ -52,7 +51,7 @@ struct JsonServerError {
///
/// It contains methods to deal with all common issues, as well with the ones related to
/// uploading media
pub trait Delegate: Clone {
pub trait Delegate {
/// Called whenever there is an HttpError, usually if there are network problems.
///
@@ -61,3 +60,8 @@ pub trait Delegate: Clone {
oauth2::Retry::Abort
}
}
#[derive(Default)]
pub struct DefaultDelegate;
impl Delegate for DefaultDelegate {}

View File

@@ -12,8 +12,9 @@
//! # }
//! ```
use std::marker::PhantomData;
use std::borrow::BorrowMut;
use std::cell::RefCell;
use std::borrow::BorrowMut;
use std::default::Default;
use hyper;
use oauth2;
@@ -45,6 +46,10 @@ impl<'a, C, NC, A> YouTube<C, NC, A>
pub fn videos(&'a self) -> videos::Service<'a, C, NC, A> {
videos::Service::new(&self)
}
pub fn channel_sections(&'a self) -> ChannelSectionMethodsBuilder<'a, C, NC, A> {
ChannelSectionMethodsBuilder { hub: &self }
}
}
@@ -77,4 +82,81 @@ mod tests {
let v = yt.videos().insert("snippet", &Default::default());
}
}
#[test] fn helper_test() {
use std::default::Default;
use oauth2::{Authenticator, DefaultAuthenticatorDelegate, ApplicationSecret, MemoryStorage};
let secret: ApplicationSecret = Default::default();
let auth = Authenticator::new(&secret, DefaultAuthenticatorDelegate,
hyper::Client::new(),
<MemoryStorage as Default>::default(), None);
let mut hub = YouTube::new(hyper::Client::new(), auth);
let result = hub.channel_sections().insert()
.delegate(&mut <DefaultDelegate as Default>::default())
.doit();
}
}
pub struct ChannelSectionMethodsBuilder<'a, C, NC, A>
where NC: 'a,
C: 'a,
A: 'a, {
hub: &'a YouTube<C, NC, A>,
}
impl<'a, C, NC, A> ChannelSectionMethodsBuilder<'a, C, NC, A> {
/// Create a builder to help you perform the following task:
///
/// Adds a channelSection for the authenticated user's channel.
pub fn insert(&self) -> ChannelSectionInsertMethodBuilder<'a, C, NC, A> {
ChannelSectionInsertMethodBuilder {
hub: self.hub,
_delegate: Default::default(),
}
}
}
pub struct ChannelSectionInsertMethodBuilder<'a, C, NC, A>
where NC: 'a,
C: 'a,
A: 'a, {
hub: &'a YouTube<C, NC, A>,
_delegate: Option<&'a mut Delegate>,
}
impl<'a, C, NC, A> ChannelSectionInsertMethodBuilder<'a, C, NC, A> where NC: hyper::net::NetworkConnector, C: BorrowMut<hyper::Client<NC>> + 'a, A: oauth2::GetToken {
/// Perform the operation you have build so far.
/// TODO: Build actual call
pub fn doit(mut self) -> () {
if self._delegate.is_some() {
self._delegate.as_mut().unwrap().connection_error(hyper::HttpError::HttpStatusError);
}
}
pub fn delegate(mut self, new_value: &'a mut Delegate) -> ChannelSectionInsertMethodBuilder<'a, C, NC, A> {
self._delegate = Some(new_value);
self
}
}
pub trait Delegate {
/// Called whenever there is an HttpError, usually if there are network problems.
///
/// Return retry information.
fn connection_error(&mut self, hyper::HttpError) -> oauth2::Retry {
oauth2::Retry::Abort
}
}
#[derive(Default)]
pub struct DefaultDelegate;
impl Delegate for DefaultDelegate {}

View File

@@ -1,4 +1,4 @@
#![feature(core)]
#![feature(core,io)]
//! library with code shared by all generated implementations
extern crate hyper;
extern crate "rustc-serialize" as rustc_serialize;