Expand description
§systemd-unit-edit
A lossless parser and editor for systemd unit files as specified by the systemd.syntax(7) and systemd.unit(5) specifications.
This library preserves all whitespace, comments, and formatting while providing a structured way to read and modify systemd unit files.
§Features
- Lossless parsing: All whitespace, comments, and formatting are preserved
- Systemd unit file support: Full support for the systemd unit file format
- Line continuation support: Handles backslash line continuations
- Comment support: Supports both
#and;style comments - Multiple values: Supports multiple entries with the same key (common in systemd)
§Example
use systemd_unit_edit::SystemdUnit;
use std::str::FromStr;
let input = r#"[Unit]
Description=Test Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/bin/test
"#;
let unit = SystemdUnit::from_str(input).unwrap();
// Read sections
for section in unit.sections() {
println!("Section: {}", section.name().unwrap());
for entry in section.entries() {
println!(" {} = {}", entry.key().unwrap(), entry.value().unwrap());
}
}
// Modify values
let mut service_section = unit.get_section("Service").unwrap();
service_section.set("Type", "forking");
// Add new entries
service_section.add("ExecReload", "/bin/kill -HUP $MAINPID");
// Write back to string
println!("{}", unit);§API
§SystemdUnit
The root type representing a parsed systemd unit file.
SystemdUnit::from_str(text)- Parse from a stringSystemdUnit::from_file(path)- Load from a fileunit.sections()- Iterate over all sectionsunit.get_section(name)- Get a specific section by nameunit.add_section(name)- Add a new sectionunit.text()- Convert back to string (lossless)unit.write_to_file(path)- Write to a file
§Section
Represents a section in a unit file (e.g., [Unit], [Service]).
section.name()- Get the section namesection.entries()- Iterate over all entriessection.get(key)- Get the first value for a keysection.get_all(key)- Get all values for a key (for multi-value keys)section.set(key, value)- Set a value (replaces first occurrence)section.add(key, value)- Add a value (appends even if key exists)section.remove(key)- Remove the first entry with the keysection.remove_all(key)- Remove all entries with the key
§Entry
Represents a key-value entry in a section.
entry.key()- Get the key nameentry.value()- Get the value (with line continuations processed)entry.raw_value()- Get the raw value as it appears in the file
§License
Apache-2.0 A lossless systemd unit file parser and editor.
This library provides a lossless parser for systemd unit files as specified by the systemd.syntax(7) and systemd.unit(5). It preserves all whitespace, comments, and formatting. It is based on the rowan library.
Re-exports§
pub use specifier::SpecifierContext;pub use timespan::parse_timespan;pub use timespan::TimespanParseError;
Modules§
- specifier
- Systemd specifier expansion Systemd specifier expansion support
- systemd_
metadata - Systemd-specific metadata and domain knowledge Systemd-specific metadata and domain knowledge
- timespan
- Systemd time span parsing Systemd time span parsing
Structs§
- Entry
- A key-value entry in a section
- Parse
- The result of parsing: a syntax tree and a collection of errors.
- Parse
Error - List of encountered syntax errors.
- Positioned
Parse Error - A positioned parse error containing location information.
- Section
- A section in a systemd unit file (e.g., [Unit], [Service])
- Systemd
Unit - The root of a systemd unit file
- Text
Range - A range in text, represented as a pair of
TextSize.
Enums§
- Error
- Error parsing systemd unit files
- Lang
- Language definition for rowan
- Syntax
Kind - Token types for systemd unit files