Entry

Struct Entry 

Source
pub struct Entry(/* private fields */);
Expand description

A parsed FreeDesktop entry file.

Implementations§

Source§

impl Entry

Source

pub fn parse(input: impl AsRef<[u8]>) -> Result<Self, ParseError>

Parse an entry from byte buffer.

Unlike Entry::parse_file this function performs no I/O and just parses the input string or bytes.

§Example
use freedesktop_entry_parser::Entry;

let entry_bytes = b"
[Unit]
Description=OpenSSH Daemon
Wants=sshdgenkeys.service
After=sshdgenkeys.service
After=network.target

[Service]
ExecStart=/usr/bin/sshd -D
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=always

[Install]
WantedBy=multi-user.target
";
let entry = Entry::parse(entry_bytes)?;
let start_cmd = &entry
    .get("Service", "ExecStart")
    .expect("File doesn't have Service -> ExecStart")[0];
assert_eq!(start_cmd, "/usr/bin/sshd -D");
Source

pub fn parse_file(path: impl AsRef<Path>) -> Result<Self>

Parse entry from file.

If there is a parse error it’ll be return an ParseError wrapped std::io::Error with the std::io::ErrorKind::Other. Equivalent to parse_entry.

Source

pub fn section(&self, title: impl AsRef<str>) -> Option<&Section>

Get section with title.

§Example
use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
";
let entry = Entry::parse(file_content)?;
let section = entry.section("Desktop Entry");
println!("{:#?}", section);
Examples found in repository?
examples/systemd_start_cmd.rs (line 7)
4fn main() -> Result<()> {
5    let entry = parse_entry("./test_data/sshd.service")?;
6    let start_cmd = &entry
7        .section("Service")
8        .expect("Section Service doesn't exist")
9        .attr("ExecStart")[0];
10    println!("{}", start_cmd);
11    Ok(())
12}
More examples
Hide additional examples
examples/get_localized_name.rs (line 6)
3fn main() -> std::io::Result<()> {
4    let lang = std::env::args().nth(1).expect("Not enough args");
5    let entry = parse_entry("./test_data/firefox.desktop")?;
6    let desktop_section = entry.section("Desktop Entry").unwrap();
7    match desktop_section.attr_with_param("GenericName", lang).get(0) {
8        Some(localized_name) => println!("{localized_name}"),
9        None => println!("No name for that lang"),
10    }
11    Ok(())
12}
Source

pub fn has_section(&self, title: impl AsRef<str>) -> bool

Check if the entry has a section title.

§Example
use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
";
let entry = Entry::parse(file_content)?;
assert!(entry.has_section("Desktop Entry"));
Source

pub fn sections(&self) -> SectionIter<'_>

Iterator over sections.

§Example
use freedesktop_entry_parser::Entry;

let file_content = "
[Unit]
Description=OpenSSH Daemon
Wants=sshdgenkeys.service

[Service]
ExecStart=/usr/bin/sshd -D

[Install]
WantedBy=multi-user.target
";
let entry = Entry::parse(file_content)?;
for section in entry.sections() {
    println!("{:#?}", section);
}
Source

pub fn get( &self, section: impl AsRef<str>, attr: impl AsRef<str>, ) -> Option<&[String]>

Get the values of attr in section.

If the section doesn’t exist, this will return None. Otherwise, if the attribute is defined multiple times, all of them will be in the list, if the attribute is missing the list will be empty.

§Example
use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
";
let entry = Entry::parse(file_content)?;
let value = entry.get("Desktop Entry", "Name");
println!("{:#?}", value);

This is a convince method that’s equivalent to calling:

use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
";
let entry = Entry::parse(file_content)?;
let value = match entry.section("Desktop Entry") {
    Some(section) => Some(section.attr("Name")),
    None => None,
};
println!("{:#?}", value);
Source

pub fn get_with_param( &self, section: impl AsRef<str>, attr: impl AsRef<str>, param: impl AsRef<str>, ) -> Option<&[String]>

Get the values of attr with param in section.

If the section doesn’t exist, this will return None. Otherwise, if the attribute is defined multiple times, all of them will be in the list, if the attribute is missing the list will be empty.

§Example
use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
GenericName[ca]=Navegador web
";
let entry = Entry::parse(file_content)?;
let value = entry.get_with_param("Desktop Entry", "GenericName", "ca");
println!("{:#?}", value);

This is a convince method that’s equivalent to calling:

use freedesktop_entry_parser::Entry;

let file_content = "
[Desktop Entry]
Name=Firefox
GenericName[ca]=Navegador web
";
let entry = Entry::parse(file_content)?;
let value = match entry.section("Desktop Entry") {
    Some(section) => Some(section.attr_with_param("GenericName", "ca")),
    None => None,
};
println!("{:#?}", value);

Trait Implementations§

Source§

impl Clone for Entry

Source§

fn clone(&self) -> Entry

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Entry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Idx: AsRef<str>> Index<Idx> for Entry

Source§

fn index(&self, title: Idx) -> &Self::Output

Returns a reference to the section corresponding to the supplied title.

§Panics

Panics if the title is not a section in the Entry.

Source§

type Output = Section

The returned type after indexing.
Source§

impl<'a> IntoIterator for &'a Entry

Source§

type Item = (&'a String, &'a Section)

The type of the elements being iterated over.
Source§

type IntoIter = SectionIter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Entry

Source§

fn eq(&self, other: &Entry) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Entry

Source§

impl StructuralPartialEq for Entry

Auto Trait Implementations§

§

impl Freeze for Entry

§

impl RefUnwindSafe for Entry

§

impl Send for Entry

§

impl Sync for Entry

§

impl Unpin for Entry

§

impl UnwindSafe for Entry

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.