summaryrefslogtreecommitdiff
path: root/src/loading/mod.rs
blob: 64a65580b9f8ac8cefe0f38be46bb59007e74171 (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
//! Resource loading.

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

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

use std::io;
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::font::FaceInfo;

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

    /// Resolve a `path` relative to a `base` file.
    ///
    /// This should return the same id for all paths pointing to the same file
    /// and `None` if the file does not exist.
    fn resolve_from(&self, base: FileId, path: &Path) -> io::Result<FileId>;

    /// Load a file by id.
    ///
    /// This must only be called with an `id` returned by a call to this
    /// loader's `resolve_from` method.
    fn load_file(&self, id: FileId) -> io::Result<Vec<u8>>;
}

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

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

    /// Convert into the raw underlying 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 resolve_from(&self, _: FileId, _: &Path) -> io::Result<FileId> {
        Err(io::ErrorKind::NotFound.into())
    }

    fn load_file(&self, _: FileId) -> io::Result<Vec<u8>> {
        panic!("resolve_from never returns an id")
    }
}