-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split storage.rs into separate models with impl
- Loading branch information
Showing
6 changed files
with
131 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
/// Where the reader is with the book. | ||
/// Defaults to None. | ||
#[wasm_bindgen] | ||
#[derive(Copy, Clone, Deserialize, Serialize, Debug, PartialEq)] | ||
pub enum BookStatus { | ||
ToRead, | ||
Read, | ||
Liked, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use crate::utils::log; | ||
use anyhow::{bail, Result}; | ||
use serde::{Deserialize, Serialize}; | ||
use web_sys::Window; | ||
use super::book::Book; | ||
/// A list of book records. | ||
#[derive(Deserialize, Serialize, Debug)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Books { | ||
pub books: Vec<Book>, | ||
} | ||
|
||
impl Books { | ||
/// Returns a sorted array of all book records stored locally. | ||
/// Errors are logged. | ||
pub(crate) fn get(runtime: &Window) -> Result<Self> { | ||
// connect to the local storage | ||
let ls = match runtime.local_storage() { | ||
Ok(Some(v)) => v, | ||
Err(e) => { | ||
bail!("Failed to get local storage: {:?}", e); | ||
} | ||
_ => { | ||
bail!("Local storage not available (OK(None))"); | ||
} | ||
}; | ||
|
||
// get the total number of records | ||
let number_of_records = match ls.length() { | ||
Ok(v) => v, | ||
Err(e) => { | ||
bail!("Failed to get local storage length: {:?}", e); | ||
} | ||
}; | ||
|
||
// init the books array to the max possible size | ||
let mut books = Vec::with_capacity(number_of_records.try_into().unwrap_or_else(|_e| { | ||
log!("Failed to convert local storage length {number_of_records} to usize. It's a bug."); | ||
0 | ||
})); | ||
|
||
// get one key at a time (inefficient, but the best we have with Local Storage) | ||
for i in 0..number_of_records { | ||
// get the key by index | ||
let key = match ls.key(i) { | ||
Ok(Some(v)) => v, | ||
Ok(None) => { | ||
log!("Key {i} not found in local storage"); | ||
continue; | ||
} | ||
Err(e) => { | ||
log!("Failed to get key {i} from local storage: {:?}", e); | ||
continue; | ||
} | ||
}; | ||
|
||
// get value by key | ||
let book = match ls.get_item(&key) { | ||
Ok(Some(v)) => v, | ||
Ok(None) => { | ||
log!("Value not found in local storage: {key}"); | ||
continue; | ||
} | ||
Err(e) => { | ||
log!("Failed to get value from local storage for {key}: {:?}", e); | ||
continue; | ||
} | ||
}; | ||
|
||
// log!("{book}"); | ||
|
||
// parse the string value into a book record | ||
let book = match serde_json::from_str::<Book>(&book) { | ||
Ok(v) => v, | ||
Err(e) => { | ||
log!("Failed to parse local storage book record for {key}: {:?}", e); | ||
continue; | ||
} | ||
}; | ||
|
||
// log!("{:?}", book); | ||
|
||
// ignore books with no titles because it is likely to be a corrupted record | ||
// from the format change or a bug | ||
// the user will have no benefit from such records | ||
// TODO: compare the ISBN inside Book and the key - they may differ | ||
// TODO: delete these ignored records | ||
if book.volume_info.title.is_empty() { | ||
log!("Empty title for {key}"); | ||
continue; | ||
} | ||
|
||
books.push(book); | ||
} | ||
|
||
// the items in the local storage are randomly sorted | ||
// sort the list to make the latest scanned book come first | ||
books.sort_by(|a, b| b.timestamp.cmp(&a.timestamp)); | ||
|
||
Ok(Books { books }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod book; | ||
pub mod book_status; | ||
pub mod books; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters