Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/code cleanup #36

Merged
merged 2 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Small rust crate to interact with systemd units

## Limitations

Currently SystemD Version <245 are not supported as unit-file-list changed from two column to three column setup. See: [SystemD Changelog](https://github.com/systemd/systemd/blob/16bfb12c8f815a468021b6e20871061d20b50f57/NEWS#L6073)
Currently, systemd Version <245 are not supported as unit-file-list changed from two column to three column setup. See: [systemd Changelog](https://github.com/systemd/systemd/blob/16bfb12c8f815a468021b6e20871061d20b50f57/NEWS#L6073)

## Unit / service operation

Expand Down
37 changes: 16 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Crate to manage and monitor services through `systemctl`
//! Crate to manage and monitor services through `systemctl`
//! Homepage: <https://github.com/gwbres/systemctl>
#![doc=include_str!("../README.md")]
use std::io::{Error, ErrorKind};
Expand Down Expand Up @@ -88,14 +88,14 @@ impl SystemCtl {
let size = stdout.len();

if size > 0 {
if let Ok(s) = String::from_utf8(stdout) {
return Ok(s);
return if let Ok(s) = String::from_utf8(stdout) {
Ok(s)
} else {
return Err(Error::new(
Err(Error::new(
ErrorKind::InvalidData,
"Invalid utf8 data in stdout",
));
}
))
};
}

// if this is reached all if's above did not work
Expand Down Expand Up @@ -187,7 +187,7 @@ impl SystemCtl {
Ok(!unit_list.is_empty())
}

/// Returns a `Vector` of `UnitList` structs extracted from systemctl listing.
/// Returns a `Vector` of `UnitList` structs extracted from systemctl listing.
/// + type filter: optional `--type` filter
/// + state filter: optional `--state` filter
/// + glob filter: optional unit name filter
Expand Down Expand Up @@ -232,7 +232,7 @@ impl SystemCtl {
Ok(result)
}

/// Returns a `Vector` of `UnitService` structs extracted from systemctl listing.
/// Returns a `Vector` of `UnitService` structs extracted from systemctl listing.
/// + type filter: optional `--type` filter
/// + state filter: optional `--state` filter
/// + glob filter: optional unit name filter
Expand Down Expand Up @@ -284,7 +284,7 @@ impl SystemCtl {
Ok(result)
}

/// Returns a `Vector` of unit names extracted from systemctl listing.
/// Returns a `Vector` of unit names extracted from systemctl listing.
/// + type filter: optional `--type` filter
/// + state filter: optional `--state` filter
/// + glob filter: optional unit name filter
Expand All @@ -298,7 +298,7 @@ impl SystemCtl {
Ok(list?.iter().map(|n| n.unit_file.clone()).collect())
}

/// Returns a `Vector` of unit names extracted from systemctl listing.
/// Returns a `Vector` of unit names extracted from systemctl listing.
/// + type filter: optional `--type` filter
/// + state filter: optional `--state` filter
/// + glob filter: optional unit name filter
Expand Down Expand Up @@ -374,10 +374,8 @@ impl SystemCtl {
let line = line.strip_suffix(')').unwrap();
let items: Vec<&str> = line.split(';').collect();
u.script = items[0].trim().to_string();
u.auto_start = match AutoStartStatus::from_str(items[1].trim()) {
Ok(x) => x,
Err(_) => AutoStartStatus::Disabled,
};
u.auto_start = AutoStartStatus::from_str(items[1].trim())
.unwrap_or(AutoStartStatus::Disabled);
if items.len() > 2 {
// preset is optionnal ?
u.preset = items[2].trim().ends_with("enabled");
Expand Down Expand Up @@ -611,13 +609,13 @@ impl Doc {
}
}

impl std::str::FromStr for Doc {
type Err = std::io::Error;
impl FromStr for Doc {
type Err = Error;
/// Builds `Doc` from systemd status descriptor
fn from_str(status: &str) -> Result<Self, Self::Err> {
let items: Vec<&str> = status.split(':').collect();
if items.len() != 2 {
return Err(std::io::Error::new(
return Err(Error::new(
ErrorKind::InvalidData,
"malformed doc descriptor",
));
Expand All @@ -629,10 +627,7 @@ impl std::str::FromStr for Doc {
},
"http" => Ok(Doc::Url("http:".to_owned() + items[1].trim())),
"https" => Ok(Doc::Url("https:".to_owned() + items[1].trim())),
_ => Err(std::io::Error::new(
ErrorKind::InvalidData,
"unknown type of doc",
)),
_ => Err(Error::new(ErrorKind::InvalidData, "unknown type of doc")),
}
}
}
Expand Down