cleanup naming in rust code

This commit is contained in:
Rob Watson 2020-07-03 12:39:31 +02:00
parent 91e0ee058c
commit 3ba5c60df0
1 changed files with 21 additions and 21 deletions

View File

@ -26,7 +26,7 @@ use serde_json::Value;
const DEFAULT_SIZE: usize = 25;
#[derive(Copy, Clone, Debug, Deserialize)]
enum Key {
enum SortKey {
Name,
Size,
Seeders,
@ -34,13 +34,13 @@ enum Key {
Date,
}
impl Default for Key {
impl Default for SortKey {
fn default() -> Self {
Self::Name
}
}
impl fmt::Display for Key {
impl fmt::Display for SortKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match &self {
Self::Name => "name",
@ -54,18 +54,18 @@ impl fmt::Display for Key {
}
#[derive(Copy, Clone, Debug, Deserialize)]
enum Direction {
enum SortDirection {
Asc,
Desc,
}
impl Default for Direction {
impl Default for SortDirection {
fn default() -> Self {
Self::Desc
}
}
impl fmt::Display for Direction {
impl fmt::Display for SortDirection {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = match &self {
Self::Asc => "asc",
@ -120,8 +120,8 @@ struct SearchQuery {
q: String,
page: Option<usize>,
size: Option<usize>,
sort_key: Option<Key>,
sort_dir: Option<Direction>,
sort_key: Option<SortKey>,
sort_dir: Option<SortDirection>,
type_: Option<String>,
}
@ -147,8 +147,8 @@ async fn search(
struct NewQuery {
page: Option<usize>,
size: Option<usize>,
sort_key: Option<Key>,
sort_dir: Option<Direction>,
sort_key: Option<SortKey>,
sort_dir: Option<SortDirection>,
type_: Option<String>,
}
@ -181,22 +181,22 @@ fn search_query(
}
let page = query.page.unwrap_or(1);
let key = query.sort_key.unwrap_or_default();
let direction = query.sort_dir.unwrap_or_default();
let sort_key = query.sort_key.unwrap_or_default();
let sort_dir = query.sort_dir.unwrap_or_default();
let size = cmp::min(100, query.size.unwrap_or(DEFAULT_SIZE));
let type_ = query.type_.as_ref().map_or("torrent", String::deref);
let offset = size * (page - 1);
println!(
"query = {}, type = {}, page = {}, size = {}, sort_key = {}, sort_dir = {}",
q, type_, page, size, key, direction
q, type_, page, size, sort_key, sort_dir
);
let res = if type_ == "file" {
let results = torrent_file_search(conn, q, size, offset)?;
serde_json::to_value(&results).unwrap()
} else {
let results = torrent_search(conn, q, key, direction, size, offset)?;
let results = torrent_search(conn, q, sort_key, sort_dir, size, offset)?;
serde_json::to_value(&results).unwrap()
};
@ -210,14 +210,14 @@ fn new_query(
let page = query.page.unwrap_or(1);
let size = cmp::min(100, query.size.unwrap_or(DEFAULT_SIZE));
let key = query.sort_key.unwrap_or_default();
let direction = query.sort_dir.unwrap_or_default();
let sort_key = query.sort_key.unwrap_or_default();
let sort_dir = query.sort_dir.unwrap_or_default();
let type_ = query.type_.as_ref().map_or("torrent", String::deref);
let offset = size * (page - 1);
dbg!(
println!(
"new, type = {}, page = {}, size = {}, sort_key = {}, sort_dir = {}",
type_, page, size, key, direction
type_, page, size, sort_key, sort_dir
);
let res = if type_ == "file" {
@ -246,8 +246,8 @@ struct Torrent {
fn torrent_search(
conn: r2d2::PooledConnection<SqliteConnectionManager>,
query: &str,
key: Key,
direction: Direction,
key: SortKey,
direction: SortDirection,
size: usize,
offset: usize,
) -> Result<Vec<Torrent>, Error> {
@ -410,7 +410,7 @@ mod tests {
let manager = SqliteConnectionManager::file(super::torrents_db_file());
let pool = r2d2::Pool::builder().max_size(15).build(manager).unwrap();
let conn = pool.get().unwrap();
let results = super::torrent_search(conn, "sherlock", 10, 0, SortKey::Name, SortDir::Desc);
let results = super::torrent_search(conn, "sherlock", 10, 0, SortKey::Name, SortDirection::Desc);
assert!(results.unwrap().len() > 2);
// println!("Query took {:?} seconds.", end - start);
}