refactor: from string impl

Remove redudant impl's, use `.as_str()`
This commit is contained in:
Jack Wills
2023-09-17 18:41:10 +00:00
parent 18c3ed4337
commit ca79893df5
3 changed files with 5 additions and 34 deletions
-26
View File
@@ -18,18 +18,6 @@ const ONE_GB: f64 = ONE_MB * 1000.0;
#[derive(Debug, Clone, Eq, Hash, PartialEq)] #[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub struct ContainerId(String); pub struct ContainerId(String);
impl From<String> for ContainerId {
fn from(x: String) -> Self {
Self(x)
}
}
impl From<&String> for ContainerId {
fn from(x: &String) -> Self {
Self(x.clone())
}
}
impl From<&str> for ContainerId { impl From<&str> for ContainerId {
fn from(x: &str) -> Self { fn from(x: &str) -> Self {
Self(x.to_owned()) Self(x.to_owned())
@@ -156,20 +144,6 @@ impl State {
} }
} }
impl From<String> for State {
fn from(input: String) -> Self {
match input.as_ref() {
"dead" => Self::Dead,
"exited" => Self::Exited,
"paused" => Self::Paused,
"removing" => Self::Removing,
"restarting" => Self::Restarting,
"running" => Self::Running,
_ => Self::Unknown,
}
}
}
impl From<&str> for State { impl From<&str> for State {
fn from(input: &str) -> Self { fn from(input: &str) -> Self {
match input { match input {
+4 -7
View File
@@ -426,7 +426,7 @@ impl AppData {
/// So can display nicely and evenly /// So can display nicely and evenly
pub fn get_width(&self) -> Columns { pub fn get_width(&self) -> Columns {
let mut columns = Columns::new(); let mut columns = Columns::new();
let count = |x: &String| u8::try_from(x.chars().count()).unwrap_or(12); let count = |x: &str| u8::try_from(x.chars().count()).unwrap_or(12);
// Should probably find a refactor here somewhere // Should probably find a refactor here somewhere
for container in &self.containers.items { for container in &self.containers.items {
@@ -552,9 +552,6 @@ impl AppData {
} }
} }
// Trim a &String and return String
let trim_owned = |x: &String| x.trim().to_owned();
for i in all_containers { for i in all_containers {
if let Some(id) = i.id.as_ref() { if let Some(id) = i.id.as_ref() {
let name = i.names.as_mut().map_or(String::new(), |names| { let name = i.names.as_mut().map_or(String::new(), |names| {
@@ -571,15 +568,15 @@ impl AppData {
.as_ref() .as_ref()
.map_or(false, |i| i.starts_with(ENTRY_POINT)); .map_or(false, |i| i.starts_with(ENTRY_POINT));
let state = State::from(i.state.as_ref().map_or("dead".to_owned(), trim_owned)); let state = State::from(i.state.as_ref().map_or("dead", |z| z));
let status = i.status.as_ref().map_or(String::new(), trim_owned); let status = i.status.as_ref().map_or(String::new(), std::clone::Clone::clone);
let image = i let image = i
.image .image
.as_ref() .as_ref()
.map_or(String::new(), std::clone::Clone::clone); .map_or(String::new(), std::clone::Clone::clone);
let id = ContainerId::from(id); let id = ContainerId::from(id.as_str());
let created = i let created = i
.created .created
+1 -1
View File
@@ -216,7 +216,7 @@ impl DockerData {
( (
i.state == Some("running".to_owned()) i.state == Some("running".to_owned())
|| i.state == Some("restarting".to_owned()), || i.state == Some("restarting".to_owned()),
ContainerId::from(id), ContainerId::from(id.as_str()),
) )
}) })
}) })