Skip to main content

Entry

Struct Entry 

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

A key-value entry in a section

Implementations§

Source§

impl Entry

Source

pub fn new(key: &str, value: &str) -> Entry

Create a new entry with key=value

Source

pub fn key(&self) -> Option<String>

Get the key name

Source

pub fn value(&self) -> Option<String>

Get the value (handles line continuations)

Source

pub fn raw_value(&self) -> Option<String>

Get the raw value as it appears in the file (including line continuations)

Source

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)
Source

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 \"
Source

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 (').

Source

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.

Source

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.

Source

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.

Source

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));
Source

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:

  • true becomes "yes"
  • false becomes "no"
§Example
assert_eq!(Entry::format_bool(true), "yes");
assert_eq!(Entry::format_bool(false), "no");
Source

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()));
Source

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"]);
Source

pub fn syntax(&self) -> &SyntaxNode<Lang>

Get the raw syntax node

Source

pub fn line(&self) -> usize

Get the line number (0-indexed) where this node starts.

Source

pub fn column(&self) -> usize

Get the column number (0-indexed, in bytes) where this node starts.

Source

pub fn line_col(&self) -> (usize, usize)

Get both line and column (0-indexed) where this node starts. Returns (line, column) where column is measured in bytes from the start of the line.

Trait Implementations§

Source§

impl AstNode for Entry

Source§

type Language = Lang

Source§

fn can_cast(kind: SyntaxKind) -> bool

Source§

fn cast(node: SyntaxNode<Lang>) -> Option<Self>

Source§

fn syntax(&self) -> &SyntaxNode<Lang>

Source§

fn clone_for_update(&self) -> Self
where Self: Sized,

Source§

fn clone_subtree(&self) -> Self
where Self: Sized,

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 Hash for Entry

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. 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 UnsafeUnpin 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<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.