summaryrefslogtreecommitdiff
path: root/src/loading/mod.rs
blob: 0e171796ba5f6560733be80b84c1cad8c15e8e95 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//! Resource loading.

#[cfg(feature = "fs")]
mod fs;

#[cfg(feature = "fs")]
pub use fs::*;

use std::path::Path;
use std::rc::Rc;

use crate::font::FaceInfo;

/// A shared byte buffer.
pub type Buffer = Rc<Vec<u8>>;

/// Loads resources from a local or remote source.
pub trait Loader {
    /// Descriptions of all font faces this loader serves.
    fn faces(&self) -> &[FaceInfo];

    /// Load the font face with the given index in [`faces()`](Self::faces).
    fn load_face(&mut self, idx: usize) -> Option<Buffer>;

    /// Load a file from a path.
    fn load_file(&mut self, path: &Path) -> Option<Buffer>;

    /// Resolve a hash for the file the path points to.
    ///
    /// This should return the same hash for all paths pointing to the same file
    /// and `None` if the file does not exist.
    fn resolve(&self, path: &Path) -> Option<FileHash>;
}

/// A file hash that can be [resolved](Loader::resolve) from a path.
///
/// Should be the same for all paths pointing to the same file.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct FileHash(u64);

impl FileHash {
    /// Create an file hash from a raw hash value.
    pub fn from_raw(v: u64) -> Self {
        Self(v)
    }

    /// Convert into the raw underlying hash value.
    pub fn into_raw(self) -> u64 {
        self.0
    }
}

/// A loader which serves nothing.
pub struct BlankLoader;

impl Loader for BlankLoader {
    fn faces(&self) -> &[FaceInfo] {
        &[]
    }

    fn load_face(&mut self, _: usize) -> Option<Buffer> {
        None
    }

    fn load_file(&mut self, _: &Path) -> Option<Buffer> {
        None
    }

    fn resolve(&self, _: &Path) -> Option<FileHash> {
        None
    }
}