#service #systemd #section #header #pair #key #key-value

bin+lib systemd_service_parser

A simple systemd service file parser

3 releases

0.1.2 Nov 21, 2024
0.1.1 Nov 21, 2024
0.1.0 Nov 21, 2024

#393 in Unix APIs

Download history 329/week @ 2024-11-18 16/week @ 2024-11-25 31/week @ 2024-12-02 10/week @ 2024-12-09

386 downloads per month

MIT/Apache

7KB
87 lines

systemd-service-parser

A simple systemd service file parser written on Rust using pest library which extracts sections and their key-value pairs.

Grammar definition

WHITESPACE = _{ " " | "\t" } - ignores whitespaces

file = { SOI ~ (SECTION | COMMENT | NEWLINE)+ ~ EOI } - The file can consist of section definitions, comments and newlines COMMENT = { "#" ~ (!NEWLINE ~ ANY)* ~ NEWLINE } SECTION = { HEADER ~ NEWLINE+ ~ KEY_VALUE* ~ NEWLINE* } HEADER = { "[" ~ ASCII_ALPHANUMERIC+ ~ "]" } - Section definition KEY_VALUE = { KEY ~ "=" ~ VALUE ~ NEWLINE } - Key-value pairs KEY = { (ASCII_ALPHANUMERIC | "-" | "_")+ } VALUE = { (!NEWLINE ~ ANY)* } NEWLINE = _{ "\r\n" | "\n" }

Example systemd service file

[Unit]
Description=A systemd service.

[Service]
ExecStart=/usr/bin/service

[Install]
WantedBy=multi-user.target

Example usage

cargo run -- test.service

Output:

{
  "sections": [
    {
      "header": "Unit",
      "key_values": [
        {
          "key": "Description",
          "value": "A systemd service."
        }
      ]
    },
    {
      "header": "Service",
      "key_values": [
        {
          "key": "ExecStart",
          "value": "/usr/bin/service"
        }
      ]
    },
    {
      "header": "Install",
      "key_values": [
        {
          "key": "WantedBy",
          "value": "multi-user.target"
        }
      ]
    }
  ]
}

crates.io - https://crates.io/crates/systemd_service_parser docs.rs - https://docs.rs/systemd_service_parser/0.1.0/systemd_service_parser

Dependencies

~2.4–3.5MB
~71K SLoC