SystemdUnit

Struct SystemdUnit 

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

The root of a systemd unit file

Implementations§

Source§

impl SystemdUnit

Source

pub fn sections(&self) -> impl Iterator<Item = Section>

Get all sections in the file

Source

pub fn get_section(&self, name: &str) -> Option<Section>

Get a specific section by name

Source

pub fn add_section(&mut self, name: &str)

Add a new section to the unit file

Source

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

Get the raw syntax node

Source

pub fn text(&self) -> String

Convert to a string (same as Display::fmt)

Source

pub fn from_file(path: &Path) -> Result<Self, Error>

Load from a file

Source

pub fn write_to_file(&self, path: &Path) -> Result<(), Error>

Write to a file

Source§

impl SystemdUnit

Source

pub fn from_file_with_dropins(path: &Path) -> Result<Self, Error>

Load a unit file with drop-in configuration files merged

This loads the main unit file and then merges all .conf files from the drop-in directory (<unit>.d/). Drop-in files are applied in lexicographic order.

Drop-in directories are searched in the same directory as the unit file. For example, if loading /etc/systemd/system/foo.service, this will look for drop-ins in /etc/systemd/system/foo.service.d/*.conf.

§Example
// Loads foo.service and merges foo.service.d/*.conf
let unit = SystemdUnit::from_file_with_dropins(
    Path::new("/etc/systemd/system/foo.service")
).unwrap();
Source

pub fn merge_dropin(&mut self, dropin: &SystemdUnit)

Merge a drop-in unit file into this unit

This applies the settings from a drop-in file to the current unit. According to systemd behavior:

  • New sections are added
  • Existing keys are replaced with values from the drop-in
  • Multiple values for the same key (e.g., Wants=) are accumulated for directives that support accumulation
§Example
let mut main = SystemdUnit::from_str("[Unit]\nDescription=Main\n").unwrap();
let dropin = SystemdUnit::from_str("[Unit]\nAfter=network.target\n").unwrap();

main.merge_dropin(&dropin);

let section = main.get_section("Unit").unwrap();
assert_eq!(section.get("Description"), Some("Main".to_string()));
assert_eq!(section.get("After"), Some("network.target".to_string()));

Trait Implementations§

Source§

impl AstNode for SystemdUnit

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 SystemdUnit

Source§

fn clone(&self) -> SystemdUnit

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 SystemdUnit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SystemdUnit

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for SystemdUnit

Source§

type Err = Error

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for SystemdUnit

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 SystemdUnit

Source§

fn eq(&self, other: &SystemdUnit) -> 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 SystemdUnit

Source§

impl StructuralPartialEq for SystemdUnit

Auto Trait Implementations§

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.