Skip to main content

aria_kit/backend/
mod.rs

1//! What every engine backend shares, whichever capability it serves: the
2//! boxed shape of its own failures, the files its model downloads as, and
3//! validation of the model files it loads. Each capability wraps these in
4//! its own error vocabulary.
5
6#[cfg(not(target_family = "wasm"))]
7mod model_path;
8
9use std::path::PathBuf;
10
11#[cfg(not(target_family = "wasm"))]
12pub use model_path::{validate_model_directory, validate_model_file};
13
14/// One file of an engine's model and where to download it.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub struct ModelDownload {
17    /// The file's name on disk, inside whatever folder holds the model.
18    pub file_name: &'static str,
19    pub url: &'static str,
20    /// Size of what `url` serves. For an archive member, the whole archive.
21    pub bytes: u64,
22    /// SHA-256 of what `url` serves, in lowercase hex.
23    pub sha256: &'static str,
24    pub unpack: Unpack,
25}
26
27/// How the file comes out of what `url` serves.
28#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub enum Unpack {
30    /// `url` serves the file itself.
31    Plain,
32    /// `url` serves a `.tar.bz2` archive holding the file at this path.
33    TarBz2Member(&'static str),
34}
35
36// The engine's own error stays boxed rather than named. Which engine failed is
37// already on the capability error, and naming the types here would put every
38// engine crate's version in Aria's public API.
39#[derive(Debug, thiserror::Error)]
40pub enum BackendError {
41    #[error(transparent)]
42    Engine(Box<dyn std::error::Error + Send + Sync>),
43
44    #[error("the backend answered, but the answer is unusable: {0}")]
45    Unusable(String),
46}
47
48impl BackendError {
49    pub fn engine(error: impl std::error::Error + Send + Sync + 'static) -> Self {
50        Self::Engine(Box::new(error))
51    }
52}
53
54#[derive(Debug, thiserror::Error)]
55pub enum ModelError {
56    #[cfg(not(target_family = "wasm"))]
57    #[error(transparent)]
58    Path(#[from] crate::primitives::file_inspection::InspectionError),
59
60    #[error("failed to read the model directory {}", path.display())]
61    ReadDirectory {
62        path: PathBuf,
63        #[source]
64        source: std::io::Error,
65    },
66
67    #[error("model directory {} is missing {file}", directory.display())]
68    Missing {
69        directory: PathBuf,
70        file: &'static str,
71    },
72
73    #[error("failed to read the model file {}", path.display())]
74    Unreadable {
75        path: PathBuf,
76        #[source]
77        source: std::io::Error,
78    },
79
80    #[error("the model file {} is malformed", path.display())]
81    Malformed {
82        path: PathBuf,
83        #[source]
84        source: Box<dyn std::error::Error + Send + Sync>,
85    },
86}