Permalink
Browse files

shuffle user::lookup around so i can fold lookup_ids and lookup_names in

  • Loading branch information...
QuietMisdreavus committed Aug 20, 2016
1 parent da0a320 commit 98b70429fac382b8f40921e7178261b6f8f1fff1
Showing with 20 additions and 4 deletions.
  1. +1 −1 examples/reciprocal.rs
  2. +13 −0 src/common/mod.rs
  3. +6 −3 src/user/mod.rs
@@ -24,7 +24,7 @@ fn main() {
println!("{} accounts that you follow follow you back.", reciprocals.len());
for user in user::lookup_ids(&reciprocals, &config.con_token, &config.access_token).unwrap().response {
for user in user::lookup(&reciprocals, &config.con_token, &config.access_token).unwrap().response {
println!("{} (@{})", user.name, user.screen_name);
}
}
@@ -32,6 +32,7 @@ pub fn add_name_param<'a>(list: &mut ParamList<'a>, id: &UserID<'a>) -> Option<C
}
///Convenience enum to generalize between referring to an account by numeric ID or by screen name.
#[derive(Debug, Clone)]
pub enum UserID<'a> {
///Referring via the account's numeric ID.
ID(i64),
@@ -45,6 +46,12 @@ impl<'a> From<i64> for UserID<'a> {
}
}
impl<'a> From<&'a i64> for UserID<'a> {
fn from(id: &'a i64) -> UserID<'a> {
UserID::ID(*id)
}
}
impl<'a> From<&'a str> for UserID<'a> {
fn from(name: &'a str) -> UserID<'a> {
UserID::ScreenName(name)
@@ -56,3 +63,9 @@ impl<'a> From<&'a String> for UserID<'a> {
UserID::ScreenName(name.as_str())
}
}
impl<'a> From<&'a UserID<'a>> for UserID<'a> {
fn from(id: &'a UserID<'a>) -> UserID<'a> {
id.clone()
}
}
@@ -64,6 +64,7 @@ pub use user::structs::*;
//---Groups of users---
///Lookup a set of Twitter users by their numerical ID.
#[deprecated(note = "you can call lookup with &[i64] now")]
pub fn lookup_ids(ids: &[i64], con_token: &auth::Token, access_token: &auth::Token)
-> Result<Response<Vec<TwitterUser>>, error::Error>
{
@@ -77,6 +78,7 @@ pub fn lookup_ids(ids: &[i64], con_token: &auth::Token, access_token: &auth::Tok
}
///Lookup a set of Twitter users by their screen name.
#[deprecated(note = "you can call lookup with &[&str] and &[String] now")]
pub fn lookup_names<S: Borrow<str>>(names: &[S], con_token: &auth::Token, access_token: &auth::Token)
-> Result<Response<Vec<TwitterUser>>, error::Error>
{
@@ -90,19 +92,20 @@ pub fn lookup_names<S: Borrow<str>>(names: &[S], con_token: &auth::Token, access
}
///Lookup a set of Twitter users by both ID and screen name, as applicable.
pub fn lookup(accts: &[UserID], con_token: &auth::Token, access_token: &auth::Token)
pub fn lookup<'a, T: 'a>(accts: &'a [T], con_token: &auth::Token, access_token: &auth::Token)
-> Result<Response<Vec<TwitterUser>>, error::Error>
where &'a T: Into<UserID<'a>>
{
let mut params = HashMap::new();
let id_param = accts.iter()
.filter_map(|x| match *x {
.filter_map(|x| match x.into() {
UserID::ID(id) => Some(id.to_string()),
_ => None,
})
.collect::<Vec<_>>()
.join(",");
let name_param = accts.iter()
.filter_map(|x| match *x {
.filter_map(|x| match x.into() {
UserID::ScreenName(name) => Some(name),
_ => None,
})

0 comments on commit 98b7042

Please sign in to comment.