Skip to main content

ResourceKey

Enum ResourceKey 

Source
pub enum ResourceKey<'a> {
    Value(Cow<'a, str>),
    Position(usize),
}
Expand description

A key identifying where the content is held for a Resource, either by string or numeric position.

§Examples

  • Creating a key using From (Into is usable as well):
let position_key = ResourceKey::from(5);
let str_key = ResourceKey::from("OEBPS/nav/toc.xhtml");
// A path may be passed as well, although it must contain valid UTF-8.
// Otherwise, the requested file won't be found and an error will
// be propagated from methods that process a `ResourceKey`:
let str_key2 = ResourceKey::from(Path::new("EPUB/toc.ncx"));

assert!(matches!(position_key, ResourceKey::Position(5)));
assert!(matches!(str_key, ResourceKey::Value(Cow::Borrowed("OEBPS/nav/toc.xhtml"))));
assert!(matches!(str_key2, ResourceKey::Value(Cow::Borrowed("EPUB/toc.ncx"))));

Variants§

§

Value(Cow<'a, str>)

A string-based key (e.g., file path, URI) that references where a resource is stored.

When using the From<&Path> impl, the input must be valid UTF-8; any non-UTF-8 sequences will be replaced with .

§

Position(usize)

A numeric index-based key for locations that cannot be represented with a string value.

This is primarily useful for resources that do not have a string value available, but rather a “pointer” to access an element within an indexable structure, such as the MOBI format.

Implementations§

Source§

impl ResourceKey<'_>

Source

pub fn value(&self) -> Option<&str>

Returns the contained value if self is ResourceKey::Value.

§Examples
  • Retrieving the value:
let key = ResourceKey::from("value");
assert_eq!(Some("value"), key.value());
assert_eq!(None, key.position());
Source

pub fn position(&self) -> Option<usize>

Returns the contained position if self is ResourceKey::Position.

§Examples
  • Retrieving the position:
let key = ResourceKey::from(5);
assert_eq!(Some(5), key.position());
assert_eq!(None, key.value());

Trait Implementations§

Source§

impl<'a> Clone for ResourceKey<'a>

Source§

fn clone(&self) -> ResourceKey<'a>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for ResourceKey<'a>

Source§

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

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

impl<'a> Eq for ResourceKey<'a>

Source§

impl<'a> From<&'a Path> for ResourceKey<'a>

It is expected that the given path is UTF-8 compliant. Otherwise, the associated resource will never be found within an ebook.

For fallible operations, prefer Path::to_str, which ensures the given string is UTF-8 compliant:

let epub = Epub::open("tests/ebooks/example_epub")?;
let mut path = PathBuf::from("/EPUB");
path.push("c1.xhtml");

// `path` is implicitly turned into a `Resource`
let bytes_a = epub.read_resource_bytes(&*path)?;

if let Some(utf8_value) = path.to_str() {
    // Explicitly providing a `Resource`
    let resource = Resource::from(utf8_value);
    let bytes_b = epub.read_resource_bytes(resource)?;
    assert_eq!(bytes_a, bytes_b);
}

§Windows

This conversion allocates on Windows operating systems to convert path separators, if needed. Specifically, replacing back-slashes (\) with forward-slashes (/).

For example: path\to\content.xhtmlpath/to/content.xhtml

Source§

fn from(path: &'a Path) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a ResourceKey<'a>> for ResourceKey<'a>

Source§

fn from(value: &'a Self) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a String> for ResourceKey<'a>

Source§

fn from(value: &'a String) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<&'a str> for ResourceKey<'a>

Source§

fn from(value: &'a str) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Cow<'a, str>> for ResourceKey<'a>

Source§

fn from(value: Cow<'a, str>) -> Self

Converts to this type from the input type.
Source§

impl<'a> From<Href<'a>> for ResourceKey<'a>

Source§

fn from(value: Href<'a>) -> Self

Converts to this type from the input type.
Source§

impl From<String> for ResourceKey<'_>

Source§

fn from(value: String) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for ResourceKey<'_>

Source§

fn from(value: usize) -> Self

Converts to this type from the input type.
Source§

impl<'a> Hash for ResourceKey<'a>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<'a> PartialEq for ResourceKey<'a>

Source§

fn eq(&self, other: &ResourceKey<'a>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for ResourceKey<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for ResourceKey<'a>

§

impl<'a> RefUnwindSafe for ResourceKey<'a>

§

impl<'a> Send for ResourceKey<'a>

§

impl<'a> Sync for ResourceKey<'a>

§

impl<'a> Unpin for ResourceKey<'a>

§

impl<'a> UnsafeUnpin for ResourceKey<'a>

§

impl<'a> UnwindSafe for ResourceKey<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<'a, I> Many<Resource<'a>> for I
where I: Into<Resource<'a>>,

Source§

type Iter = Once<Resource<'a>>

The iterator produced.
Source§

fn iter_many(self) -> <I as Many<Resource<'a>>>::Iter

Returns an iterator over one-to-many items.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.