@@ -3,7 +3,7 @@ use seeyou_cup::CupFile;
33use std:: collections:: HashMap ;
44use std:: fs:: File ;
55use std:: io:: { Cursor , Seek , Write } ;
6- use std:: path:: { Path , PathBuf } ;
6+ use std:: path:: Path ;
77
88/// A builder for creating CUPX files with waypoint data and pictures.
99///
@@ -18,46 +18,41 @@ use std::path::{Path, PathBuf};
1818/// use seeyou_cup::CupFile;
1919/// # use std::path::Path;
2020///
21- /// CupxWriter::new(CupFile::default())
21+ /// # let cup_file = CupFile::default();
22+ /// CupxWriter::new(&cup_file)
2223/// .add_picture("photo.jpg", Path::new("images/photo.jpg"))
2324/// .write_to_path("output.cupx")?;
2425/// # Ok::<(), seeyou_cupx::Error>(())
2526/// ```
26- pub struct CupxWriter {
27- cup_file : CupFile ,
28- pictures : HashMap < String , PictureSource > ,
27+ pub struct CupxWriter < ' a > {
28+ cup_file : & ' a CupFile ,
29+ pictures : HashMap < & ' a str , PictureSource < ' a > > ,
2930}
3031
3132/// Source of picture data for inclusion in a CUPX file.
3233///
33- /// Pictures can be provided either as in-memory byte vectors or as file paths
34+ /// Pictures can be provided either as in-memory byte slices or as file paths
3435/// that will be read when the CUPX file is written.
35- pub enum PictureSource {
36- /// Picture data provided as a byte vector in memory .
37- Bytes ( Vec < u8 > ) ,
36+ pub enum PictureSource < ' a > {
37+ /// Picture data provided as a borrowed byte slice .
38+ Bytes ( & ' a [ u8 ] ) ,
3839 /// Picture data will be read from a file at the given path.
39- Path ( PathBuf ) ,
40+ Path ( & ' a Path ) ,
4041}
4142
42- impl From < Vec < u8 > > for PictureSource {
43- fn from ( bytes : Vec < u8 > ) -> Self {
43+ impl < ' a > From < & ' a [ u8 ] > for PictureSource < ' a > {
44+ fn from ( bytes : & ' a [ u8 ] ) -> Self {
4445 PictureSource :: Bytes ( bytes)
4546 }
4647}
4748
48- impl From < PathBuf > for PictureSource {
49- fn from ( path : PathBuf ) -> Self {
49+ impl < ' a > From < & ' a Path > for PictureSource < ' a > {
50+ fn from ( path : & ' a Path ) -> Self {
5051 PictureSource :: Path ( path)
5152 }
5253}
5354
54- impl From < & Path > for PictureSource {
55- fn from ( path : & Path ) -> Self {
56- PictureSource :: Path ( path. to_path_buf ( ) )
57- }
58- }
59-
60- impl CupxWriter {
55+ impl < ' a > CupxWriter < ' a > {
6156 /// Creates a new CUPX writer with the given waypoint/task data.
6257 ///
6358 /// Pictures can be added using [`add_picture`](Self::add_picture).
@@ -69,10 +64,10 @@ impl CupxWriter {
6964 /// use seeyou_cup::CupFile;
7065 ///
7166 /// let cup_file = CupFile::default();
72- /// let writer = CupxWriter::new(cup_file);
67+ /// let writer = CupxWriter::new(& cup_file);
7368 /// # Ok::<(), seeyou_cupx::Error>(())
7469 /// ```
75- pub fn new ( cup_file : CupFile ) -> Self {
70+ pub fn new ( cup_file : & ' a CupFile ) -> Self {
7671 Self {
7772 cup_file,
7873 pictures : HashMap :: new ( ) ,
@@ -93,18 +88,20 @@ impl CupxWriter {
9388 /// use seeyou_cup::CupFile;
9489 /// # use std::path::Path;
9590 ///
96- /// CupxWriter::new(CupFile::default())
91+ /// # let cup_file = CupFile::default();
92+ /// # let image_data = vec![0u8; 100];
93+ /// CupxWriter::new(&cup_file)
9794 /// .add_picture("photo1.jpg", Path::new("images/photo1.jpg"))
98- /// .add_picture("photo2.jpg", vec![0u8; 100 ])
95+ /// .add_picture("photo2.jpg", &image_data[.. ])
9996 /// .write_to_path("output.cupx")?;
10097 /// # Ok::<(), seeyou_cupx::Error>(())
10198 /// ```
10299 pub fn add_picture (
103100 & mut self ,
104- filename : impl Into < String > ,
105- source : impl Into < PictureSource > ,
101+ filename : & ' a str ,
102+ source : impl Into < PictureSource < ' a > > ,
106103 ) -> & mut Self {
107- self . pictures . insert ( filename. into ( ) , source. into ( ) ) ;
104+ self . pictures . insert ( filename, source. into ( ) ) ;
108105 self
109106 }
110107
@@ -121,7 +118,7 @@ impl CupxWriter {
121118 pub fn write < W : Write + Seek > ( & self , writer : W ) -> Result < ( ) , Error > {
122119 for filename in self . pictures . keys ( ) {
123120 if filename. is_empty ( ) || filename. contains ( '/' ) || filename. contains ( '\\' ) {
124- return Err ( Error :: InvalidFilename ( filename. clone ( ) ) ) ;
121+ return Err ( Error :: InvalidFilename ( filename. to_string ( ) ) ) ;
125122 }
126123 }
127124
@@ -168,7 +165,8 @@ impl CupxWriter {
168165 /// use seeyou_cupx::CupxWriter;
169166 /// use seeyou_cup::CupFile;
170167 ///
171- /// let bytes = CupxWriter::new(CupFile::default()).write_to_vec()?;
168+ /// let cup_file = CupFile::default();
169+ /// let bytes = CupxWriter::new(&cup_file).write_to_vec()?;
172170 /// # Ok::<(), seeyou_cupx::Error>(())
173171 /// ```
174172 ///
@@ -190,7 +188,8 @@ impl CupxWriter {
190188 /// use seeyou_cupx::CupxWriter;
191189 /// use seeyou_cup::CupFile;
192190 ///
193- /// CupxWriter::new(CupFile::default()).write_to_path("output.cupx")?;
191+ /// let cup_file = CupFile::default();
192+ /// CupxWriter::new(&cup_file).write_to_path("output.cupx")?;
194193 /// # Ok::<(), seeyou_cupx::Error>(())
195194 /// ```
196195 ///
0 commit comments