From 4f0daec63d868e16b1aa0349652ce9480623a496 Mon Sep 17 00:00:00 2001 From: Alexandre Pasmantier <47638216+alexpasmantier@users.noreply.github.com> Date: Fri, 15 Nov 2024 00:05:52 +0100 Subject: [PATCH] refactor(channels): converting between entries and channels is now generic over channels (#25) --- crates/television-channels/src/channels.rs | 20 +++----------------- crates/television/television.rs | 19 ++++++++----------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/crates/television-channels/src/channels.rs b/crates/television-channels/src/channels.rs index e1cff1f..717f6c0 100644 --- a/crates/television-channels/src/channels.rs +++ b/crates/television-channels/src/channels.rs @@ -1,5 +1,4 @@ use crate::entry::Entry; -use color_eyre::eyre::Result; use television_derive::{Broadcast, ToCliChannel, ToUnitChannel}; mod alias; @@ -140,22 +139,9 @@ pub enum TelevisionChannel { RemoteControl(remote_control::RemoteControl), } -/// NOTE: this could/should be generated by a macro -impl TryFrom<&Entry> for TelevisionChannel { - type Error = String; - - fn try_from(entry: &Entry) -> Result { - match entry.name.to_ascii_lowercase().as_ref() { - "env" => Ok(TelevisionChannel::Env(env::Channel::default())), - "files" => Ok(TelevisionChannel::Files(files::Channel::default())), - "gitrepos" => { - Ok(TelevisionChannel::GitRepos(git_repos::Channel::default())) - } - "text" => Ok(TelevisionChannel::Text(text::Channel::default())), - "stdin" => Ok(TelevisionChannel::Stdin(stdin::Channel::default())), - "alias" => Ok(TelevisionChannel::Alias(alias::Channel::default())), - _ => Err(format!("Unknown channel: {}", entry.name)), - } +impl From<&Entry> for TelevisionChannel { + fn from(entry: &Entry) -> Self { + UnitChannel::from(entry.name.as_str()).into() } } diff --git a/crates/television/television.rs b/crates/television/television.rs index 2b10150..cf114c6 100644 --- a/crates/television/television.rs +++ b/crates/television/television.rs @@ -312,17 +312,14 @@ impl Television { .unwrap() .send(Action::SelectAndExit)?, Mode::RemoteControl => { - if let Ok(new_channel) = - // FIXME: this is kind of shitty - TelevisionChannel::try_from(&entry) - { - // this resets the RC picker - self.reset_picker_selection(); - self.reset_picker_input(); - self.remote_control.find(EMPTY_STRING); - self.mode = Mode::Channel; - self.change_channel(new_channel); - } + // FIXME: this is kind of shitty + let new_channel = TelevisionChannel::from(&entry); + // this resets the RC picker + self.reset_picker_selection(); + self.reset_picker_input(); + self.remote_control.find(EMPTY_STRING); + self.mode = Mode::Channel; + self.change_channel(new_channel); } Mode::SendToChannel => { let new_channel = self