refactor(channels): converting between entries and channels is now generic over channels (#25)

This commit is contained in:
Alexandre Pasmantier 2024-11-15 00:05:52 +01:00 committed by GitHub
parent f47b8be9de
commit 4f0daec63d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 28 deletions

View File

@ -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<Self, Self::Error> {
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()
}
}

View File

@ -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