Skip to content

Commit

Permalink
imp(duration): use std::time::Duration everywhere
Browse files Browse the repository at this point in the history
This gets rid of the time crate, which was necessary only while Duration wasn't stable
in std.
  • Loading branch information
Byron committed Feb 7, 2016
1 parent c039db5 commit c18ae07
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ build = "src/build.rs"

[dependencies]
chrono = ">= 0.2"
time = ">= 0.1"
log = ">= 0.3"
mime = ">= 0.1"
url = ">= 0.5"
Expand Down
19 changes: 8 additions & 11 deletions examples/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ extern crate hyper;
extern crate chrono;
extern crate getopts;
extern crate open;
extern crate time;

use oauth2::GetToken;
use chrono::{Local};
use getopts::{HasArg,Options,Occur,Fail};
use std::env;
use std::default::Default;
use time::Duration;
use std::thread::sleep_ms;
use std::time::Duration;
use std::thread::sleep;


fn usage(program: &str, opts: &Options, err: Option<Fail>) -> ! {
Expand Down Expand Up @@ -54,9 +53,7 @@ fn main() {
token_uri: Default::default(),
auth_uri: Default::default(),
redirect_uris: Default::default(),
client_email: None,
auth_provider_x509_cert_url: None,
client_x509_cert_url: None
..Default::default()
};

println!("THIS PROGRAM PRINTS ALL COMMUNICATION TO STDERR !!!");
Expand All @@ -69,9 +66,9 @@ fn main() {
You have time until {} to do that.
Do not terminate the program until you deny or grant access !",
pi.user_code, pi.verification_url, pi.expires_at.with_timezone(&Local));
let delay = Duration::seconds(5);
println!("Browser opens automatically in {} seconds", delay);
sleep_ms(delay.num_milliseconds() as u32);
let delay = Duration::from_secs(5);
println!("Browser opens automatically in {:?} seconds", delay);
sleep(delay);
open::that(&pi.verification_url).ok();
println!("DONE - waiting for authorization ...");
}
Expand All @@ -81,7 +78,7 @@ fn main() {
connector: hyper::net::HttpConnector
});

match oauth2::Authenticator::new(&secret, StdoutHandler, client,
match oauth2::Authenticator::new(&secret, StdoutHandler, client,
oauth2::NullStorage, None).token(&m.free) {
Ok(t) => {
println!("Authentication granted !");
Expand All @@ -94,4 +91,4 @@ fn main() {
std::process::exit(10);
}
}
}
}
13 changes: 7 additions & 6 deletions src/device.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::iter::IntoIterator;
use time::Duration;
use std::time::Duration;
use std::default::Default;
use std::fmt;

Expand All @@ -8,9 +8,10 @@ use hyper::header::ContentType;
use url::form_urlencoded;
use itertools::Itertools;
use serde_json as json;
use chrono::{DateTime,UTC};
use chrono::{DateTime,UTC, self};
use std::borrow::BorrowMut;
use std::io::Read;
use std::i64;

use common::{Token, FlowType, Flow, JsonError};

Expand Down Expand Up @@ -230,8 +231,8 @@ impl<C> DeviceFlow<C>
let pi = PollInformation {
user_code: decoded.user_code,
verification_url: decoded.verification_url,
expires_at: UTC::now() + Duration::seconds(decoded.expires_in),
interval: Duration::seconds(decoded.interval),
expires_at: UTC::now() + chrono::Duration::seconds(decoded.expires_in),
interval: Duration::from_secs(i64::abs(decoded.interval) as u64),
};
self.state = Some(DeviceFlowState::Pending(pi.clone()));

Expand Down Expand Up @@ -337,7 +338,7 @@ impl<C> DeviceFlow<C>
pub mod tests {
use super::*;
use std::default::Default;
use time::Duration;
use std::time::Duration;
use hyper;
use yup_hyper_mock::{SequentialConnector, MockStream};

Expand Down Expand Up @@ -394,7 +395,7 @@ pub mod tests {
match flow.request_code("bogus_client_id",
"bogus_secret",
&["https://www.googleapis.com/auth/youtube.upload"]) {
Ok(pi) => assert_eq!(pi.interval, Duration::seconds(0)),
Ok(pi) => assert_eq!(pi.interval, Duration::from_secs(0)),
_ => unreachable!(),
}

Expand Down
21 changes: 11 additions & 10 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::iter::IntoIterator;
use std::borrow::BorrowMut;
use std::collections::HashMap;
use std::hash::{SipHasher, Hash, Hasher};
use std::thread::sleep_ms;
use std::thread::sleep;
use std::cmp::min;
use std::error::Error;
use std::fmt;
Expand All @@ -11,7 +11,8 @@ use std::convert::From;
use common::{Token, FlowType, ApplicationSecret};
use device::{PollInformation, RequestError, DeviceFlow, PollError};
use refresh::{RefreshResult, RefreshFlow};
use chrono::{DateTime, UTC, Duration, Local};
use chrono::{DateTime, UTC, Local};
use std::time::Duration;
use hyper;


Expand Down Expand Up @@ -203,7 +204,7 @@ impl<D, S, C> Authenticator<D, S, C>
RequestError::HttpError(err) => {
match self.delegate.connection_error(&err) {
Retry::Abort|Retry::Skip => return Err(Box::new(StringError::from(&err as &Error))),
Retry::After(d) => sleep_ms(d.num_milliseconds() as u32),
Retry::After(d) => sleep(d),
}
},
RequestError::InvalidClient
Expand Down Expand Up @@ -234,7 +235,7 @@ impl<D, S, C> Authenticator<D, S, C>
match self.delegate.connection_error(err) {
Retry::Abort|Retry::Skip
=> return Err(Box::new(StringError::from(err as &Error))),
Retry::After(d) => sleep_ms(d.num_milliseconds() as u32),
Retry::After(d) => sleep(d),
}
},
&&PollError::Expired(ref t) => {
Expand All @@ -251,7 +252,7 @@ impl<D, S, C> Authenticator<D, S, C>
match self.delegate.pending(&pi) {
Retry::Abort|Retry::Skip
=> return Err(Box::new(StringError::new("Pending authentication aborted".to_string(), None))),
Retry::After(d) => sleep_ms(min(d, pi.interval).num_milliseconds() as u32),
Retry::After(d) => sleep(min(d, pi.interval)),
},
Ok(Some(token)) => return Ok(token)
}
Expand Down Expand Up @@ -301,7 +302,7 @@ impl<D, S, C> GetToken for Authenticator<D, S, C>
return Err(Box::new(StringError::new(
err.description().to_string(),
None))),
Retry::After(d) => sleep_ms(d.num_milliseconds() as u32),
Retry::After(d) => sleep(d),
}
},
RefreshResult::RefreshError(ref err_str, ref err_description) => {
Expand All @@ -322,7 +323,7 @@ impl<D, S, C> GetToken for Authenticator<D, S, C>
Retry::Skip => break,
Retry::Abort => return Err(Box::new(err)),
Retry::After(d) => {
sleep_ms(d.num_milliseconds() as u32);
sleep(d);
continue;
}
}
Expand Down Expand Up @@ -351,7 +352,7 @@ impl<D, S, C> GetToken for Authenticator<D, S, C>
Retry::Skip => break,
Retry::Abort => return Err(Box::new(err)),
Retry::After(d) => {
sleep_ms(d.num_milliseconds() as u32);
sleep(d);
continue;
}
}
Expand All @@ -367,7 +368,7 @@ impl<D, S, C> GetToken for Authenticator<D, S, C>
match self.delegate.token_storage_failure(false, &err) {
Retry::Abort|Retry::Skip => Err(Box::new(err)),
Retry::After(d) => {
sleep_ms(d.num_milliseconds() as u32);
sleep(d);
continue
}
}
Expand Down Expand Up @@ -437,7 +438,7 @@ pub trait AuthenticatorDelegate {
/// * Only used in `DeviceFlow`. Return value will only be used if it
/// is larger than the interval desired by the server.
fn pending(&mut self, &PollInformation) -> Retry {
Retry::After(Duration::seconds(5))
Retry::After(Duration::from_secs(5))
}

/// The server has returned a `user_code` which must be shown to the user,
Expand Down
3 changes: 1 addition & 2 deletions src/lib.rs.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ extern crate log;
extern crate yup_hyper_mock;
extern crate mime;
extern crate url;
extern crate time;
extern crate itertools;

mod device;
Expand All @@ -21,5 +20,5 @@ mod helper;
pub use device::{DeviceFlow, PollInformation, PollError};
pub use refresh::{RefreshFlow, RefreshResult};
pub use common::{Token, FlowType, ApplicationSecret, ConsoleApplicationSecret, Scheme, TokenType};
pub use helper::{TokenStorage, NullStorage, MemoryStorage, Authenticator,
pub use helper::{TokenStorage, NullStorage, MemoryStorage, Authenticator,
AuthenticatorDelegate, Retry, DefaultAuthenticatorDelegate, GetToken};

0 comments on commit c18ae07

Please sign in to comment.