Skip to content

Commit 3aaf079

Browse files
docs(filtersCache): add documentation
1 parent c6f1d85 commit 3aaf079

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/backend/manga_provider/filters.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
//! This module provides the `FiltersCache` struct, which is responsible for caching and retrieving filter data used in manga search
2+
//! operations.
3+
//!
4+
//! The `FiltersCache` allows you to serialize filter configurations (such as languages, publication status, sort order, tags,
5+
//! authors, and more) into TOML files for persistent storage, and deserialize them back when needed. This is useful for persisting
6+
//! user-selected filters or default filter sets between application runs.
7+
//!
8+
//! The cache is stored in a specified directory and file, and the module provides methods to write filter data to the cache and
9+
//! read it back. The filter data must implement `serde::Serialize` and `serde::de::DeserializeOwned`, making it flexible for
10+
//! various filter types.
11+
//!
12+
//! Example use cases include caching search filters for manga providers like MangaDex, where filters may include fields such as
13+
//! languages, publication status, sort order, tags, magazine demographics, authors, and artists.
114
use std::error::Error;
215
use std::fs::{File, create_dir_all};
316
use std::io::{Read, Write};
@@ -6,6 +19,40 @@ use std::path::PathBuf;
619
use serde::Serialize;
720
use serde::de::DeserializeOwned;
821

22+
/// A cache handler for serializing and deserializing filter data to and from TOML files.
23+
///
24+
/// `FiltersCache` is designed to persist filter configurations used in manga search operations, such as those for MangaDex.
25+
/// It stores filter data (implementing `serde::Serialize` and `serde::de::DeserializeOwned`) in a specified directory and file.
26+
///
27+
/// # Example Usage
28+
///
29+
/// The struct is typically used to cache filters like the following (see tests for more details):
30+
///
31+
/// ```rust
32+
/// # use crate::backend::manga_provider::mangadex::filters::api_parameter::{Filters, ContentRating, PublicationStatus, SortBy, Tags, TagData, TagSelection, MagazineDemographic, User, AuthorFilterState};
33+
/// # use crate::backend::manga_provider::Languages;
34+
/// let filters = Filters {
35+
/// content_rating: vec![ContentRating::Suggestive, ContentRating::Erotic],
36+
/// publication_status: vec![PublicationStatus::Completed, PublicationStatus::Ongoing],
37+
/// sort_by: SortBy::HighestRating,
38+
/// tags: Tags::new(vec![TagData::new("id_tag".to_string(), TagSelection::Included, "fantasy".to_string())]),
39+
/// magazine_demographic: vec![MagazineDemographic::Shoujo, MagazineDemographic::Seinen],
40+
/// authors: User::new(vec![AuthorFilterState::new("user_id".to_string(), "".to_string())]),
41+
/// artists: User::default(),
42+
/// languages: vec![Languages::English, Languages::Spanish],
43+
/// };
44+
/// ```
45+
///
46+
/// You can then write these filters to a cache file and retrieve them later:
47+
///
48+
/// ```rust
49+
/// # use std::path::Path;
50+
/// # let filters_cache = FiltersCache::new(Path::new("./cache_dir"), "filters.toml");
51+
/// filters_cache.write_to_cache(&filters).unwrap();
52+
/// let cached: Option<Filters> = filters_cache.get_cached_filters();
53+
/// ```
54+
///
55+
/// This enables persistent storage and retrieval of user or default filter sets between application runs.
956
pub struct FiltersCache {
1057
base_directory: PathBuf,
1158
cache_filename: &'static str,

src/backend/manga_provider/mangadex/filters/filter_provider.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,17 @@ impl MangadexFilterProvider {
717717
fn save_filters_on_close(&self) {
718718
let filters_cache_writer = FiltersCache::new(&*MANGADEX_CACHE_BASE_DIRECTORY, MANGADEX_CACHE_FILENAME);
719719

720-
filters_cache_writer.write_to_cache(&self.filters).ok();
720+
filters_cache_writer
721+
.write_to_cache(&self.filters)
722+
.inspect_err(|e| {
723+
#[cfg(not(test))]
724+
{
725+
use crate::backend::error_log::{ErrorType, write_to_error_log};
726+
727+
write_to_error_log(ErrorType::String(&e.to_string()));
728+
}
729+
})
730+
.ok();
721731
}
722732

723733
pub fn reset(&mut self) {

0 commit comments

Comments
 (0)