Make get_collection_from_file async

This commit is contained in:
Reinout Meliesie 2024-12-22 21:02:39 +01:00
parent 183efaa047
commit f1751882be
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6
4 changed files with 83 additions and 53 deletions

View file

@ -4,10 +4,7 @@ mod persistence ;
mod ui ;
mod utility ;
use {
gtk4 :: { gio :: spawn_blocking , glib :: * } ,
libadwaita :: { * , prelude :: * } ,
} ;
use { gtk4 :: glib :: * , libadwaita :: { * , prelude :: * } } ;
use crate :: { error :: { * , ZoodexError :: * } , persistence :: * , ui :: * } ;
@ -28,10 +25,8 @@ fn on_activate ( app : & Application ) {
spawn_future_local ( async move {
async_unit_result_context (
async {
let collection = spawn_blocking ( ||
get_collection_from_file () ) . await ? ? ;
let collection = get_collection_from_file () . await ? ;
ui . render_collection (collection) ;
Ok (())
} ,
|error| {

View file

@ -1,6 +1,9 @@
use {
async_rusqlite :: {
* ,
rusqlite :: { Error , OpenFlags , Error :: * , ffi :: ErrorCode :: * } ,
} ,
fallible_iterator :: * ,
rusqlite :: { Connection , Error , Error :: * , ffi :: ErrorCode :: * } ,
std :: path :: * ,
} ;
@ -8,45 +11,50 @@ use crate :: { collection :: * , error :: { * , ZoodexError :: * } } ;
pub fn get_collection_from_file () -> Result <Collection> {
let sqlite_connection = Connection :: open ("zoodex.sqlite") ? ;
pub async fn get_collection_from_file () -> Result <Collection> {
let sqlite_connection = Connection :: builder () . open_with_flags (
"zoodex.sqlite" ,
OpenFlags :: SQLITE_OPEN_READ_ONLY | OpenFlags :: SQLITE_OPEN_NO_MUTEX ,
) . await ? ;
let mut films_query = sqlite_connection
. prepare ("select * from films order by release_date desc") ? ;
let films = films_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let release_date = row . get (3) ? ;
let runtime_minutes = row . get (4) ? ;
let poster_file_path = row . get :: < _ , Option <String> > (5) ?
. map ( PathBuf :: from ) ;
let video_file_path = row . get :: < _ , Option <String> > (6) ?
. map ( PathBuf :: from ) ;
sqlite_connection . call ( |sqlite_connection| {
let mut films_query = sqlite_connection
. prepare ("select * from films order by release_date desc") ? ;
let films = films_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let release_date = row . get (3) ? ;
let runtime_minutes = row . get (4) ? ;
let poster_file_path = row . get :: < _ , Option <String> > (5) ?
. map ( PathBuf :: from ) ;
let video_file_path = row . get :: < _ , Option <String> > (6) ?
. map ( PathBuf :: from ) ;
Ok ( Film {
uuid ,
name ,
original_name ,
release_date ,
runtime_minutes ,
poster_file_path ,
video_file_path ,
} )
} ) . collect () ? ;
Ok ( Film {
uuid ,
name ,
original_name ,
release_date ,
runtime_minutes ,
poster_file_path ,
video_file_path ,
} )
} ) . collect () ? ;
let mut series_query = sqlite_connection . prepare ("select * from series") ? ;
let series = series_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let poster_file_path = row . get :: < _ , Option <String> > (3) ?
. map ( PathBuf :: from ) ;
let mut series_query = sqlite_connection . prepare ("select * from series") ? ;
let series = series_query . query (()) ? . map ( |row| {
let uuid = row . get (0) ? ;
let name = row . get (1) ? ;
let original_name = row . get (2) ? ;
let poster_file_path = row . get :: < _ , Option <String> > (3) ?
. map ( PathBuf :: from ) ;
Ok ( Series { uuid , name , original_name , poster_file_path } )
} ) . collect () ? ;
Ok ( Series { uuid , name , original_name , poster_file_path } )
} ) . collect () ? ;
Ok ( Collection { films , series } )
Ok ( Collection { films , series } )
} ) . await
}
impl From <Error> for ZoodexError {
@ -62,3 +70,7 @@ impl From <Error> for ZoodexError {
}
}
}
impl From <AlreadyClosed> for ZoodexError {
fn from ( _ : AlreadyClosed ) -> Self { CollectionFileError }
}