Skip to content

Commit

Permalink
Fix offset calculation for list items (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinisculeGirraffe authored Aug 8, 2022
1 parent 246169e commit e5e8978
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tdl"
description = "A command line tool for downloading files from the TIDAL API"
version = "0.2.4"
version = "0.2.5"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
20 changes: 11 additions & 9 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,37 @@ impl ApiClient {
&self,
url: &str,
opts: Option<Vec<(String, String)>>,
max: Option<u32>,
max: Option<usize>,
) -> Result<Vec<T>, Error>
where
T: DeserializeOwned + 'a,
{
let mut limit = 50;
let mut offset = 0;
let max = max.unwrap_or(u32::MAX);
let mut params = vec![
("limit".to_string(), limit.to_string()),
("offset".to_string(), offset.to_string()),
];
let max = max.unwrap_or(usize::MAX);
let mut params = vec![("limit".to_string(), limit.to_string())];
if let Some(opt) = opts {
params.extend(opt);
}
};

let mut result: Vec<T> = Vec::new();
'req: loop {
params.push(("offset".to_string(), offset.to_string()));
let json = self.get::<ItemResponse<T>>(url, Some(&params)).await?;
limit = json.limit;
// the minimum between the items in the response, and the total number of items requested
let item_limit = u32::min(json.total_number_of_items, max);
let item_limit = usize::min(json.total_number_of_items, max);
for item in json.items {
if result.len() as u32 >= item_limit {
if result.len() >= item_limit {
break 'req;
}
result.push(item);
}
offset += limit;
params.pop();
if offset >= json.total_number_of_items {
break 'req;
}
}
Ok(result)
}
Expand Down
2 changes: 1 addition & 1 deletion src/api/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl Default for DeviceAuthRequest {
pub struct ItemResponse<T> {
pub limit: usize,
pub offset: usize,
pub total_number_of_items: u32,
pub total_number_of_items: usize,
pub items: Vec<T>,
}

Expand Down
2 changes: 1 addition & 1 deletion src/api/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl SearchClient {
&self,
url: &str,
query: &str,
max: Option<u32>,
max: Option<usize>,
) -> Result<Table, Error>
where
T: DeserializeOwned + 'a + Tabled,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn consume_channel(channel: ReceiveChannel, concurrency: usize) {
async fn search(matches: &ArgMatches) {
let client = login().await;
if let Some(query) = matches.get_one::<String>("query") {
let max = matches.get_one::<u32>("max").cloned();
let max = matches.get_one::<usize>("max").cloned();
let result = match matches.get_one::<String>("filter") {
Some(filter) => match filter.as_str() {
"artist" => {
Expand Down

0 comments on commit e5e8978

Please sign in to comment.