pub struct KeyFile { /* private fields */ }Expand description
A parsed key file, such as the repository config file.
A key file is an ordered list of groups. Each group is an ordered list of entries, and each entry is a key and its raw value text.
§Syntax
parse reads a subset of the GLib key file format:
- A
[group]line starts a group. The group name is the text from[to the first]. Only white space can follow the]. - A
key=valueline is an entry of the current group. The key is the text before the first=. - A line that starts with
#is a comment line. Only#starts a comment. A#in a value is part of the value. - A blank line has no text or only white space.
- A line ends with LF or with CRLF. The parser removes one carriage return at the end of a line.
The parser uses ASCII space and tab as white space:
- It ignores this white space around a blank line, a comment line, and a group header.
- It removes this white space around a key and at the start of a value.
- It keeps the white space at the end of a value.
- It keeps a non-breaking space (U+00A0) and all other non-ASCII white space everywhere.
The parser keeps the group order, the key order, and the raw value text. It drops comment lines and blank lines. A repeated group header merges into the group of the first header. A repeated key keeps its first position and takes the last value.
§Writes
get_value returns the raw value text. The typed
getters unescape the value on read, and
get_string_list also splits it into items.
The Display output parses to an equal KeyFile. A
rewritten file keeps the groups and the keys that the caller did not
change, in their order and with their value text. It does not keep the
comment lines and the blank lines of the input. The ostree command
rewrites a config file in the same way.
§Examples
use ostrya_core::KeyFile;
let mut config = KeyFile::parse("[core]\nrepo_version=1\nmode=archive-z2\n")?;
assert_eq!(config.get_value("core", "mode"), Some("archive-z2"));
assert_eq!(config.get_integer("core", "repo_version")?, Some(1));
config.set_string("core", "fsync", "false")?;
assert_eq!(
config.to_string(),
"[core]\nrepo_version=1\nmode=archive-z2\nfsync=false\n",
);Implementations§
Source§impl KeyFile
impl KeyFile
Sourcepub fn parse(input: &str) -> Result<KeyFile>
pub fn parse(input: &str) -> Result<KeyFile>
Parses a key file from text.
The rules are in KeyFile syntax.
§Errors
Error::KeyFile with one of these reasons, after a “line N:” prefix:
- “group header is not closed with ‘]’” if a line starts with
[and has no]. - “group header has trailing text after ‘]’” if text other than white
space follows the first
]. - “empty group name” if the group name is empty.
- “group name ‘NAME’ contains ‘[’ or ‘]’” if the group name contains
a
[. - “expected a key=value pair” if a line is not blank, not a comment,
not a group header, and has no
=. - “empty key” if the text before the
=is empty or only white space. - “key ‘KEY’ precedes any group” if an entry comes before the first group header.
Sourcepub fn group_name(&self, group: &str) -> Option<&str>
pub fn group_name(&self, group: &str) -> Option<&str>
Returns the name of the group group as the key file holds it, or
None if the key file has no such group.
The name is equal to group. It borrows from the key file, so it
lives as long as the key file and not as long as group.
Sourcepub fn groups(&self) -> impl Iterator<Item = &str>
pub fn groups(&self) -> impl Iterator<Item = &str>
Returns an iterator over the group names in file order.
Sourcepub fn keys(&self, group: &str) -> impl Iterator<Item = &str>
pub fn keys(&self, group: &str) -> impl Iterator<Item = &str>
Returns an iterator over the keys of one group in file order.
If the group is absent, the iterator is empty.
Sourcepub fn get_value(&self, group: &str, key: &str) -> Option<&str>
pub fn get_value(&self, group: &str, key: &str) -> Option<&str>
Returns the raw value text of a key.
The text keeps its escape sequences. If the group or the key is absent,
the method returns None.
Sourcepub fn get_string(&self, group: &str, key: &str) -> Result<Option<String>>
pub fn get_string(&self, group: &str, key: &str) -> Result<Option<String>>
Returns the unescaped string value of a key.
The method replaces each escape sequence with its character:
\swith a space\twith a tab\nwith a newline\rwith a carriage return\\with a backslash\;with a;
If the group or the key is absent, the method returns Ok(None).
§Errors
Error::KeyFilewith the reason “invalid escape ‘\X’” if a backslash comes before a characterXthat is not in the list.Error::KeyFilewith the reason “value ends with a lone backslash” if the value ends with a backslash that starts no escape sequence.
Sourcepub fn get_bool(&self, group: &str, key: &str) -> Result<Option<bool>>
pub fn get_bool(&self, group: &str, key: &str) -> Result<Option<bool>>
Returns the boolean value of a key.
The value text true or 1 gives true, and false or 0 gives
false. The match is exact. If the group or the key is absent, the
method returns Ok(None).
§Errors
Error::KeyFile with the reason “value ‘VALUE’ for GROUP.KEY is not a
boolean” if the value text is not one of the four values.
Sourcepub fn get_integer(&self, group: &str, key: &str) -> Result<Option<i64>>
pub fn get_integer(&self, group: &str, key: &str) -> Result<Option<i64>>
Returns the signed integer value of a key.
The value text is a decimal i64 with an optional - sign. If the
group or the key is absent, the method returns Ok(None).
§Errors
Error::KeyFile with the reason “value ‘VALUE’ for GROUP.KEY is not
an integer” if the value text is not a decimal i64. White space in
the text and a leading + sign cause this error.
Sourcepub fn get_string_list(
&self,
group: &str,
key: &str,
) -> Result<Option<Vec<String>>>
pub fn get_string_list( &self, group: &str, key: &str, ) -> Result<Option<Vec<String>>>
Returns the list value of a key as unescaped strings.
A ; separates two items, and \; is a ; in an item. The text after
the last ; is an item only if it is not empty, so a trailing ; adds
no empty item. The method unescapes each item as
get_string does. If the group or the key is
absent, the method returns Ok(None).
§Errors
Error::KeyFilewith the reason “invalid escape ‘\X’” if a backslash comes before a characterXthatget_stringdoes not accept.Error::KeyFilewith the reason “value ends with a lone backslash” if the value ends with a backslash that starts no escape sequence.
Sourcepub fn set_value(&mut self, group: &str, key: &str, value: &str) -> Result<()>
pub fn set_value(&mut self, group: &str, key: &str, value: &str) -> Result<()>
Sets the raw value text of a key.
If the group is absent, the method adds it at the end of the file. If the key is present, the new value replaces the old value at the same position. If the key is absent, the method adds it at the end of the group.
The method does not escape the value.
set_string escapes it.
The method checks the group name, the key, and the value, so that the
Display output parses to an equal KeyFile. If
a check fails, the key file does not change.
§Errors
Error::KeyFile if one of these conditions is true:
- The group name is empty.
- The group name contains
[,], a newline, or a carriage return. - The key is empty.
- The key starts or ends with a space or a tab.
- The key contains
=, a newline, or a carriage return. - The key starts with
#or[. - The value contains a newline or a carriage return.
- The value starts with a space or a tab.
Sourcepub fn set_string(&mut self, group: &str, key: &str, value: &str) -> Result<()>
pub fn set_string(&mut self, group: &str, key: &str, value: &str) -> Result<()>
Sets the string value of a key and escapes the value.
The method escapes the value as the ostree command does when it
writes a value:
- A backslash becomes
\\, a newline\n, and a carriage return\r, at each position in the value. - In the run of white space at the start of the value, each space
becomes
\sand each tab\t. - A space or a tab after that run, and a
;, stay as they are.
The escaped value parses again to the same text, and
get_string returns the original value. The
method places the group and the key as
set_value does. set_value stores a value
that is already escaped.
§Errors
Error::KeyFile if the group name or the key fails a check of
set_value. The escaped value always passes the
checks.
Sourcepub fn set_string_list(
&mut self,
group: &str,
key: &str,
items: &[impl AsRef<str>],
) -> Result<()>
pub fn set_string_list( &mut self, group: &str, key: &str, items: &[impl AsRef<str>], ) -> Result<()>
Sets the list value of a key and escapes each item.
get_string_list returns the same list.
The method escapes each item as set_string
escapes a value, so each space and tab at the start of an item becomes
\s or \t. A ; in an item becomes \;.
A ; follows each item, also the last item. An empty list gives an
empty value. Because the last item always has a separator, an empty
item reads back: ["a", ""] gives a;;, and [""] gives ;.
§Errors
Error::KeyFile if the group name or the key fails a check of
set_value. The escaped value always passes the
checks.
Sourcepub fn remove_key(&mut self, group: &str, key: &str) -> bool
pub fn remove_key(&mut self, group: &str, key: &str) -> bool
Removes one key and returns true if the key was present.
The group stays, also when it loses its last key. The ostree command
keeps the header of an empty group in the same way.
Display writes the header with no entries.
Sourcepub fn remove_group(&mut self, group: &str) -> bool
pub fn remove_group(&mut self, group: &str) -> bool
Removes a group and its keys, and returns true if the group was
present.
The other groups keep their order.
Trait Implementations§
Source§impl Display for KeyFile
Writes the groups and the keys in file order, in the GLib key file layout.
impl Display for KeyFile
Writes the groups and the keys in file order, in the GLib key file layout.
Each group starts with a [name] line, and each entry is a key=value
line. A blank line separates two groups. to_string gives the text of
the whole file.