replace thiserror with snauf and poem with axum + utoipa
This commit is contained in:
parent
b4a0aadef9
commit
c067088a32
31 changed files with 223 additions and 317 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -313,7 +313,6 @@ dependencies = [
|
|||
"serde",
|
||||
"snafu",
|
||||
"tempfile",
|
||||
"thiserror 1.0.69",
|
||||
"time",
|
||||
]
|
||||
|
||||
|
|
|
@ -7,7 +7,6 @@ members = [
|
|||
[workspace.dependencies]
|
||||
serde = "1.0.219"
|
||||
snafu = { version = "0.8.6", features = ["rust_1_81"] }
|
||||
thiserror = "1.0.61"
|
||||
time = { version = "0.3.41", features = ["macros", "serde", "formatting", "parsing" ] }
|
||||
|
||||
[workspace.package]
|
||||
|
|
|
@ -14,5 +14,4 @@ rusqlite = { version = "0.36.0", features = ["bundled", "time"] }
|
|||
serde = { workspace = true }
|
||||
snafu = { workspace = true }
|
||||
tempfile = "3.20.0"
|
||||
thiserror = { workspace = true }
|
||||
time = { workspace = true }
|
||||
|
|
|
@ -7,13 +7,13 @@ use std::{
|
|||
|
||||
use r2d2::Pool;
|
||||
use r2d2_sqlite::SqliteConnectionManager;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tempfile::{NamedTempFile, PersistError};
|
||||
|
||||
use crate::{
|
||||
data::{self, author::Author, book::Book, pagination::SortOrder, series::Series},
|
||||
search::search,
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Top level calibre functions, bundling all sub functions in one place and providing secure access to
|
||||
/// the database.
|
||||
|
@ -26,7 +26,7 @@ pub struct Calibre {
|
|||
#[derive(Debug, Snafu)]
|
||||
pub enum LoadError {
|
||||
#[snafu(display("Failed to create database connection pool."))]
|
||||
DbPool { source: r2d2::Error },
|
||||
CreateDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to create temporary database view."))]
|
||||
TmpDb { source: io::Error },
|
||||
#[snafu(display("Failed to keep temporary database from deletion."))]
|
||||
|
@ -34,171 +34,75 @@ pub enum LoadError {
|
|||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
#[snafu(display("Failed to search."))]
|
||||
pub struct SearchError {
|
||||
source: crate::search::SearchError,
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum BooksError {
|
||||
pub enum DataStoreError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
BooksDbPool { source: r2d2::Error },
|
||||
GetDbConn { source: r2d2::Error },
|
||||
|
||||
#[snafu(display("Failed to search."))]
|
||||
Search { source: crate::search::SearchError },
|
||||
|
||||
#[snafu(display("Failed to fetch multiple books."))]
|
||||
FetchBooks {
|
||||
MultipleBooks {
|
||||
source: data::book::MultipleBooksError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum AuthorsError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
AuthorsDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch multiple authors."))]
|
||||
FetchAuthors {
|
||||
source: data::author::MultipleAuthorsError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum AuthorBooksError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
AuthorBooksDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch multiple an author's books."))]
|
||||
FetchAuthorBooks {
|
||||
source: data::book::AuthorBooksError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum RecentBooksError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
RecentBooksDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch recent books."))]
|
||||
FetchRecentBooks {
|
||||
RecentBooks {
|
||||
source: data::book::RecentBooksError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum ScalarBookError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
ScalarBookDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch book."))]
|
||||
FetchScalarBook { source: data::book::ScalarBookError },
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum ScalarAuthorError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
ScalarAuthorDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch author."))]
|
||||
FetchScalarAuthor {
|
||||
source: data::author::ScalarAuthorError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum ScalarSeriesError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
ScalarSeriesDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch series."))]
|
||||
FetchScalarSeries {
|
||||
source: data::series::ScalarSeriesError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum BookAuthorError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
BookAuthorDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch book's author."))]
|
||||
FetchBookAuthor {
|
||||
source: data::author::BookAuthorError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum MultipleSeriesError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
MultipleSeriesDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch multiple series."))]
|
||||
FetchMultipleSeries {
|
||||
source: data::series::MultiplSeriesError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum BookSeriesError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
BookSeriesDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to get the series a book belongs to."))]
|
||||
FetchBookSeries {
|
||||
source: data::series::SeriesBooksError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum SeriesBooksError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
SeriesBooksDbPool { source: r2d2::Error },
|
||||
ScalarBook { source: data::book::ScalarBookError },
|
||||
#[snafu(display("Failed to get a series' books."))]
|
||||
FetchSeriesBooks { source: data::book::SeriesBookError },
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasPreviousAuthorsError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasPreviousAuthorsDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to check if there are previous authors."))]
|
||||
FetchHasPreviousAuthors {
|
||||
source: data::author::PreviousAuthorsError,
|
||||
SeriesBooks { source: data::book::SeriesBookError },
|
||||
#[snafu(display("Failed to fetch an author's books."))]
|
||||
AuthorBooks {
|
||||
source: data::book::AuthorBooksError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasMoreAuthorsError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasMoreAuthorsDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to check if there are previous authors."))]
|
||||
FetchHasMoreAuthors {
|
||||
source: data::author::MoreAuthorsError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasPreviousBooksError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasPreviousBooksDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to check if there are previous books."))]
|
||||
FetchHasPreviousBooks {
|
||||
HasPreviousBooks {
|
||||
source: data::book::PreviousBooksError,
|
||||
},
|
||||
}
|
||||
#[snafu(display("Failed to check if there are more books."))]
|
||||
HasMoreBooks { source: data::book::MoreBooksError },
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasMoreBooksError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasMoreBooksDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to check if there are previous books."))]
|
||||
FetchHasMoreBooks { source: data::book::MoreBooksError },
|
||||
}
|
||||
#[snafu(display("Failed to fetch multiple authors."))]
|
||||
MultipleAuthors {
|
||||
source: data::author::MultipleAuthorsError,
|
||||
},
|
||||
#[snafu(display("Failed to fetch author."))]
|
||||
ScalarAuthor {
|
||||
source: data::author::ScalarAuthorError,
|
||||
},
|
||||
#[snafu(display("Failed to fetch book's author."))]
|
||||
BookAuthor {
|
||||
source: data::author::BookAuthorError,
|
||||
},
|
||||
#[snafu(display("Failed to check if there are previous authors."))]
|
||||
HasPreviousAuthors {
|
||||
source: data::author::PreviousAuthorsError,
|
||||
},
|
||||
#[snafu(display("Failed to check if there are more authors."))]
|
||||
HasMoreAuthors {
|
||||
source: data::author::MoreAuthorsError,
|
||||
},
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasPreviousSeriesError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasPreviousSeriesDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to fetch multiple series."))]
|
||||
MultipleSeries {
|
||||
source: data::series::MultiplSeriesError,
|
||||
},
|
||||
#[snafu(display("Failed to fetch series."))]
|
||||
ScalarSeries {
|
||||
source: data::series::ScalarSeriesError,
|
||||
},
|
||||
#[snafu(display("Failed to get the series a book belongs to."))]
|
||||
BookSeries {
|
||||
source: data::series::SeriesBooksError,
|
||||
},
|
||||
#[snafu(display("Failed to check if there are previous series."))]
|
||||
FetchHasPreviousSeries {
|
||||
HasPreviousSeries {
|
||||
source: data::series::PreviousSeriesError,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum HasMoreSeriesError {
|
||||
#[snafu(display("Failed to get database connection from pool."))]
|
||||
HasMoreSeriesDbPool { source: r2d2::Error },
|
||||
#[snafu(display("Failed to check if there are previous series."))]
|
||||
FetchHasMoreSeries {
|
||||
#[snafu(display("Failed to check if there are more series."))]
|
||||
HasMoreSeries {
|
||||
source: data::series::MoreSeriesError,
|
||||
},
|
||||
}
|
||||
|
@ -208,7 +112,7 @@ impl Calibre {
|
|||
/// Fail if the database file can not be opened or not be found.
|
||||
pub fn load(path: &Path) -> Result<Self, LoadError> {
|
||||
let manager = SqliteConnectionManager::file(path);
|
||||
let pool = r2d2::Pool::new(manager).context(DbPoolSnafu)?;
|
||||
let pool = r2d2::Pool::new(manager).context(CreateDbPoolSnafu)?;
|
||||
|
||||
let tmpfile = NamedTempFile::new().context(TmpDbSnafu)?;
|
||||
let (_, search_db_path) = tmpfile.keep().context(PersistTmpDbSnafu)?;
|
||||
|
@ -222,7 +126,7 @@ impl Calibre {
|
|||
/// Full text search with a query.
|
||||
///
|
||||
/// See https://www.sqlite.org/fts5.html#full_text_query_syntax for syntax.
|
||||
pub fn search(&self, query: &str) -> Result<Vec<Book>, SearchError> {
|
||||
pub fn search(&self, query: &str) -> Result<Vec<Book>, DataStoreError> {
|
||||
search(query, &self.pool, &self.search_db_path).context(SearchSnafu)
|
||||
}
|
||||
|
||||
|
@ -233,9 +137,9 @@ impl Calibre {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Book>, BooksError> {
|
||||
let conn = self.pool.get().context(BooksDbPoolSnafu)?;
|
||||
Book::multiple(&conn, limit, cursor, sort_order).context(FetchBooksSnafu)
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::multiple(&conn, limit, cursor, sort_order).context(MultipleBooksSnafu)
|
||||
}
|
||||
|
||||
/// Fetch author data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
||||
|
@ -245,9 +149,9 @@ impl Calibre {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Author>, AuthorsError> {
|
||||
let conn = self.pool.get().context(AuthorsDbPoolSnafu)?;
|
||||
Author::multiple(&conn, limit, cursor, sort_order).context(FetchAuthorsSnafu)
|
||||
) -> Result<Vec<Author>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Author::multiple(&conn, limit, cursor, sort_order).context(MultipleAuthorsSnafu)
|
||||
}
|
||||
|
||||
/// Fetch books for an author specified by `author_id`, paginate the books by starting at `cursor`,
|
||||
|
@ -258,28 +162,27 @@ impl Calibre {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: SortOrder,
|
||||
) -> Result<Vec<Book>, AuthorBooksError> {
|
||||
let conn = self.pool.get().context(AuthorBooksDbPoolSnafu)?;
|
||||
Book::author_books(&conn, author_id, limit, cursor, sort_order)
|
||||
.context(FetchAuthorBooksSnafu)
|
||||
) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::author_books(&conn, author_id, limit, cursor, sort_order).context(AuthorBooksSnafu)
|
||||
}
|
||||
|
||||
/// Get recent books up to a limit of `limit`.
|
||||
pub fn recent_books(&self, limit: u64) -> Result<Vec<Book>, RecentBooksError> {
|
||||
let conn = self.pool.get().context(RecentBooksDbPoolSnafu)?;
|
||||
Book::recents(&conn, limit).context(FetchRecentBooksSnafu)
|
||||
pub fn recent_books(&self, limit: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::recents(&conn, limit).context(RecentBooksSnafu)
|
||||
}
|
||||
|
||||
/// Get a single book, specified `id`.
|
||||
pub fn scalar_book(&self, id: u64) -> Result<Book, ScalarBookError> {
|
||||
let conn = self.pool.get().context(ScalarBookDbPoolSnafu)?;
|
||||
Book::scalar_book(&conn, id).context(FetchScalarBookSnafu)
|
||||
pub fn scalar_book(&self, id: u64) -> Result<Book, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::scalar_book(&conn, id).context(ScalarBookSnafu)
|
||||
}
|
||||
|
||||
/// Get the author to a book with id `id`.
|
||||
pub fn book_author(&self, id: u64) -> Result<Author, BookAuthorError> {
|
||||
let conn = self.pool.get().context(BookAuthorDbPoolSnafu)?;
|
||||
Author::book_author(&conn, id).context(FetchBookAuthorSnafu)
|
||||
pub fn book_author(&self, id: u64) -> Result<Author, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Author::book_author(&conn, id).context(BookAuthorSnafu)
|
||||
}
|
||||
|
||||
/// Fetch series data from calibre, starting at `cursor`, fetching up to an amount of `limit` and
|
||||
|
@ -289,69 +192,69 @@ impl Calibre {
|
|||
limit: u64,
|
||||
cursor: Option<&str>,
|
||||
sort_order: &SortOrder,
|
||||
) -> Result<Vec<Series>, MultipleSeriesError> {
|
||||
let conn = self.pool.get().context(MultipleSeriesDbPoolSnafu)?;
|
||||
Series::multiple(&conn, limit, cursor, sort_order).context(FetchMultipleSeriesSnafu)
|
||||
) -> Result<Vec<Series>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Series::multiple(&conn, limit, cursor, sort_order).context(MultipleSeriesSnafu)
|
||||
}
|
||||
|
||||
/// Get the series a book with id `id` is in, as well as the book's position within the series.
|
||||
pub fn book_series(&self, id: u64) -> Result<Option<(Series, f64)>, BookSeriesError> {
|
||||
let conn = self.pool.get().context(BookSeriesDbPoolSnafu)?;
|
||||
Series::book_series(&conn, id).context(FetchBookSeriesSnafu)
|
||||
pub fn book_series(&self, id: u64) -> Result<Option<(Series, f64)>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Series::book_series(&conn, id).context(BookSeriesSnafu)
|
||||
}
|
||||
|
||||
/// Get all books belonging to the series with id `id`.
|
||||
pub fn series_books(&self, id: u64) -> Result<Vec<Book>, SeriesBooksError> {
|
||||
let conn = self.pool.get().context(SeriesBooksDbPoolSnafu)?;
|
||||
Book::series_books(&conn, id).context(FetchSeriesBooksSnafu)
|
||||
pub fn series_books(&self, id: u64) -> Result<Vec<Book>, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::series_books(&conn, id).context(SeriesBooksSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more authors before the specified cursor.
|
||||
pub fn has_previous_authors(&self, author_sort: &str) -> Result<bool, HasPreviousAuthorsError> {
|
||||
let conn = self.pool.get().context(HasPreviousAuthorsDbPoolSnafu)?;
|
||||
Author::has_previous_authors(&conn, author_sort).context(FetchHasPreviousAuthorsSnafu)
|
||||
pub fn has_previous_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Author::has_previous_authors(&conn, author_sort).context(HasPreviousAuthorsSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more authors after the specified cursor.
|
||||
pub fn has_more_authors(&self, author_sort: &str) -> Result<bool, HasMoreAuthorsError> {
|
||||
let conn = self.pool.get().context(HasMoreAuthorsDbPoolSnafu)?;
|
||||
Author::has_more_authors(&conn, author_sort).context(FetchHasMoreAuthorsSnafu)
|
||||
pub fn has_more_authors(&self, author_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Author::has_more_authors(&conn, author_sort).context(HasMoreAuthorsSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more books before the specified cursor.
|
||||
pub fn has_previous_books(&self, book_sort: &str) -> Result<bool, HasPreviousBooksError> {
|
||||
let conn = self.pool.get().context(HasPreviousBooksDbPoolSnafu)?;
|
||||
Book::has_previous_books(&conn, book_sort).context(FetchHasPreviousBooksSnafu)
|
||||
pub fn has_previous_books(&self, book_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::has_previous_books(&conn, book_sort).context(HasPreviousBooksSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more books after the specified cursor.
|
||||
pub fn has_more_books(&self, book_sort: &str) -> Result<bool, HasMoreBooksError> {
|
||||
let conn = self.pool.get().context(HasMoreBooksDbPoolSnafu)?;
|
||||
Book::has_more_books(&conn, book_sort).context(FetchHasMoreBooksSnafu)
|
||||
pub fn has_more_books(&self, book_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Book::has_more_books(&conn, book_sort).context(HasMoreBooksSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more series before the specified cursor.
|
||||
pub fn has_previous_series(&self, series_sort: &str) -> Result<bool, HasPreviousSeriesError> {
|
||||
let conn = self.pool.get().context(HasPreviousSeriesDbPoolSnafu)?;
|
||||
Series::has_previous_series(&conn, series_sort).context(FetchHasPreviousSeriesSnafu)
|
||||
pub fn has_previous_series(&self, series_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Series::has_previous_series(&conn, series_sort).context(HasPreviousSeriesSnafu)
|
||||
}
|
||||
|
||||
/// Check if there are more series after the specified cursor.
|
||||
pub fn has_more_series(&self, series_sort: &str) -> Result<bool, HasMoreSeriesError> {
|
||||
let conn = self.pool.get().context(HasMoreSeriesDbPoolSnafu)?;
|
||||
Series::has_more_series(&conn, series_sort).context(FetchHasMoreSeriesSnafu)
|
||||
pub fn has_more_series(&self, series_sort: &str) -> Result<bool, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Series::has_more_series(&conn, series_sort).context(HasMoreSeriesSnafu)
|
||||
}
|
||||
|
||||
/// Fetch a single author with id `id`.
|
||||
pub fn scalar_author(&self, id: u64) -> Result<Author, ScalarAuthorError> {
|
||||
let conn = self.pool.get().context(ScalarAuthorDbPoolSnafu)?;
|
||||
Author::scalar_author(&conn, id).context(FetchScalarAuthorSnafu)
|
||||
pub fn scalar_author(&self, id: u64) -> Result<Author, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Author::scalar_author(&conn, id).context(ScalarAuthorSnafu)
|
||||
}
|
||||
|
||||
/// Fetch a single series with id `id`.
|
||||
pub fn scalar_series(&self, id: u64) -> Result<Series, ScalarSeriesError> {
|
||||
let conn = self.pool.get().context(ScalarSeriesDbPoolSnafu)?;
|
||||
Series::scalar_series(&conn, id).context(FetchScalarSeriesSnafu)
|
||||
pub fn scalar_series(&self, id: u64) -> Result<Series, DataStoreError> {
|
||||
let conn = self.pool.get().context(GetDbConnSnafu)?;
|
||||
Series::scalar_series(&conn, id).context(ScalarSeriesSnafu)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
5
calibre-db/src/data.rs
Normal file
5
calibre-db/src/data.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
pub mod author;
|
||||
pub mod book;
|
||||
pub mod error;
|
||||
pub mod pagination;
|
||||
pub mod series;
|
|
@ -1,10 +1,11 @@
|
|||
//! Author data.
|
||||
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
use rusqlite::{Connection, Row, named_params};
|
||||
use serde::Serialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
|
||||
/// Author in calibre.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Author {
|
||||
|
|
|
@ -2,10 +2,10 @@
|
|||
|
||||
use rusqlite::{Connection, Row, named_params};
|
||||
use serde::Serialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Book in calibre.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
|
|
@ -2,9 +2,9 @@
|
|||
|
||||
use rusqlite::{Connection, Row, named_params};
|
||||
use serde::Serialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use super::pagination::{HasPrevOrMoreError, Pagination, PaginationError, SortOrder};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Series in calibre.
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
|
|
|
@ -1,13 +1,6 @@
|
|||
//! Read data from a calibre library, leveraging its SQLite metadata database.
|
||||
|
||||
pub mod calibre;
|
||||
pub mod search;
|
||||
|
||||
/// Data structs for the calibre database.
|
||||
pub mod data {
|
||||
pub mod author;
|
||||
pub mod book;
|
||||
pub mod error;
|
||||
pub mod pagination;
|
||||
pub mod series;
|
||||
}
|
||||
pub mod data;
|
||||
pub mod search;
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::{
|
|||
};
|
||||
|
||||
use ignore::Walk;
|
||||
use zip::{write::SimpleFileOptions, CompressionMethod};
|
||||
use zip::{CompressionMethod, write::SimpleFileOptions};
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let out_dir = env::var("OUT_DIR")?;
|
||||
|
|
|
@ -1,21 +1,20 @@
|
|||
use std::path::Path;
|
||||
|
||||
use calibre_db::{
|
||||
calibre::{AuthorBooksError, Calibre, ScalarAuthorError},
|
||||
calibre::{Calibre, DataStoreError},
|
||||
data::author::Author,
|
||||
};
|
||||
|
||||
use crate::data::book::Book;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use super::SortOrder;
|
||||
use crate::data::book::Book;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum SingleAuthorError {
|
||||
#[snafu(display("Failed to fetch author data."))]
|
||||
AuthorData { source: ScalarAuthorError },
|
||||
AuthorData { source: DataStoreError },
|
||||
#[snafu(display("Failed to fetch books from author."))]
|
||||
BookData { source: AuthorBooksError },
|
||||
BookData { source: DataStoreError },
|
||||
}
|
||||
|
||||
pub async fn single(
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use calibre_db::calibre::{Calibre, RecentBooksError};
|
||||
use calibre_db::calibre::{Calibre, DataStoreError};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use crate::data::book::Book;
|
||||
|
@ -8,7 +8,7 @@ use crate::data::book::Book;
|
|||
#[derive(Debug, Snafu)]
|
||||
pub enum RecentBooksError {
|
||||
#[snafu(display("Failed to fetch recent books."))]
|
||||
RecentBooks { source: RecentBooksError },
|
||||
RecentBooks { source: DataStoreError },
|
||||
}
|
||||
|
||||
pub async fn recent(calibre: &Calibre, library_path: &Path) -> Result<Vec<Book>, RecentBooksError> {
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tera::Context;
|
||||
|
||||
use crate::{
|
||||
api::{
|
||||
SortOrder, TAG,
|
||||
|
@ -11,13 +19,6 @@ use crate::{
|
|||
http_error,
|
||||
templates::TEMPLATES,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tera::Context;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum RetrieveError {
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::{io, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Response,
|
||||
};
|
||||
use calibre_db::calibre::DataStoreError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tokio::fs::File;
|
||||
|
||||
use crate::{
|
||||
api::{
|
||||
SortOrder, TAG, download,
|
||||
|
@ -11,14 +20,6 @@ use crate::{
|
|||
http_error,
|
||||
opds::media_type::MediaType,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Response,
|
||||
};
|
||||
use calibre_db::calibre::ScalarBookError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tokio::fs::File;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum RetrieveError {
|
||||
|
@ -102,7 +103,7 @@ async fn books(
|
|||
#[derive(Debug, Snafu)]
|
||||
pub enum DownloadError {
|
||||
#[snafu(display("Failed to fetch book data."))]
|
||||
BookData { source: ScalarBookError },
|
||||
BookData { source: DataStoreError },
|
||||
#[snafu(display("No such book."))]
|
||||
NotFound,
|
||||
#[snafu(display("No such book."))]
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
use std::{fs::File, io, path::Path as FilePath, sync::Arc};
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::Response,
|
||||
};
|
||||
use calibre_db::calibre::{Calibre, ScalarBookError};
|
||||
use calibre_db::calibre::{Calibre, DataStoreError};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use std::{fs::File, io, path::Path as FilePath, sync::Arc};
|
||||
use tokio::fs::File as AsyncFile;
|
||||
|
||||
use crate::{
|
||||
|
@ -89,7 +90,7 @@ pub async fn full(
|
|||
#[derive(Debug, Snafu)]
|
||||
pub enum CoverError {
|
||||
#[snafu(display("Failed to fetch book data."))]
|
||||
BookData { source: ScalarBookError },
|
||||
BookData { source: DataStoreError },
|
||||
#[snafu(display("No such cover"))]
|
||||
NotFound { source: CoverFetchError },
|
||||
#[snafu(display("Failed to fetch cover thumbnail."))]
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tera::Context;
|
||||
|
||||
use crate::{
|
||||
api::{
|
||||
SortOrder, TAG,
|
||||
|
@ -11,13 +19,6 @@ use crate::{
|
|||
http_error,
|
||||
templates::TEMPLATES,
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::StatusCode,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tera::Context;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum RetrieveError {
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::{calibre::DataStoreError, data::author::Author as DbAuthor};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{
|
||||
APP_NAME,
|
||||
api::{
|
||||
|
@ -14,19 +23,11 @@ use crate::{
|
|||
relation::Relation,
|
||||
},
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::{calibre::AuthorsError, data::author::Author as DbAuthor};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum AuthorsError {
|
||||
#[snafu(display("Failed to fetch author data."))]
|
||||
Data { source: AuthorsError },
|
||||
Data { source: DataStoreError },
|
||||
#[snafu(display("Failed to render author data."))]
|
||||
Render { source: AsXmlError },
|
||||
}
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::calibre::DataStoreError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{
|
||||
APP_NAME,
|
||||
api::{
|
||||
|
@ -14,19 +23,11 @@ use crate::{
|
|||
relation::Relation,
|
||||
},
|
||||
};
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::calibre::BooksError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum OdpsBooksError {
|
||||
#[snafu(display("Failed to fetch book data."))]
|
||||
Data { source: BooksError },
|
||||
Data { source: DataStoreError },
|
||||
#[snafu(display("Failed to render book data."))]
|
||||
Render { source: RenderError },
|
||||
}
|
||||
|
|
|
@ -1,5 +1,13 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{
|
||||
APP_NAME,
|
||||
api::{
|
||||
|
@ -14,13 +22,6 @@ use crate::{
|
|||
relation::Relation,
|
||||
},
|
||||
};
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum RecentError {
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use super::books::{RenderError, render_books};
|
||||
use crate::{
|
||||
APP_NAME,
|
||||
api::{
|
||||
|
@ -14,15 +23,6 @@ use crate::{
|
|||
search::{OpenSearchDescription, Url},
|
||||
},
|
||||
};
|
||||
use axum::{
|
||||
extract::{Query, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use super::books::{RenderError, render_books};
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum SearchError {
|
||||
|
|
|
@ -1,5 +1,14 @@
|
|||
use std::sync::Arc;
|
||||
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::calibre::DataStoreError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{
|
||||
APP_NAME,
|
||||
api::{
|
||||
|
@ -14,19 +23,11 @@ use crate::{
|
|||
relation::Relation,
|
||||
},
|
||||
};
|
||||
use axum::{
|
||||
extract::{Path, State},
|
||||
http::{StatusCode, header},
|
||||
response::{IntoResponse, Response},
|
||||
};
|
||||
use calibre_db::calibre::MultipleSeriesError;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use time::OffsetDateTime;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum SeriesError {
|
||||
#[snafu(display("Failed to fetch series data."))]
|
||||
Data { source: MultipleSeriesError },
|
||||
Data { source: DataStoreError },
|
||||
#[snafu(display("Failed to render series data."))]
|
||||
Render { source: AsXmlError },
|
||||
}
|
||||
|
|
|
@ -1,13 +1,15 @@
|
|||
//! Deal with cursor pagination.
|
||||
|
||||
use crate::templates::TEMPLATES;
|
||||
use std::fmt::Debug;
|
||||
|
||||
use axum::response::{Html, IntoResponse, Response};
|
||||
use calibre_db::data::error::DataStoreError;
|
||||
use calibre_db::calibre::DataStoreError;
|
||||
use serde::Serialize;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use std::fmt::Debug;
|
||||
use tera::Context;
|
||||
|
||||
use crate::templates::TEMPLATES;
|
||||
|
||||
#[derive(Debug, Snafu)]
|
||||
pub enum PaginationError {
|
||||
#[snafu(display("Failed to fetch pagination data."))]
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use std::path::Path;
|
||||
|
||||
use calibre_db::{calibre::Calibre, data::error::DataStoreError};
|
||||
|
||||
use calibre_db::calibre::{Calibre, DataStoreError};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
use crate::data::book::Book;
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
use std::path::Path;
|
||||
|
||||
use calibre_db::{
|
||||
calibre::Calibre,
|
||||
data::{error::DataStoreError, series::Series},
|
||||
calibre::{Calibre, DataStoreError},
|
||||
data::series::Series,
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
use std::{
|
||||
fmt,
|
||||
fmt::Write,
|
||||
fs::{self, File},
|
||||
io,
|
||||
path::{Path, PathBuf},
|
||||
|
@ -12,7 +13,6 @@ use sha2::{
|
|||
digest::{generic_array::GenericArray, typenum::U32},
|
||||
};
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use std::fmt::Write;
|
||||
use tracing::debug;
|
||||
|
||||
/// Errors from converting a hash to its string representation.
|
||||
|
|
|
@ -2,15 +2,14 @@
|
|||
|
||||
use std::{
|
||||
env, fs, io,
|
||||
net::SocketAddr,
|
||||
net::ToSocketAddrs,
|
||||
net::{SocketAddr, ToSocketAddrs},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use snafu::{ResultExt, Snafu};
|
||||
use tracing::info;
|
||||
|
||||
use crate::cli::Cli;
|
||||
use snafu::{ResultExt, Snafu};
|
||||
|
||||
/// Errors from loading application configuration.
|
||||
#[derive(Debug, Snafu)]
|
||||
|
|
|
@ -33,6 +33,9 @@ mod tests {
|
|||
fn serialize() {
|
||||
let author = init();
|
||||
let xml = to_string(&author).unwrap();
|
||||
assert_eq!(xml, "<author><name>Rohal der Weise</name><uri>https://de.wiki-aventurica.de/wiki/Rohal_der_Weise</uri><email>rohal@aventurien.de</email></author>");
|
||||
assert_eq!(
|
||||
xml,
|
||||
"<author><name>Rohal der Weise</name><uri>https://de.wiki-aventurica.de/wiki/Rohal_der_Weise</uri><email>rohal@aventurien.de</email></author>"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,11 +4,10 @@ use calibre_db::data::{author::Author as DbAuthor, series::Series};
|
|||
use serde::Serialize;
|
||||
use time::OffsetDateTime;
|
||||
|
||||
use crate::{data::book::Book, APP_NAME};
|
||||
|
||||
use super::{
|
||||
author::Author, content::Content, link::Link, media_type::MediaType, relation::Relation,
|
||||
};
|
||||
use crate::{APP_NAME, data::book::Book};
|
||||
|
||||
/// Fundamental piece of OPDS, holding information about entries (for example a book).
|
||||
#[derive(Debug, Serialize)]
|
||||
|
@ -126,9 +125,8 @@ mod tests {
|
|||
use quick_xml::se::to_string;
|
||||
use time::macros::datetime;
|
||||
|
||||
use crate::opds::{content::Content, media_type::MediaType, relation::Relation};
|
||||
|
||||
use super::*;
|
||||
use crate::opds::{content::Content, media_type::MediaType, relation::Relation};
|
||||
|
||||
fn init() -> Entry {
|
||||
Entry {
|
||||
|
|
|
@ -2,9 +2,8 @@
|
|||
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::data::book::{Book, Format};
|
||||
|
||||
use super::{media_type::MediaType, relation::Relation};
|
||||
use crate::data::book::{Book, Format};
|
||||
|
||||
/// Link element in OPDS.
|
||||
#[derive(Debug, Serialize)]
|
||||
|
|
|
@ -8,11 +8,11 @@ use quick_xml::{
|
|||
se::to_string,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use snafu::ResultExt;
|
||||
|
||||
use super::error::{
|
||||
AsXmlError, BytesToUtf8Snafu, ReadXmlEventSnafu, ToStringSnafu, WriteXmlEventSnafu,
|
||||
};
|
||||
use snafu::ResultExt;
|
||||
|
||||
/// Url pointing to a location.
|
||||
#[derive(Debug, Serialize)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue