Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
MilkFeng committed Aug 15, 2024
1 parent d9d27fc commit 01ddcb4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 39 deletions.
12 changes: 7 additions & 5 deletions src/book.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::fmt::{Debug, Display};

use thiserror::Error;

use crate::file::Files;
use crate::oebps::{Container, ContainerError};
use crate::package::parser::{PackageError, PackageParser, ParseOptions};
use crate::package::prefix::Prefixes;
use crate::package::Package;
use std::fmt::{Debug, Display};
use thiserror::Error;
use crate::package::parser::{PackageError, PackageParseOptions, PackageParser};
use crate::package::prefix::Prefixes;

#[derive(Debug)]
pub struct EpubBook {
Expand Down Expand Up @@ -54,7 +56,7 @@ pub fn parse_book(files: Files) -> Result<EpubBook, ParseBookError> {
.map_err(ParseBookError::ParseContainerError)?
};

let package_parse_options = ParseOptions {
let package_parse_options = PackageParseOptions {
base_url: container.rootfiles[0].full_path.clone(),
reserved_prefixes: Prefixes::reserved(),
};
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ pub mod oebps;

#[cfg(test)]
mod test {
use crate::book::parse_book;
use crate::file::read_zip;
use std::fs::File;

use zip::ZipArchive;

use crate::book::parse_book;
use crate::file::read_zip;

#[test]
fn test_parse() {
let file = File::open("res/example.epub").unwrap();
Expand Down
4 changes: 3 additions & 1 deletion src/oebps.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use minidom::Element;
use std::error::Error;
use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;

use minidom::Element;
use thiserror::Error;
use url::Url;

use crate::package::media_type::media_types::OEBPS;
use crate::package::media_type::MediaType;

Expand Down
7 changes: 4 additions & 3 deletions src/package/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::package::media_type::MediaType;
use crate::package::property::{Properties, Property};
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::string::ToString;

use once_cell::sync::Lazy;
use thiserror::Error;
use url::Url;

use crate::package::media_type::MediaType;
use crate::package::prefix::OPF;
use crate::package::property::{Properties, Property};

/// A Publication Resource.
///
Expand Down Expand Up @@ -85,7 +87,6 @@ static NAV: Lazy<Property> = Lazy::new(|| {
impl Manifest {
/// Create a new Manifest
pub fn new(id: Option<&str>, resources: Vec<Resource>) -> Result<Self, ManifestCheckError> {

let mut id_to_resource = HashMap::new();
let mut href_to_resource = HashMap::new();

Expand Down
4 changes: 2 additions & 2 deletions src/package/media_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl Display for MediaType {
}

impl MediaType {

/// Create a new media type
pub fn new(media_type: &str) -> Self {
MediaType(media_type.to_string())
Expand All @@ -62,9 +61,10 @@ impl MediaType {

/// Core media types
pub mod media_types {
use crate::package::media_type::MediaType;
use once_cell::sync::Lazy;

use crate::package::media_type::MediaType;

// Core media types
// images
pub static GIF: Lazy<MediaType> = Lazy::new(|| MediaType("image/gif".to_string()));
Expand Down
15 changes: 8 additions & 7 deletions src/package/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use crate::package::property::{Properties, Property, WithNamespace};
use chrono::{DateTime, Utc};
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::str::FromStr;

use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use thiserror::Error;
use url::{ParseError, Url};

use crate::package::media_type::MediaType;
use crate::package::prefix::{DC, DCTERMS};
use crate::package::property::{Properties, Property, WithNamespace};

/// The basic metadata element of an EPUB.
///
Expand Down Expand Up @@ -164,7 +165,7 @@ pub struct Metadata {
/// The metadata elements are used to provide information about the publication.
///
/// It MUST contain Dublin Core Metadata Element Set
pub elems: HashMap<WithNamespace, Vec<MetadataElement>>,
pub elems: BTreeMap<WithNamespace, Vec<MetadataElement>>,

/// All meta elements
pub metas: Vec<Meta>,
Expand Down Expand Up @@ -206,7 +207,7 @@ impl Metadata {
links: Vec<Link>,
) -> Result<Self, MetadataCheckError> {
let elems = {
let mut elems_map= HashMap::new();
let mut elems_map= BTreeMap::new();

// group metadata elements by property
for elem in elems {
Expand All @@ -221,7 +222,7 @@ impl Metadata {
// check dublin core metadata element set
{

fn check(elems_map: &HashMap<WithNamespace, Vec<MetadataElement>>, tag_name: &WithNamespace) -> Result<(), MetadataCheckError> {
fn check(elems_map: &BTreeMap<WithNamespace, Vec<MetadataElement>>, tag_name: &WithNamespace) -> Result<(), MetadataCheckError> {
let elems = elems_map.get(&tag_name);
if elems.is_none() || elems.unwrap().is_empty() {
Err(MetadataCheckError::MissingElementError(tag_name.reference.clone()))
Expand Down
22 changes: 12 additions & 10 deletions src/package/parser.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use std::marker::PhantomData;
use std::str::FromStr;

use minidom::Element;
use thiserror::Error;
use url::Url;

use crate::package::manifest::{Manifest, ManifestCheckError, Resource};
use crate::package::metadata::{Link, Meta, Metadata, MetadataCheckError, MetadataElement, Refines};
use crate::package::prefix::{Prefixes, PrefixesStack, DC};
use crate::package::Package;
use crate::package::prefix::{DC, Prefixes, PrefixesStack};
use crate::package::property::{Properties, Property, WithNamespace};
use crate::package::spine::{Spine, SpineReference};
use crate::package::Package;
use crate::utils::invert;
use minidom::Element;
use std::marker::PhantomData;
use std::str::FromStr;
use thiserror::Error;
use url::Url;

#[derive(Debug, Error)]
pub enum PackageError {
Expand All @@ -33,7 +35,7 @@ pub enum PackageError {
}

#[derive(Debug, PartialEq, Clone)]
pub struct ParseOptions {
pub struct PackageParseOptions {
/// base url of the package document.
///
/// every url in the package document will be resolved against this url.
Expand All @@ -50,15 +52,15 @@ pub struct ParseState {
#[derive(Debug)]
pub struct PackageParser {
/// parse options
pub options: ParseOptions,
pub options: PackageParseOptions,
pub parse_state: ParseState,

_private: PhantomData<()>,
}


impl PackageParser {
pub fn new(options: ParseOptions) -> Self {
pub fn new(options: PackageParseOptions) -> Self {
PackageParser {
options,
parse_state: ParseState { prefixes_stack: PrefixesStack::default() },
Expand Down
6 changes: 1 addition & 5 deletions src/package/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::collections::BTreeMap;
use std::ops::{Deref, DerefMut};
use once_cell::sync::Lazy;

use once_cell::sync::Lazy;

/// A map of prefixes to namespaces.
///
/// This trait is used to get the namespace URI for a given prefix.
pub trait PrefixMap {

/// Get the namespace URI for a given prefix.
///
/// # Arguments
Expand Down Expand Up @@ -45,7 +44,6 @@ pub trait PrefixMap {
/// [EPUB 3.3 SPEC reserved-prefixes](https://www.w3.org/TR/epub-33/#sec-reserved-prefixes)
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
pub struct Prefix {

/// The name of the prefix.
///
/// `None` represents the default namespace.
Expand Down Expand Up @@ -148,7 +146,6 @@ impl PrefixMap for Prefixes {
}

impl Prefixes {

/// Create a new Prefixes form a map of prefixes to namespaces.
pub fn new(prefixes: PrefixesInner) -> Self {
Prefixes(prefixes)
Expand Down Expand Up @@ -230,7 +227,6 @@ impl PrefixesStack {
}

impl PrefixMap for PrefixesStack {

/// Get the namespace URI for a given prefix.
///
/// It will find from the top of the stack to the bottom to see if the Prefixes has been pushed before.
Expand Down
8 changes: 4 additions & 4 deletions src/package/property.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::package::prefix::{Prefix, PrefixMap};
use std::cmp::PartialEq;
use std::fmt::Display;
use std::ops::{Deref, DerefMut};

use thiserror::Error;

use crate::package::prefix::{Prefix, PrefixMap};

#[derive(Debug, Error)]
#[error("Invalid namespace: {0:?}")]
pub struct NamespaceError(Option<String>);
Expand All @@ -12,9 +14,8 @@ pub struct NamespaceError(Option<String>);
/// A value with a namespace.
///
/// It can be used to represent a property or a tag name.
#[derive(Debug, PartialEq, Clone, Hash, Eq)]
#[derive(Debug, PartialEq, Clone, Hash, Eq, Ord, PartialOrd)]
pub struct WithNamespace {

/// The namespace of the value.
pub ns: String,

Expand All @@ -23,7 +24,6 @@ pub struct WithNamespace {
}

impl WithNamespace {

/// Create a new WithNamespace from a namespace and a reference.
pub fn new(ns: String, reference: String) -> Self {
WithNamespace { ns, reference }
Expand Down

0 comments on commit 01ddcb4

Please sign in to comment.