use super::input::JsonReactor;
use super::{parse_reader, JsonError};
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 AsRef<[u8]>,
mut options: PdfOpenOptions,
) -> Result<Pdf<Box<dyn crate::ReadSeek>>>
where
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
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 AsRef<[u8]>) -> 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 AsRef<[u8]>,
mut options: PdfOpenOptions,
) -> Result<Self>
where
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
options.description = input_name.clone();
let mut pdf = Self::open_mem_owned_with_options(JSON_PDF.to_vec(), options)?; if let Err(error) = pdf.import_json(source, input_name, true) {
let diagnostics = pdf.repair_diagnostics();
return Err(Error::with_open_diagnostics(error, diagnostics));
}
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_description_bytes(&path);
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 AsRef<[u8]>) -> 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_description_bytes(&path);
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 AsRef<[u8]>,
must_be_complete: bool,
) -> Result<()>
where
S: Read + Seek + 'static,
{
let input_name = input_name.as_ref().to_vec();
let mut source = source;
let base_offset = source
.stream_position()
.map_err(|error| raw_system_error(&input_name, error.to_string().as_bytes()))?;
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(raw_system_error(&input_name, error.as_bytes()));
}
if let Err(error) = parsed {
let detail: &[u8] = match &error {
JsonError::Type(message) | JsonError::Parse(message) => message.as_bytes(),
};
return Err(raw_system_error(&input_name, detail));
}
if reactor.any_errors() {
return Err(raw_system_error(&input_name, b"errors found in JSON"));
}
Ok(())
}
}
fn raw_system_error(input_name: &[u8], detail: &[u8]) -> Error {
let mut message = input_name.to_vec();
message.extend_from_slice(b": ");
message.extend_from_slice(detail);
Error::SystemBytes(message)
}
fn open_json_file(path: &PathBuf) -> Result<File> {
File::open(path).map_err(|error| Error::file_io("open JSON input", path.clone(), error))
}
fn path_description_bytes(path: &Path) -> Vec<u8> {
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
path.as_os_str().as_bytes().to_vec()
}
#[cfg(not(unix))]
{
path.to_string_lossy().into_owned().into_bytes()
}
}