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.
114use std:: error:: Error ;
215use std:: fs:: { File , create_dir_all} ;
316use std:: io:: { Read , Write } ;
@@ -6,6 +19,40 @@ use std::path::PathBuf;
619use serde:: Serialize ;
720use 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.
956pub struct FiltersCache {
1057 base_directory : PathBuf ,
1158 cache_filename : & ' static str ,
0 commit comments