#[cfg(not(target_arch = "wasm32"))]
use crate::detect::{detect_format_from_path, FormatType};
#[cfg(not(target_arch = "wasm32"))]
use crate::error::Result;
use crate::model::{Metadata, Section, StyleRegistry};
use crate::parse_options::{ErrorMode, ParseOptions};
use crate::Error;
#[cfg(not(target_arch = "wasm32"))]
use std::ops::ControlFlow;
#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
pub enum ParseEvent<'doc> {
DocumentStart {
metadata: &'doc Metadata,
styles: &'doc StyleRegistry,
section_count: usize,
image_map: std::collections::HashMap<String, String>,
},
SectionParsed(&'doc Section),
SectionFailed {
index: usize,
error: Error,
},
DocumentEnd,
ResourceExtracted {
name: String,
data: Vec<u8>,
},
}
#[derive(Debug, Clone)]
pub struct SectionStreamOptions {
pub error_mode: ErrorMode,
pub extract_resources: bool,
}
impl Default for SectionStreamOptions {
fn default() -> Self {
Self {
error_mode: ErrorMode::Strict,
extract_resources: true,
}
}
}
impl From<&ParseOptions> for SectionStreamOptions {
fn from(opts: &ParseOptions) -> Self {
Self {
error_mode: opts.error_mode,
extract_resources: opts.extract_resources,
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn parse_file_streaming<F>(
path: impl AsRef<Path>,
opts: SectionStreamOptions,
f: F,
) -> Result<()>
where
F: FnMut(ParseEvent<'_>) -> ControlFlow<()>,
{
let path = path.as_ref();
let format = detect_format_from_path(path)?;
match format {
#[cfg(feature = "hwp5")]
FormatType::Hwp5 => {
let mut parser = crate::hwp5::Hwp5Parser::open(path)?;
parser.for_each_section(opts, f)
}
#[cfg(feature = "hwpx")]
FormatType::Hwpx => {
let mut parser = crate::hwpx::HwpxParser::open(path)?;
parser.for_each_section(opts, f)
}
#[cfg(feature = "hwp3")]
FormatType::Hwp3 => Err(Error::UnsupportedFormat(
"streaming not yet supported for HWP 3.x".into(),
)),
#[cfg(not(feature = "hwp3"))]
FormatType::Hwp3 => Err(Error::UnsupportedFormat(
"HWP 3.x support requires 'hwp3' feature".into(),
)),
#[allow(unreachable_patterns)]
_ => Err(Error::UnsupportedFormat(format.to_string())),
}
}