1use std::fs::File;
2use std::io::{self, BufReader, Read, Seek, Write};
3use std::ops::{Deref, DerefMut};
4use std::path::{Path, PathBuf};
5
6use crate::{sign, template::Template};
7
8#[derive(Debug, Clone)]
10pub struct Pass {
11 pass_path: PathBuf,
13 pub template: Template,
15}
16
17impl Pass {
18 pub fn from_path<P: AsRef<Path>>(pass_path: P) -> io::Result<Self> {
20 let path_buf = pass_path.as_ref().join("pass.json");
21
22 let file = File::open(path_buf)?;
23 let mut file_reader = BufReader::new(file);
24 let mut file_buffer = Vec::new();
25 file_reader.read_to_end(&mut file_buffer)?;
26 let template: crate::template::Template = serde_json::from_slice(&file_buffer)?;
27
28 Ok(Self {
29 pass_path: pass_path.as_ref().to_path_buf(),
30 template,
31 })
32 }
33
34 pub fn from_template<P: AsRef<Path>>(template: &Template, pass_path: P) -> Self {
36 Self {
37 pass_path: pass_path.as_ref().to_path_buf(),
38 template: template.clone(),
39 }
40 }
41
42 pub fn export<T, P1: AsRef<Path>, P2: AsRef<Path>>(
44 &self,
45 certificate_path: P1,
46 certificate_password: &str,
47 wwdr_intermediate_certificate_path: P2,
48 writer: T,
49 ) -> io::Result<T>
50 where
51 T: Write + Seek,
52 {
53 sign::sign_path(
54 &self.pass_path,
55 Some(&self.template),
56 certificate_path,
57 certificate_password,
58 wwdr_intermediate_certificate_path,
59 writer,
60 false,
61 )
62 }
63
64 pub fn export_to_file<P1: AsRef<Path>, P2: AsRef<Path>, P3: AsRef<Path>>(
66 &self,
67 certificate_path: P1,
68 certificate_password: &str,
69 wwdr_intermediate_certificate_path: P2,
70 output_path: P3,
71 ) -> io::Result<()> {
72 let file = File::create(output_path)?;
73 self.export(
74 certificate_path,
75 certificate_password,
76 wwdr_intermediate_certificate_path,
77 file,
78 )?;
79
80 Ok(())
81 }
82}
83
84impl Deref for Pass {
85 type Target = Template;
86
87 fn deref(&self) -> &Self::Target {
88 &self.template
89 }
90}
91
92impl DerefMut for Pass {
93 fn deref_mut(&mut self) -> &mut Self::Target {
94 &mut self.template
95 }
96}