Struct libwebnovel_storage::LocalLibrary

source ·
pub struct LocalLibrary { /* private fields */ }
Expand description

A local disk storage.

use tempfile::tempdir;
use libwebnovel_storage::LocalLibrary;

// Dummy path to a config file. If the path does not exist, a default configuration will be created
let config_path = "path/to/toml_config";

let mut library = LocalLibrary::load(config_path).unwrap();
// Add & download a given URL
library
    .add("https://www.royalroad.com/fiction/21220/mother-of-learning")
    .unwrap();
let novel_urls_list = library.list();
assert_eq!(novel_urls_list.len(), 1);
assert_eq!(
    &novel_urls_list[0].to_string(),
    "https://www.royalroad.com/fiction/21220/mother-of-learning"
);

Implementations§

source§

impl LocalLibrary

source

pub fn load(config_path: impl Into<PathBuf>) -> Result<Self, LibraryError>

Attempts to create a new Self from a config file.

use directories::BaseDirs;
use libwebnovel_storage::LocalLibrary;

let config_path = "path/to/config.toml";
let library = LocalLibrary::load(config_path).unwrap(); // If the path doesn't exist, a
                                                        // default configuration will be
                                                        // loaded
// Get the default data directory on this platform
let basedirs = BaseDirs::new().unwrap();
let data_dir = basedirs.data_dir().join(env!["CARGO_PKG_NAME"]);
assert_eq!(library.base_path(), data_dir);
source

pub fn set_base_path( &mut self, base_path: impl Into<PathBuf> + AsRef<Path>, ) -> Result<(), LibraryError>

Changes the library base data path, moving existing content if necessary.

source

pub fn persist(&self) -> Result<(), LibraryError>

Saves the current config to disk.

use std::path::Path;

use libwebnovel_storage::LocalLibrary;

let config_path_str = "/tmp/libwebnovel/config.toml";
let config_path = Path::new(config_path_str);
let library = LocalLibrary::load(config_path_str.clone()).unwrap();

let config_path = Path::new(&config_path_str);
assert!(!config_path.exists());
library.persist().unwrap();
assert!(config_path.exists());
source

pub fn base_path(&self) -> &Path

Returns the base path of the library storage

source

pub fn add(&mut self, url: &str) -> Result<String, LibraryError>

Adds a new webnovel to watch. Won’t call [self.update()].

source

pub fn list(&self) -> Vec<Url>

Returns a list of webnovels currently watched

source

pub fn update(&mut self) -> Result<(), Vec<LibraryError>>

Updates all watched novels. If at least one error has been encountered during update.

source

pub fn remove(&mut self, url: &str) -> Result<(), LibraryError>

Removes a webnovel from the watchlist and deletes local content. If there are duplicates in the novels list, only the first found will be removed and deleted.

source

pub fn novels(&self) -> &Vec<Novel>

Returns a reference to the internal novels vector.

source

pub fn novels_mut(&mut self) -> &mut Vec<Novel>

Returns a mutable reference to the internal novels vector

Trait Implementations§

source§

impl Debug for LocalLibrary

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for LocalLibrary

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for LocalLibrary

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Serialize for LocalLibrary

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,