Skip to content

Commit

Permalink
shuffle user::lookup around so i can fold lookup_ids and lookup_names in
Browse files Browse the repository at this point in the history
  • Loading branch information
QuietMisdreavus committed Aug 20, 2016
1 parent da0a320 commit 98b7042
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/reciprocal.rs
Expand Up @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/common/mod.rs
Expand Up @@ -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),
Expand All @@ -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)
Expand All @@ -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()
}
}
9 changes: 6 additions & 3 deletions src/user/mod.rs
Expand Up @@ -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>
{
Expand All @@ -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>
{
Expand All @@ -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,
})
Expand Down

0 comments on commit 98b7042

Please sign in to comment.