pgs_parse/
pgs_reader.rs

1use std::{fs::File, path::Path};
2
3use crate::{pgs_error::Result, Error, PgsFile};
4
5/// A struct for handling the opening of files to create `PgsFile` instances.
6///
7/// Currently, this struct does not maintain any internal state but provides a method to open a file and return a
8/// `PgsFile` instance.
9#[derive(Debug, Default)]
10pub struct PgsReader {
11
12}
13
14impl PgsReader {
15    /// Opens a file and returns a `PgsFile` instance.
16    ///
17    /// This method checks if the specified file exists and then attempts to open it. If successful, it creates a new
18    /// `PgsFile` instance using the opened file.
19    ///
20    /// # Arguments
21    /// * `sup_file_path` - A string slice representing the path to the file to be opened.
22    ///
23    /// # Returns
24    /// Returns a `Result` containing either a `PgsFile` instance on success or an `Error` if the file does not exist,
25    /// cannot be opened, or if creating the `PgsFile` instance fails.
26    ///
27    /// # Errors
28    /// * `Error::File` - If the file does not exist or if the file cannot be opened.
29    /// * Any other `Error` arising from `PgsFile::new` or file operations.
30    pub fn open(sup_file_path: &str) -> Result<PgsFile> {
31        let path = Path::new(sup_file_path);
32        if !path.exists() {
33            return Err(Error::File(std::io::Error::new(std::io::ErrorKind::NotFound, "File not Exists")));
34        }
35        let file = File::open(sup_file_path)?;
36        PgsFile::new(file)
37    }
38}