pub struct Entry(/* private fields */);Expand description
A parsed FreeDesktop entry file.
Implementations§
Source§impl Entry
impl Entry
Sourcepub fn parse(input: impl AsRef<[u8]>) -> Result<Self, ParseError>
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");Sourcepub fn parse_file(path: impl AsRef<Path>) -> Result<Self>
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.
Sourcepub fn section(&self, title: impl AsRef<str>) -> Option<&Section>
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?
More examples
Sourcepub fn has_section(&self, title: impl AsRef<str>) -> bool
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"));Sourcepub fn sections(&self) -> SectionIter<'_> ⓘ
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);
}Sourcepub fn get(
&self,
section: impl AsRef<str>,
attr: impl AsRef<str>,
) -> Option<&[String]>
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);Sourcepub fn get_with_param(
&self,
section: impl AsRef<str>,
attr: impl AsRef<str>,
param: impl AsRef<str>,
) -> Option<&[String]>
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<'a> IntoIterator for &'a Entry
impl<'a> IntoIterator for &'a Entry
impl Eq for Entry
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.