pub struct Entry(/* private fields */);Expand description
A key-value entry in a section
Implementations§
Source§impl Entry
impl Entry
Sourcepub fn raw_value(&self) -> Option<String>
pub fn raw_value(&self) -> Option<String>
Get the raw value as it appears in the file (including line continuations)
Sourcepub fn unescape_value(&self) -> Option<String>
pub fn unescape_value(&self) -> Option<String>
Get the value with escape sequences processed
This processes C-style escape sequences as defined in the systemd specification:
\n- newline\t- tab\r- carriage return\\- backslash\"- double quote\'- single quote\xhh- hexadecimal byte (2 digits)\nnn- octal byte (3 digits)\unnnn- Unicode codepoint (4 hex digits)\Unnnnnnnn- Unicode codepoint (8 hex digits)
Sourcepub fn escape_value(value: &str) -> String
pub fn escape_value(value: &str) -> String
Escape a string value for use in systemd unit files
This escapes special characters that need escaping in systemd values:
- backslash (
\) becomes\\ - newline (
\n) becomes\n - tab (
\t) becomes\t - carriage return (
\r) becomes\r - double quote (
") becomes\"
Sourcepub fn is_quoted(&self) -> Option<char>
pub fn is_quoted(&self) -> Option<char>
Check if the value is quoted (starts and ends with matching quotes)
Returns the quote character if the value is quoted, None otherwise.
Systemd supports both double quotes (") and single quotes (').
Sourcepub fn unquoted_value(&self) -> Option<String>
pub fn unquoted_value(&self) -> Option<String>
Get the value with quotes removed (if present)
According to systemd specification, quotes are removed when processing values. This method returns the value with outer quotes stripped if present.
Sourcepub fn quoted_value(&self) -> Option<String>
pub fn quoted_value(&self) -> Option<String>
Get the value with quotes preserved as they appear in the file
This is useful when you want to preserve the exact quoting style.
Sourcepub fn value_as_list(&self) -> Vec<String>
pub fn value_as_list(&self) -> Vec<String>
Parse the value as a space-separated list
Many systemd directives use space-separated lists (e.g., Wants=,
After=, Before=). This method splits the value on whitespace
and returns a vector of strings.
Empty values return an empty vector.
Sourcepub fn value_as_bool(&self) -> Option<bool>
pub fn value_as_bool(&self) -> Option<bool>
Parse the value as a boolean
According to systemd specification, boolean values accept:
- Positive:
1,yes,true,on - Negative:
0,no,false,off
Returns None if the value is not a valid boolean or if the entry has no value.
§Example
let unit = SystemdUnit::from_str("[Service]\nRemainAfterExit=yes\n").unwrap();
let section = unit.get_section("Service").unwrap();
let entry = section.entries().next().unwrap();
assert_eq!(entry.value_as_bool(), Some(true));Sourcepub fn format_bool(value: bool) -> &'static str
pub fn format_bool(value: bool) -> &'static str
Format a boolean value for use in systemd unit files
This converts a boolean to the canonical systemd format:
truebecomes"yes"falsebecomes"no"
§Example
assert_eq!(Entry::format_bool(true), "yes");
assert_eq!(Entry::format_bool(false), "no");Sourcepub fn expand_specifiers(&self, context: &SpecifierContext) -> Option<String>
pub fn expand_specifiers(&self, context: &SpecifierContext) -> Option<String>
Expand systemd specifiers in the value
This replaces systemd specifiers like %i, %u, %h with their
values from the provided context.
§Example
let unit = SystemdUnit::from_str("[Service]\nWorkingDirectory=/var/lib/%i\n").unwrap();
let section = unit.get_section("Service").unwrap();
let entry = section.entries().next().unwrap();
let mut ctx = SpecifierContext::new();
ctx.set("i", "myinstance");
assert_eq!(entry.expand_specifiers(&ctx), Some("/var/lib/myinstance".to_string()));Sourcepub fn set_value(&self, new_value: &str)
pub fn set_value(&self, new_value: &str)
Set a new value for this entry, modifying it in place
This replaces the entry’s value while preserving its key and position in the section. This is useful when iterating over entries and modifying them selectively.
§Example
let input = r#"[Unit]
After=network.target syslog.target
Wants=foo.service
After=remote-fs.target
"#;
let unit = SystemdUnit::from_str(input).unwrap();
let section = unit.get_section("Unit").unwrap();
for entry in section.entries() {
if entry.key().as_deref() == Some("After") {
let values = entry.value_as_list();
let filtered: Vec<_> = values.iter()
.filter(|v| v.as_str() != "syslog.target")
.map(|s| s.as_str())
.collect();
entry.set_value(&filtered.join(" "));
}
}
let section = unit.get_section("Unit").unwrap();
assert_eq!(section.get_all("After"), vec!["network.target", "remote-fs.target"]);Sourcepub fn syntax(&self) -> &SyntaxNode<Lang>
pub fn syntax(&self) -> &SyntaxNode<Lang>
Get the raw syntax node