noctua/core/src/document/loader.rs

73 lines
2.4 KiB
Rust
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-or-later
// src/document/loader.rs
//
// Document loading: 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::error::{Error, Result};
use std::path::Path;
/// Loads a document from a path and returns its content.
pub fn load_document(path: &Path) -> Result<DocumentContent> {
if !path.exists() {
return Err(Error::NotFound(format!("{:?}", path)));
}
match detect_format(path) {
Some(DocumentFormat::Portable) => {
// TODO: Replace with actual pdfium-render loading.
Ok(DocumentContent::Portable(PortableModel {
path: path.to_path_buf(),
page_count: 3,
layout: PageLayout::new(vec![(595.0, 842.0); 3], 10.0),
}))
}
Some(DocumentFormat::Vector) => {
// TODO: Replace with actual usvg parsing.
Ok(DocumentContent::Vector(VectorModel {
path: path.to_path_buf(),
view_box: None,
}))
}
Some(DocumentFormat::Raster) => {
// TODO: Replace with actual image-crate loading.
Ok(DocumentContent::Raster(RasterModel {
path: path.to_path_buf(),
dimensions: (1920, 1080),
}))
}
None => Err(Error::Unsupported(format!(
"extension: {}",
path.extension()
.and_then(|e| e.to_str())
.unwrap_or("(none)")
))),
}
}
/// Scans a directory for supported files and populates the session.
pub 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())
.map(|e| e.path())
.filter(|p| p.is_file() && detect_format(p).is_some())
.collect();
paths.sort();
session.items = paths.into_iter().map(|p| SessionItem::new(p, 0)).collect();
}
session.current_index = if session.items.is_empty() {
None
} else {
Some(0)
};
}