Skip to content

Commit

Permalink
Switch usize datatype to u64
Browse files Browse the repository at this point in the history
Usize wasn't really a good fit.
  • Loading branch information
Kobzol committed Feb 3, 2024
1 parent 141546c commit f96691a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 57 deletions.
6 changes: 3 additions & 3 deletions src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ impl GraphPageInfo {
pub(crate) struct Team {
/// The ID returned by the GitHub API can't be empty, but the None marks teams "created" during
/// a dry run and not actually present on GitHub, so other methods can avoid acting on them.
pub(crate) id: Option<usize>,
pub(crate) id: Option<u64>,
pub(crate) name: String,
pub(crate) description: Option<String>,
pub(crate) privacy: TeamPrivacy,
Expand Down Expand Up @@ -301,11 +301,11 @@ pub(crate) struct TeamMember {
pub(crate) role: TeamRole,
}

fn user_node_id(id: usize) -> String {
fn user_node_id(id: u64) -> String {
base64::encode(format!("04:User{id}"))
}

fn team_node_id(id: usize) -> String {
fn team_node_id(id: u64) -> String {
base64::encode(format!("04:Team{id}"))
}

Expand Down
18 changes: 9 additions & 9 deletions src/github/api/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ use std::collections::{HashMap, HashSet};

pub(crate) trait GithubRead {
/// Get user names by user ids
fn usernames(&self, ids: &[usize]) -> anyhow::Result<HashMap<usize, String>>;
fn usernames(&self, ids: &[u64]) -> anyhow::Result<HashMap<u64, String>>;

/// Get the owners of an org
fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<usize>>;
fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<u64>>;

/// Get all teams associated with a org
///
Expand All @@ -20,7 +20,7 @@ pub(crate) trait GithubRead {
/// Get the team by name and org
fn team(&self, org: &str, team: &str) -> anyhow::Result<Option<Team>>;

fn team_memberships(&self, team: &Team) -> anyhow::Result<HashMap<usize, TeamMember>>;
fn team_memberships(&self, team: &Team) -> anyhow::Result<HashMap<u64, TeamMember>>;

/// The GitHub names of users invited to the given team
fn team_membership_invitations(&self, org: &str, team: &str)
Expand Down Expand Up @@ -56,11 +56,11 @@ impl GitHubApiRead {
}

impl GithubRead for GitHubApiRead {
fn usernames(&self, ids: &[usize]) -> anyhow::Result<HashMap<usize, String>> {
fn usernames(&self, ids: &[u64]) -> anyhow::Result<HashMap<u64, String>> {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct Usernames {
database_id: usize,
database_id: u64,
login: String,
}
#[derive(serde::Serialize)]
Expand Down Expand Up @@ -93,10 +93,10 @@ impl GithubRead for GitHubApiRead {
Ok(result)
}

fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<usize>> {
fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<u64>> {
#[derive(serde::Deserialize, Eq, PartialEq, Hash)]
struct User {
id: usize,
id: u64,
}
let mut owners = HashSet::new();
self.client.rest_paginated(
Expand Down Expand Up @@ -130,7 +130,7 @@ impl GithubRead for GitHubApiRead {
.send_option(Method::GET, &format!("orgs/{org}/teams/{team}"))
}

fn team_memberships(&self, team: &Team) -> anyhow::Result<HashMap<usize, TeamMember>> {
fn team_memberships(&self, team: &Team) -> anyhow::Result<HashMap<u64, TeamMember>> {
#[derive(serde::Deserialize)]
struct RespTeam {
members: RespMembers,
Expand All @@ -149,7 +149,7 @@ impl GithubRead for GitHubApiRead {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct RespNode {
database_id: usize,
database_id: u64,
login: String,
}
#[derive(serde::Serialize)]
Expand Down
21 changes: 12 additions & 9 deletions src/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ struct SyncGitHub {
github: Box<dyn GithubRead>,
teams: Vec<rust_team_data::v1::Team>,
repos: Vec<rust_team_data::v1::Repo>,
usernames_cache: HashMap<usize, String>,
org_owners: HashMap<String, HashSet<usize>>,
usernames_cache: HashMap<u64, String>,
org_owners: HashMap<String, HashSet<u64>>,
}

impl SyncGitHub {
Expand All @@ -45,7 +45,8 @@ impl SyncGitHub {
.flat_map(|team| &team.members)
.copied()
.collect::<HashSet<_>>();
let usernames_cache = github.usernames(&users.into_iter().collect::<Vec<_>>())?;
let usernames_cache =
github.usernames(&users.into_iter().map(|id| id as u64).collect::<Vec<_>>())?;

debug!("caching organization owners");
let orgs = teams
Expand Down Expand Up @@ -134,8 +135,9 @@ impl SyncGitHub {
.members
.iter()
.map(|member| {
let expected_role = self.expected_role(&github_team.org, *member);
(self.usernames_cache[member].clone(), expected_role)
let member = *member as u64;
let expected_role = self.expected_role(&github_team.org, member);
(self.usernames_cache[&member].clone(), expected_role)
})
.collect();
return Ok(TeamDiff::Create(CreateTeamDiff {
Expand Down Expand Up @@ -176,9 +178,10 @@ impl SyncGitHub {

// Ensure all expected members are in the team
for member in &github_team.members {
let expected_role = self.expected_role(&github_team.org, *member);
let username = &self.usernames_cache[member];
if let Some(member) = current_members.remove(member) {
let member = *member as u64;
let expected_role = self.expected_role(&github_team.org, member);
let username = &self.usernames_cache[&member];
if let Some(member) = current_members.remove(&member) {
if member.role != expected_role {
member_diffs.push((
username.clone(),
Expand Down Expand Up @@ -332,7 +335,7 @@ impl SyncGitHub {
Ok(branch_protection_diffs)
}

fn expected_role(&self, org: &str, user: usize) -> TeamRole {
fn expected_role(&self, org: &str, user: u64) -> TeamRole {
if let Some(true) = self
.org_owners
.get(org)
Expand Down
22 changes: 11 additions & 11 deletions src/github/tests/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ pub struct DataModel {
}

impl DataModel {
pub fn add_user(&mut self, name: &str) -> usize {
pub fn add_user(&mut self, name: &str) -> u64 {
let github_id = self.people.len();
self.people.push(Person {
name: name.to_string(),
email: Some(format!("{name}@rust.com")),
github_id,
});
github_id
github_id as u64
}

pub fn gh_model(&self) -> GithubMock {
GithubMock {
users: self
.people
.iter()
.map(|user| (user.github_id, user.name.clone()))
.map(|user| (user.github_id as u64, user.name.clone()))
.collect(),
owners: Default::default(),
teams: self
Expand All @@ -46,7 +46,7 @@ impl DataModel {
.into_iter()
.enumerate()
.map(|(id, team)| api::Team {
id: Some(id),
id: Some(id as u64),
name: team.name.clone(),
description: None,
privacy: TeamPrivacy::Closed,
Expand Down Expand Up @@ -107,12 +107,12 @@ impl TeamData {
}

impl TeamDataBuilder {
pub fn gh_team(mut self, name: &str, members: &[usize]) -> Self {
pub fn gh_team(mut self, name: &str, members: &[u64]) -> Self {
let mut gh_teams = self.gh_teams.unwrap_or_default();
gh_teams.push(GitHubTeam {
org: DEFAULT_ORG.to_string(),
name: name.to_string(),
members: members.to_vec(),
members: members.into_iter().map(|v| *v as usize).collect(),
});
self.gh_teams = Some(gh_teams);
self
Expand All @@ -122,13 +122,13 @@ impl TeamDataBuilder {
/// Represents the state of GitHub repositories, teams and users.
#[derive(Default, Clone)]
pub struct GithubMock {
users: HashMap<usize, String>,
owners: HashMap<String, Vec<usize>>,
users: HashMap<u64, String>,
owners: HashMap<String, Vec<u64>>,
teams: Vec<Team>,
}

impl GithubRead for GithubMock {
fn usernames(&self, ids: &[usize]) -> anyhow::Result<HashMap<usize, String>> {
fn usernames(&self, ids: &[u64]) -> anyhow::Result<HashMap<u64, String>> {
Ok(self
.users
.iter()
Expand All @@ -137,7 +137,7 @@ impl GithubRead for GithubMock {
.collect())
}

fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<usize>> {
fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<u64>> {
Ok(self
.owners
.get(org)
Expand All @@ -159,7 +159,7 @@ impl GithubRead for GithubMock {
Ok(self.teams.iter().find(|t| t.name == team).cloned())
}

fn team_memberships(&self, _team: &Team) -> anyhow::Result<HashMap<usize, TeamMember>> {
fn team_memberships(&self, _team: &Team) -> anyhow::Result<HashMap<u64, TeamMember>> {
todo!()
}

Expand Down
4 changes: 2 additions & 2 deletions src/mailgun/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Mailgun {
}
}

pub(super) fn get_routes(&self, skip: Option<usize>) -> Result<RoutesResponse, Error> {
pub(super) fn get_routes(&self, skip: Option<u64>) -> Result<RoutesResponse, Error> {
let url = if let Some(skip) = skip {
format!("routes?skip={skip}")
} else {
Expand Down Expand Up @@ -119,7 +119,7 @@ impl Mailgun {
#[derive(serde::Deserialize)]
pub(super) struct RoutesResponse {
pub(super) items: Vec<Route>,
pub(super) total_count: usize,
pub(super) total_count: u64,
}

#[derive(serde::Deserialize)]
Expand Down
4 changes: 2 additions & 2 deletions src/mailgun/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ pub(crate) fn run(

let mut routes = Vec::new();
let mut response = mailgun.get_routes(None)?;
let mut cur = 0;
let mut cur = 0u64;
while !response.items.is_empty() {
cur += response.items.len();
cur += response.items.len() as u64;
routes.extend(response.items);
if cur >= response.total_count {
break;
Expand Down
16 changes: 8 additions & 8 deletions src/zulip/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ZulipApi {
&self,
user_group_name: &str,
description: &str,
member_ids: &[usize],
member_ids: &[u64],
) -> anyhow::Result<()> {
log::info!(
"creating Zulip user group '{}' with description '{}' and member ids: {:?}",
Expand Down Expand Up @@ -98,9 +98,9 @@ impl ZulipApi {

pub(crate) fn update_user_group_members(
&self,
user_group_id: usize,
add_ids: &[usize],
remove_ids: &[usize],
user_group_id: u64,
add_ids: &[u64],
remove_ids: &[u64],
) -> anyhow::Result<()> {
if add_ids.is_empty() && remove_ids.is_empty() {
log::debug!(
Expand Down Expand Up @@ -164,7 +164,7 @@ impl ZulipApi {
}

/// Serialize a slice of numbers as a JSON array
fn serialize_as_array(items: &[usize]) -> String {
fn serialize_as_array(items: &[u64]) -> String {
let items = items
.iter()
.map(|id| id.to_string())
Expand All @@ -185,7 +185,7 @@ pub(crate) struct ZulipUser {
// Note: users may hide their emails
#[serde(rename = "delivery_email")]
pub(crate) email: Option<String>,
pub(crate) user_id: usize,
pub(crate) user_id: u64,
}

/// A collection of Zulip user groups
Expand All @@ -197,7 +197,7 @@ struct ZulipUserGroups {
/// A single Zulip user group
#[derive(Deserialize)]
pub(crate) struct ZulipUserGroup {
pub(crate) id: usize,
pub(crate) id: u64,
pub(crate) name: String,
pub(crate) members: Vec<usize>,
pub(crate) members: Vec<u64>,
}
Loading

0 comments on commit f96691a

Please sign in to comment.