refactor(core): reorganize module structure
This commit is contained in:
parent
59ab96be98
commit
a6c1863f57
22 changed files with 231 additions and 164 deletions
|
|
@ -1,21 +1,56 @@
|
|||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
// src/document/loader.rs
|
||||
//
|
||||
// Document loading: format dispatch and directory scanning.
|
||||
// Document loading: format detection, format dispatch, and directory scanning.
|
||||
|
||||
use crate::document::format::{detect_format, DocumentFormat};
|
||||
use crate::document::portable::PortableModel;
|
||||
use crate::document::raster::RasterModel;
|
||||
use crate::document::session::{SessionData, SessionItem};
|
||||
use crate::document::vector::VectorModel;
|
||||
use crate::document::{core::PageLayout, DocumentContent};
|
||||
use crate::document::model::{PortableModel, RasterModel, VectorModel};
|
||||
use crate::document::session::data::{SessionData, SessionItem};
|
||||
use crate::document::types::PageLayout;
|
||||
use crate::document::DocumentContent;
|
||||
use crate::error::{Error, Result};
|
||||
use std::path::Path;
|
||||
|
||||
/// The three document formats Noctua understands.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub(crate) enum DocumentFormat {
|
||||
/// Raster images – anything the image crate can decode.
|
||||
Raster,
|
||||
/// Vector graphics (SVG).
|
||||
Vector,
|
||||
/// Portable documents (PDF).
|
||||
Portable,
|
||||
}
|
||||
|
||||
/// Detects the document format from the file extension.
|
||||
///
|
||||
/// Returns `None` for unknown extensions so directory scans can skip
|
||||
/// unsupported files early. At load time the underlying library
|
||||
/// (`ImageReader`, `usvg`, `pdfium`) is the final authority.
|
||||
fn detect_format(path: &Path) -> Option<DocumentFormat> {
|
||||
match path
|
||||
.extension()
|
||||
.and_then(|e| e.to_str())
|
||||
.map(str::to_lowercase)
|
||||
.as_deref()
|
||||
{
|
||||
Some("pdf") => Some(DocumentFormat::Portable),
|
||||
Some("svg") => Some(DocumentFormat::Vector),
|
||||
Some("png" | "jpg" | "jpeg" | "webp" | "gif" | "bmp" | "tiff" | "tif") => {
|
||||
Some(DocumentFormat::Raster)
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a document from a path and returns its content.
|
||||
pub fn load_document(path: &Path) -> Result<DocumentContent> {
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
/// Returns [`Error::NotFound`] if the path does not exist.
|
||||
/// Returns [`Error::Unsupported`] if the file extension is not recognised.
|
||||
pub(crate) fn load_document(path: &Path) -> Result<DocumentContent> {
|
||||
if !path.exists() {
|
||||
return Err(Error::NotFound(format!("{:?}", path)));
|
||||
return Err(Error::NotFound(format!("{}", path.display())));
|
||||
}
|
||||
|
||||
match detect_format(path) {
|
||||
|
|
@ -51,10 +86,10 @@ pub fn load_document(path: &Path) -> Result<DocumentContent> {
|
|||
}
|
||||
|
||||
/// Scans a directory for supported files and populates the session.
|
||||
pub fn scan_directory_into(session: &mut SessionData, dir: &Path) {
|
||||
pub(crate) fn scan_directory_into(session: &mut SessionData, dir: &Path) {
|
||||
if let Ok(entries) = std::fs::read_dir(dir) {
|
||||
let mut paths: Vec<_> = entries
|
||||
.filter_map(|e| e.ok())
|
||||
.filter_map(std::result::Result::ok)
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && detect_format(p).is_some())
|
||||
.collect();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue