~zethra/freedesktop_entry_parser

43d89b9a91353ae707e9cc3b84ed021aefa62b7c — zethra 3 months ago 5329190
Removed unwraps on file io errors
2 files changed, 26 insertions(+), 17 deletions(-)

M examples/systemd_start_cmd.rs
M src/lib.rs
M examples/systemd_start_cmd.rs => examples/systemd_start_cmd.rs +4 -3
@@ 1,7 1,8 @@
use freedesktop_entry_parser::{Entry, ParseError};
use freedesktop_entry_parser::parse_entry;
use std::io::Result;

fn main() -> Result<(), ParseError> {
    let entry = Entry::parse_file("./test_data/sshd.service")?;
fn main() -> Result<()> {
    let entry = parse_entry("./test_data/sshd.service")?;
    let start_cmd = entry
        .section("Service")
        .attr("ExecStart")

M src/lib.rs => src/lib.rs +22 -14
@@ 85,7 85,7 @@
//!     .expect("Attribute doesn't exist");
//! println!("{}", start_cmd);
//!
//! # Ok::<(), freedesktop_entry_parser::ParseError>(())
//! # Ok::<(), std::io::Error>(())
//! ```
//! There are more examples in the [`examples`]() directory.
//!


@@ 117,9 117,9 @@

/// `Debug` trait impls
mod debug;
/// Eror types
/// Error types
pub mod errors;
/// Entry map inplementaion
/// Entry map implementation
mod internal;
/// Low level parser
mod parser;


@@ 131,15 131,23 @@ pub mod low_level {
    pub use crate::parser::EntryIter;
    pub use crate::parser::SectionBytes;
}
pub use errors::{Result, ParseError};
pub use errors::{ParseError, Result};
use internal::{
    AttrNamesIter, AttrValue, Internal, ParamMap, ParamNamesIter,
    SectionNamesIter,
};
use std::{fs::File, io::Read, path::Path, pin::Pin};
use std::{
    fs::File,
    io::{self, Read},
    path::Path,
    pin::Pin,
};

/// Parse a FreeDesktop entry file.
pub fn parse_entry(input: impl AsRef<Path>) -> Result<Entry> {
///
/// If there is a parse error it'll be return an crate::Error wrapped io::Error
/// with the ErrorKind::Other.
pub fn parse_entry(input: impl AsRef<Path>) -> io::Result<Entry> {
    Entry::parse_file(input)
}



@@ 153,11 161,14 @@ impl Entry {
    }

    /// Parse entry from file.
    pub fn parse_file(path: impl AsRef<Path>) -> Result<Self> {
        let mut file = File::open(path).unwrap();
    ///
    /// If there is a parse error it'll be return an crate::Error wrapped io::Error
    /// with the ErrorKind::Other.
    pub fn parse_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let mut file = File::open(path)?;
        let mut buf = Vec::new();
        file.read_to_end(&mut buf).unwrap();
        Self::parse(buf)
        file.read_to_end(&mut buf)?;
        Self::parse(buf).map_err(|e| io::Error::new(io::ErrorKind::Other, e))
    }

    /// Check if the entry has a section with a `name`.


@@ 293,10 304,7 @@ impl<'a> Attr<'a> {
        ParamIter {
            section_name: self.section_name,
            attr_name: self.name,
            iter: self
                .entry
                .0
                .param_names_iter(self.section_name, self.name),
            iter: self.entry.0.param_names_iter(self.section_name, self.name),
            params: self.attr.get_params(),
        }
    }