Skip to content

Commit

Permalink
refactor: standarize the use of usize for unsigned integers
Browse files Browse the repository at this point in the history
usize is used in a lot of places in the code by cast os as u32. To
standarize the usage of unsigned integers, usize is going to be used
in detriment of u32.

Closes #35

Signed-off-by: Thiago Duvanel <thiago.duvanel@usp.br>
  • Loading branch information
th-duvanel committed Sep 11, 2024
1 parent b636f2c commit c2f380e
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 60 deletions.
53 changes: 26 additions & 27 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ mod config;

pub struct BookmarkedPatchsetsState {
pub bookmarked_patchsets: Vec<Patch>,
pub patchset_index: u32,
pub patchset_index: usize,
}

impl BookmarkedPatchsetsState {
pub fn select_below_patchset(&mut self) {
if (self.patchset_index as usize) + 1 < self.bookmarked_patchsets.len() {
if self.patchset_index + 1 < self.bookmarked_patchsets.len() {
self.patchset_index += 1;
}
}
Expand All @@ -28,7 +28,7 @@ impl BookmarkedPatchsetsState {

fn get_selected_patchset(&self) -> Patch {
self.bookmarked_patchsets
.get(self.patchset_index as usize)
.get(self.patchset_index)
.unwrap()
.clone()
}
Expand All @@ -54,13 +54,13 @@ pub struct LatestPatchsetsState {
lore_session: LoreSession,
lore_api_client: BlockingLoreAPIClient,
target_list: String,
page_number: u32,
patchset_index: u32,
page_size: u32,
page_number: usize,
patchset_index: usize,
page_size: usize,
}

impl LatestPatchsetsState {
pub fn new(target_list: String, page_size: u32) -> LatestPatchsetsState {
pub fn new(target_list: String, page_size: usize) -> LatestPatchsetsState {
LatestPatchsetsState {
lore_session: LoreSession::new(target_list.clone()),
lore_api_client: BlockingLoreAPIClient::new(),
Expand All @@ -86,8 +86,7 @@ impl LatestPatchsetsState {
}

pub fn select_below_patchset(&mut self) {
if (self.patchset_index as usize) + 1
< self.lore_session.get_representative_patches_ids().len()
if self.patchset_index + 1 < self.lore_session.get_representative_patches_ids().len()
&& self.patchset_index + 1 < self.page_size * self.page_number
{
self.patchset_index += 1;
Expand All @@ -104,7 +103,7 @@ impl LatestPatchsetsState {
}

pub fn increment_page(&mut self) {
let patchsets_processed: u32 = self
let patchsets_processed: usize = self
.lore_session
.get_representative_patches_ids()
.len()
Expand All @@ -129,19 +128,19 @@ impl LatestPatchsetsState {
&self.target_list
}

pub fn get_page_number(&self) -> u32 {
pub fn get_page_number(&self) -> usize {
self.page_number
}

pub fn get_patchset_index(&self) -> u32 {
pub fn get_patchset_index(&self) -> usize {
self.patchset_index
}

pub fn get_selected_patchset(&self) -> Patch {
let message_id: &str = self
.lore_session
.get_representative_patches_ids()
.get(self.patchset_index as usize)
.get(self.patchset_index)
.unwrap();

self.lore_session
Expand All @@ -159,8 +158,8 @@ impl LatestPatchsetsState {
pub struct PatchsetDetailsAndActionsState {
pub representative_patch: Patch,
pub patches: Vec<String>,
pub preview_index: u32,
pub preview_scroll_offset: u32,
pub preview_index: usize,
pub preview_scroll_offset: usize,
pub patchset_actions: HashMap<PatchsetAction, bool>,
pub last_screen: CurrentScreen,
}
Expand All @@ -173,28 +172,28 @@ pub enum PatchsetAction {

impl PatchsetDetailsAndActionsState {
pub fn preview_next_patch(&mut self) {
if ((self.preview_index as usize) + 1) < self.patches.len() {
if (self.preview_index + 1) < self.patches.len() {
self.preview_index += 1;
self.preview_scroll_offset = 0;
}
}

pub fn preview_previous_patch(&mut self) {
if (self.preview_index as usize) > 0 {
if self.preview_index > 0 {
self.preview_index -= 1;
self.preview_scroll_offset = 0;
}
}

pub fn preview_scroll_down(&mut self) {
let number_of_lines = self.patches[self.preview_index as usize].lines().count();
if ((self.preview_scroll_offset as usize) + 1) <= number_of_lines {
let number_of_lines = self.patches[self.preview_index].lines().count();
if (self.preview_scroll_offset + 1) <= number_of_lines {
self.preview_scroll_offset += 1;
}
}

pub fn preview_scroll_up(&mut self) {
if (self.preview_scroll_offset as usize) > 0 {
if self.preview_scroll_offset > 0 {
self.preview_scroll_offset -= 1;
}
}
Expand Down Expand Up @@ -223,7 +222,7 @@ impl PatchsetDetailsAndActionsState {
pub fn reply_patchset_with_reviewed_by(
&self,
target_list: &str,
) -> color_eyre::Result<Vec<u32>> {
) -> color_eyre::Result<Vec<usize>> {
let lore_api_client = BlockingLoreAPIClient::new();
let (git_user_name, git_user_email) = lore_session::get_git_signature("");
let mut successful_indexes = Vec::new();
Expand Down Expand Up @@ -253,7 +252,7 @@ impl PatchsetDetailsAndActionsState {
let mut child = command.spawn().unwrap();
let exit_status = child.wait().unwrap();
if exit_status.success() {
successful_indexes.push(index as u32);
successful_indexes.push(index);
}
}

Expand All @@ -265,7 +264,7 @@ pub struct MailingListSelectionState {
pub mailing_lists: Vec<MailingList>,
pub target_list: String,
pub possible_mailing_lists: Vec<MailingList>,
pub highlighted_list_index: u32,
pub highlighted_list_index: usize,
pub mailing_lists_path: String,
}

Expand Down Expand Up @@ -320,7 +319,7 @@ impl MailingListSelectionState {
}

pub fn highlight_below_list(&mut self) {
if (self.highlighted_list_index as usize) + 1 < self.possible_mailing_lists.len() {
if self.highlighted_list_index + 1 < self.possible_mailing_lists.len() {
self.highlighted_list_index += 1;
}
}
Expand All @@ -331,7 +330,7 @@ impl MailingListSelectionState {

pub fn has_valid_target_list(&self) -> bool {
let list_length = self.possible_mailing_lists.len(); // Possible mailing list length
let list_index = self.highlighted_list_index as usize; // Index of the selected mailing list
let list_index = self.highlighted_list_index; // Index of the selected mailing list

if list_index < list_length {
return true;
Expand All @@ -354,7 +353,7 @@ pub struct App {
pub bookmarked_patchsets_state: BookmarkedPatchsetsState,
pub latest_patchsets_state: Option<LatestPatchsetsState>,
pub patchset_details_and_actions_state: Option<PatchsetDetailsAndActionsState>,
pub reviewed_patchsets: HashMap<String, Vec<u32>>,
pub reviewed_patchsets: HashMap<String, Vec<usize>>,
pub config: Config,
}

Expand Down Expand Up @@ -396,7 +395,7 @@ impl App {
pub fn init_latest_patchsets_state(&mut self) {
// the target mailing list for "latest patchsets" is the highlighted
// entry in the possible lists of "mailing list selection"
let list_index = self.mailing_list_selection_state.highlighted_list_index as usize;
let list_index = self.mailing_list_selection_state.highlighted_list_index;
let target_list = self.mailing_list_selection_state.possible_mailing_lists[list_index]
.get_name()
.to_string();
Expand Down
4 changes: 2 additions & 2 deletions src/app/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::env;

pub struct Config {
pub page_size: u32,
pub page_size: usize,
pub patchsets_cache_dir: String,
pub bookmarked_patchsets_path: String,
pub mailing_lists_path: String,
Expand All @@ -10,7 +10,7 @@ pub struct Config {

impl Config {
pub fn build() -> Self {
let page_size: u32 = match env::var("PATCH_HUB_PAGE_SIZE") {
let page_size: usize = match env::var("PATCH_HUB_PAGE_SIZE") {
Ok(value) => value.parse().unwrap(),
Err(_) => 30,
};
Expand Down
8 changes: 4 additions & 4 deletions src/lore_api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ pub trait PatchFeedRequest {
fn request_patch_feed(
&self,
target_list: &str,
min_index: u32,
min_index: usize,
) -> Result<String, FailedFeedRequest>;
}

impl PatchFeedRequest for BlockingLoreAPIClient {
fn request_patch_feed(
&self,
target_list: &str,
min_index: u32,
min_index: usize,
) -> Result<String, FailedFeedRequest> {
let feed_request: String =
format!("{LORE_DOMAIN}/{target_list}/{BASE_QUERY_FOR_FEED_REQUEST}&o={min_index}");
Expand Down Expand Up @@ -73,14 +73,14 @@ pub enum FailedAvailableListsRequest {
pub trait AvailableListsRequest {
fn request_available_lists(
&self,
min_index: u32,
min_index: usize,
) -> Result<String, FailedAvailableListsRequest>;
}

impl AvailableListsRequest for BlockingLoreAPIClient {
fn request_available_lists(
&self,
min_index: u32,
min_index: usize,
) -> Result<String, FailedAvailableListsRequest> {
let available_lists_request: String = format!("{LORE_DOMAIN}/?&o={min_index}");

Expand Down
25 changes: 13 additions & 12 deletions src/lore_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ use std::{
#[cfg(test)]
mod tests;

const LORE_PAGE_SIZE: u32 = 200;
const LORE_PAGE_SIZE: usize = 200;

pub struct LoreSession {
representative_patches_ids: Vec<String>,
processed_patches_map: HashMap<String, Patch>,
patch_regex: PatchRegex,
target_list: String,
min_index: u32,
min_index: usize,
}

impl LoreSession {
Expand All @@ -51,12 +51,12 @@ impl LoreSession {
pub fn process_n_representative_patches<T: PatchFeedRequest>(
&mut self,
lore_api_client: &T,
n: u32,
n: usize,
) -> Result<(), FailedFeedRequest> {
let mut patch_feed: PatchFeed;
let mut processed_patches_ids: Vec<String>;

while self.representative_patches_ids.len() < usize::try_from(n).unwrap() {
while self.representative_patches_ids.len() < n {
match lore_api_client.request_patch_feed(&self.target_list, self.min_index) {
Ok(feed_response_body) => patch_feed = from_str(&feed_response_body).unwrap(),
Err(failed_feed_request) => return Err(failed_feed_request),
Expand Down Expand Up @@ -92,7 +92,7 @@ impl LoreSession {

fn update_representative_patches(&mut self, processed_patches_ids: Vec<String>) {
let mut patch: &Patch;
let mut patch_number_in_series: u32;
let mut patch_number_in_series: usize;

for message_id in processed_patches_ids {
patch = self.processed_patches_map.get(&message_id).unwrap();
Expand Down Expand Up @@ -121,13 +121,14 @@ impl LoreSession {
}
}

pub fn get_patch_feed_page(&self, page_size: u32, page_number: u32) -> Option<Vec<&Patch>> {
pub fn get_patch_feed_page(&self, page_size: usize, page_number: usize) -> Option<Vec<&Patch>> {
let mut patch_feed_page: Vec<&Patch> = Vec::new();
let representative_patches_ids_max_index: u32 = (self.representative_patches_ids.len() - 1)
let representative_patches_ids_max_index: usize = (self.representative_patches_ids.len()
- 1)
.try_into()
.unwrap();
let lower_end: u32 = page_size * (page_number - 1);
let mut upper_end: u32 = page_size * page_number;
let lower_end: usize = page_size * (page_number - 1);
let mut upper_end: usize = page_size * page_number;

if representative_patches_ids_max_index <= lower_end {
return None;
Expand All @@ -140,7 +141,7 @@ impl LoreSession {
for i in lower_end..upper_end {
patch_feed_page.push(
self.processed_patches_map
.get(&self.representative_patches_ids[usize::try_from(i).unwrap()])
.get(&self.representative_patches_ids[i])
.unwrap(),
)
}
Expand Down Expand Up @@ -492,7 +493,7 @@ pub fn get_git_signature(git_repo_path: &str) -> (String, String) {
}

pub fn save_reviewed_patchsets(
reviewed_patchsets: &HashMap<String, Vec<u32>>,
reviewed_patchsets: &HashMap<String, Vec<usize>>,
filepath: &str,
) -> io::Result<()> {
if let Some(parent) = Path::new(filepath).parent() {
Expand All @@ -508,7 +509,7 @@ pub fn save_reviewed_patchsets(
Ok(())
}

pub fn load_reviewed_patchsets(filepath: &str) -> io::Result<HashMap<String, Vec<u32>>> {
pub fn load_reviewed_patchsets(filepath: &str) -> io::Result<HashMap<String, Vec<usize>>> {
let reviewed_patchsets_file = File::open(filepath)?;
let reviewed_patchsets = serde_json::from_reader(reviewed_patchsets_file)?;
Ok(reviewed_patchsets)
Expand Down
4 changes: 2 additions & 2 deletions src/lore_session/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl PatchFeedRequest for FakeLoreAPIClient {
fn request_patch_feed(
&self,
target_list: &str,
min_index: u32,
min_index: usize,
) -> Result<String, FailedFeedRequest> {
let _ = min_index;
let _ = target_list;
Expand Down Expand Up @@ -305,7 +305,7 @@ fn should_process_available_lists() {
impl AvailableListsRequest for FakeLoreAPIClient {
fn request_available_lists(
&self,
min_index: u32,
min_index: usize,
) -> Result<String, FailedAvailableListsRequest> {
match min_index {
0 => Ok(fs::read_to_string("src/test_samples/lore_session/process_available_lists/available_lists_response-1.html").unwrap()),
Expand Down
Loading

0 comments on commit c2f380e

Please sign in to comment.