From 432faa275f89bb1c3ab00b60ff07225eec5a4489 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 9 Mar 2015 11:18:44 +0100 Subject: [PATCH] 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 ;) --- src/rust/cmn.rs | 8 +++-- src/rust/dev/mod.rs | 86 +++++++++++++++++++++++++++++++++++++++++++-- src/rust/lib.rs | 2 +- 3 files changed, 91 insertions(+), 5 deletions(-) diff --git a/src/rust/cmn.rs b/src/rust/cmn.rs index 2bca48a760..46addc2d6b 100644 --- a/src/rust/cmn.rs +++ b/src/rust/cmn.rs @@ -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 {} diff --git a/src/rust/dev/mod.rs b/src/rust/dev/mod.rs index a286393e59..77e7d294ac 100644 --- a/src/rust/dev/mod.rs +++ b/src/rust/dev/mod.rs @@ -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 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()); } -} \ No newline at end of file + + #[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(), + ::default(), None); + let mut hub = YouTube::new(hyper::Client::new(), auth); + let result = hub.channel_sections().insert() + .delegate(&mut ::default()) + .doit(); + } +} + +pub struct ChannelSectionMethodsBuilder<'a, C, NC, A> + where NC: 'a, + C: 'a, + A: 'a, { + + hub: &'a YouTube, +} + +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, + _delegate: Option<&'a mut Delegate>, +} + + +impl<'a, C, NC, A> ChannelSectionInsertMethodBuilder<'a, C, NC, A> where NC: hyper::net::NetworkConnector, C: BorrowMut> + '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 {} diff --git a/src/rust/lib.rs b/src/rust/lib.rs index afa9f297a4..37d32a636f 100644 --- a/src/rust/lib.rs +++ b/src/rust/lib.rs @@ -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;