| Crates.io | systemd-unit-edit |
| lib.rs | systemd-unit-edit |
| version | 0.1.3 |
| created_at | 2025-11-25 22:37:30.753316+00 |
| updated_at | 2025-12-08 02:05:08.001704+00 |
| description | A lossless parser and editor for systemd unit files |
| homepage | https://github.com/jelmer/systemd-unit-edit |
| repository | |
| max_upload_size | |
| id | 1950574 |
| size | 144,927 |
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.
# and ; style commentsuse 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);
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 fileRepresents 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 keyRepresents 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 fileApache-2.0