Skip to main content

rust_motion_affinity/
lib.rs

1//! Reader for native Affinity documents (`.af`, and `.afdesign`/`.afphoto`/`.afpub` from
2//! Affinity 1 and 2): the archive container, the tagged object stream in `doc.dat`, and the
3//! subset of the document model PhotoCraft imports.
4//!
5//! Layouts follow the MIT-licensed afread reference and were checked against public documents
6//! saved by Affinity 1.x to 3.x (container versions 8 to 12); see the crate README for provenance
7//! and the exact supported subset. Nothing here writes Affinity files.
8#![forbid(unsafe_code)]
9#![cfg_attr(
10    not(test),
11    deny(
12        clippy::unwrap_used,
13        clippy::expect_used,
14        clippy::panic,
15        clippy::unimplemented,
16        clippy::todo,
17        clippy::unreachable
18    )
19)]
20
21use std::fmt;
22
23mod access;
24pub mod container;
25mod geometry;
26pub mod model;
27pub mod paint;
28pub mod preview;
29mod raster;
30mod shapes;
31pub mod stream;
32#[cfg(any(test, feature = "synth"))]
33pub mod synth;
34mod text;
35
36pub use container::{is_affinity, Archive, Limits, MAGIC};
37pub use model::{read, Document};
38pub use preview::{preview, Preview, MAX_PREVIEW_BYTES, MAX_PREVIEW_DIMENSION};
39
40/// Why a file could not be read. Messages are short reasons; callers add advice.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub enum Error {
43    Malformed(&'static str),
44    Unsupported(&'static str),
45    Limit(&'static str),
46}
47
48impl fmt::Display for Error {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        match self {
51            Self::Malformed(s) => write!(f, "damaged Affinity file: {s}"),
52            Self::Unsupported(s) => write!(f, "unsupported Affinity file: {s}"),
53            Self::Limit(s) => write!(f, "Affinity file exceeds a safety limit: {s}"),
54        }
55    }
56}
57
58impl std::error::Error for Error {}
59
60#[cfg(test)]
61mod tests;