chore: refactor

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2024-05-05 22:37:19 +02:00
parent 21fc587e6e
commit 74f91a6201
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912

View File

@ -32,8 +32,9 @@ impl GraphItem {
pub fn get(&self, path: &[&str]) -> Option<&GraphItem> { pub fn get(&self, path: &[&str]) -> Option<&GraphItem> {
match path.split_first() { match path.split_first() {
Some((first, rest)) => match self { Some((first, rest)) => match self {
GraphItem::User(u) => u.get(*first)?.get(rest), GraphItem::User(section) | GraphItem::Section(section) => {
GraphItem::Section(s) => s.get(*first)?.get(rest), section.get(*first)?.get(rest)
}
GraphItem::Item { .. } => None, GraphItem::Item { .. } => None,
}, },
None => Some(self), None => Some(self),
@ -43,8 +44,9 @@ impl GraphItem {
pub fn get_mut(&mut self, path: &[&str]) -> Option<&mut GraphItem> { pub fn get_mut(&mut self, path: &[&str]) -> Option<&mut GraphItem> {
match path.split_first() { match path.split_first() {
Some((first, rest)) => match self { Some((first, rest)) => match self {
GraphItem::User(u) => u.get_mut(*first)?.get_mut(rest), GraphItem::User(section) | GraphItem::Section(section) => {
GraphItem::Section(s) => s.get_mut(*first)?.get_mut(rest), section.get_mut(*first)?.get_mut(rest)
}
GraphItem::Item { .. } => None, GraphItem::Item { .. } => None,
}, },
None => Some(self), None => Some(self),
@ -54,20 +56,12 @@ impl GraphItem {
pub fn take(&mut self, path: &[&str]) -> Option<GraphItem> { pub fn take(&mut self, path: &[&str]) -> Option<GraphItem> {
match path.split_first() { match path.split_first() {
Some((first, rest)) => match self { Some((first, rest)) => match self {
GraphItem::User(u) => { GraphItem::User(section) | GraphItem::Section(section) => {
if rest.is_empty() { if rest.is_empty() {
let val = u.remove(*first); let val = section.remove(*first);
val.map(|v| *v) val.map(|v| *v)
} else { } else {
u.get_mut(*first)?.take(rest) section.get_mut(*first)?.take(rest)
}
}
GraphItem::Section(s) => {
if rest.is_empty() {
let val = s.remove(*first);
val.map(|v| *v)
} else {
s.get_mut(*first)?.take(rest)
} }
} }
GraphItem::Item { .. } => None, GraphItem::Item { .. } => None,