pub struct ConfigEdit<'a> { /* private fields */ }Expand description
An edit of the document of config.
The edit changes the document in memory. It does no file I/O. Each write
stores the text of Value::format, and the other keys and groups keep
their order and their text. A write takes each key of a single group,
also a key that is not Settable.
ConfigEdit::new gives an edit of a key file. In the ostrya crate,
UpdateGuard::edit_config gives an edit of the document of config,
and writes the edited document.
Implementations§
Source§impl<'a> ConfigEdit<'a>
impl<'a> ConfigEdit<'a>
Sourcepub fn new(keyfile: &'a mut KeyFile) -> ConfigEdit<'a>
pub fn new(keyfile: &'a mut KeyFile) -> ConfigEdit<'a>
Returns an edit of keyfile.
§Examples
use ostrya_core::KeyFile;
use ostrya_core::config::{ConfigEdit, ConfigError, ConfigView, repo};
let mut kf = KeyFile::parse("[core]\nrepo_version=1\nmode=bare\n")?;
ConfigEdit::new(&mut kf).set::<repo::Fsync>(false)?;
assert_eq!(kf.get_value("core", "fsync"), Some("false"));
assert!(!ConfigView::new(&kf).get::<repo::Fsync>()?);Sourcepub fn set<K>(&mut self, value: ValueOf<K>) -> Result<(), ConfigError>
pub fn set<K>(&mut self, value: ValueOf<K>) -> Result<(), ConfigError>
Sets the key K of a single group to value.
If the key is present, the new value replaces the old value at the same position. If the key is absent, the call adds it at the end of its group. If the group is absent, the call adds it at the end of the document.
§Errors
ConfigError::Core if the text of the value cannot be in a key
file, for example a text with a line break. The document then does not
change.
Sourcepub fn remove<K>(&mut self) -> bool
pub fn remove<K>(&mut self) -> bool
Removes the key K of a single group, and returns true if the key
was present.
The group stays, also when it loses its last key.
Sourcepub fn section_mut<G: Group>(
&mut self,
name: &str,
) -> Result<SectionMut<'_, G>, ConfigError>
pub fn section_mut<G: Group>( &mut self, name: &str, ) -> Result<SectionMut<'_, G>, ConfigError>
Returns the edit of the section name of the named group G.
The call does not add the section. The first
set adds it at the end of the document.
§Errors
ConfigError::InvalidInput if name is empty, or holds [, ],
", or a line break. A key file cannot hold such a group header, and
a read of a section does not take such a name.
Sourcepub fn remove_section<G: Group>(&mut self, name: &str) -> bool
pub fn remove_section<G: Group>(&mut self, name: &str) -> bool
Removes the section name of the named group G with its keys, and
returns true if the section was present.
Sourcepub fn keyfile_mut(&mut self) -> &mut KeyFile
pub fn keyfile_mut(&mut self) -> &mut KeyFile
Returns the document, for an edit that the typed writes do not cover.