use super::input::JsonReactor;
use super::parse_reader;
use crate::{Error, Pdf, PdfOpenOptions, Result};
use std::cell::RefCell;
use std::fs::File;
use std::io::{Cursor, Read, Seek};
use std::path::{Path, PathBuf};
use std::rc::Rc;
const JSON_PDF: &[u8] = concat!(
"%PDF-1.3\n",
"xref\n",
"0 1\n",
"0000000000 65535 f \n",
"trailer << /Size 1 >>\n",
"startxref\n",
"9\n",
"%%EOF\n",
)
.as_bytes();
pub(crate) fn create_from_json_erased<S>(
source: S,
input_name: impl Into<String>,
mut options: PdfOpenOptions,
) -> Result<Pdf<Box<dyn crate::ReadSeek>>>
where
S: Read + Seek + 'static,
{
let input_name = input_name.into();
options.description = input_name.clone();
let mut pdf = Pdf::<Box<dyn crate::ReadSeek>>::open_with_options(
Box::new(Cursor::new(JSON_PDF.to_vec())),
options,
)?; pdf.import_json(source, input_name, true)?;
Ok(pdf)
}
impl Pdf<Cursor<Vec<u8>>> {
pub fn create_from_json<S>(source: S, input_name: impl Into<String>) -> Result<Self>
where
S: Read + Seek + 'static,
{
Self::create_from_json_with_options(source, input_name, PdfOpenOptions::default())
}
pub fn create_from_json_with_options<S>(
source: S,
input_name: impl Into<String>,
mut options: PdfOpenOptions,
) -> Result<Self>
where
S: Read + Seek + 'static,
{
let input_name = input_name.into();
options.description = input_name.clone();
let mut pdf = Self::open_mem_owned_with_options(JSON_PDF.to_vec(), options)?; pdf.import_json(source, input_name, true)?;
Ok(pdf)
}
pub fn create_from_json_file(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref().to_path_buf();
let input_name = path.display().to_string();
let source = open_json_file(&path)?;
Self::create_from_json(source, input_name)
}
}
impl<R> Pdf<R>
where
R: Read + Seek + 'static,
{
pub fn update_from_json<S>(&mut self, source: S, input_name: impl Into<String>) -> Result<()>
where
S: Read + Seek + 'static,
{
self.import_json(source, input_name, false)
}
pub fn update_from_json_file(&mut self, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref().to_path_buf();
let input_name = path.display().to_string();
let source = open_json_file(&path)?;
self.update_from_json(source, input_name)
}
pub(crate) fn import_json<S>(
&mut self,
source: S,
input_name: impl Into<String>,
must_be_complete: bool,
) -> Result<()>
where
S: Read + Seek + 'static,
{
let input_name = input_name.into();
let mut source = source;
let base_offset = source
.stream_position()
.map_err(|error| Error::System(format!("{input_name}: {error}")))?;
let source = Rc::new(RefCell::new(source));
let mut reactor = JsonReactor::new(
self,
Rc::clone(&source),
input_name.clone(),
must_be_complete,
)
.with_stream_data_base_offset(base_offset);
let parsed = {
let mut source = source.borrow_mut();
parse_reader(&mut *source, Some(&mut reactor))
};
if let Some(error) = reactor.fatal_error() {
return Err(Error::System(format!("{input_name}: {error}")));
}
if let Err(error) = parsed {
return Err(Error::System(format!("{input_name}: {error}")));
}
if reactor.any_errors() {
return Err(Error::System(format!("{input_name}: errors found in JSON")));
}
Ok(())
}
}
fn open_json_file(path: &PathBuf) -> Result<File> {
File::open(path).map_err(|error| Error::file_io("open JSON input", path.clone(), error))
}