use std::fs;
use std::io::Write;
use std::error::Error;
pub use printpdf::{PdfDocument, PdfPage};
use crate::spellbook_writer::*;
pub use crate::spells;
pub use crate::spellbook_options::*;
pub fn create_spellbook
(
title: &str,
spells: &Vec<spells::Spell>,
font_paths: FontPaths,
font_sizes: FontSizes,
font_scalars: FontScalars,
spacing_options: SpacingOptions,
text_colors: TextColorOptions,
page_size_options: PageSizeOptions,
page_number_options: Option<PageNumberOptions>,
background: Option<(&str, XObjectTransform)>,
table_options: TableOptions
)
-> Result<PdfDocument, Box<dyn Error>>
{
SpellbookWriter::create_spellbook
(
title,
spells,
font_paths,
font_sizes,
font_scalars,
spacing_options,
text_colors,
page_size_options,
page_number_options,
background,
table_options
)
}
pub fn save_spellbook(doc: PdfDocument, file_name: &str) -> Result<(), Box<dyn std::error::Error>>
{
let save_options = printpdf::serialize::PdfSaveOptions
{
optimize: true,
subset_fonts: true,
secure: true,
image_optimization: None
};
let doc_bytes = doc.save(&save_options, &mut Vec::new());
let mut file = fs::File::create(file_name)?;
file.write_all(&doc_bytes)?;
Ok(())
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SpellFileNameReadError;
impl std::fmt::Display for SpellFileNameReadError
{
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result
{
write!(f, "Couldn't find a file name.")
}
}
impl Error for SpellFileNameReadError {}
pub fn get_all_spells_in_folder(folder_path: &str) -> Result<Vec<spells::Spell>, Box<dyn std::error::Error>>
{
let file_paths = fs::read_dir(folder_path)?;
let mut spell_list = Vec::new();
for file_path in file_paths
{
let file_name_option = file_path?.path();
let file_name = match file_name_option.to_str()
{
Some(name) => name,
None => return Err(Box::new(SpellFileNameReadError))
};
if file_name.ends_with(".json")
{
spell_list.push(spells::Spell::from_json_file(file_name)?);
}
}
Ok(spell_list)
}