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 crate::entry::Entry;
use color_eyre::eyre::Result;
use television_derive::{Broadcast, ToCliChannel, ToUnitChannel}; use television_derive::{Broadcast, ToCliChannel, ToUnitChannel};
mod alias; mod alias;
@ -140,22 +139,9 @@ pub enum TelevisionChannel {
RemoteControl(remote_control::RemoteControl), RemoteControl(remote_control::RemoteControl),
} }
/// NOTE: this could/should be generated by a macro impl From<&Entry> for TelevisionChannel {
impl TryFrom<&Entry> for TelevisionChannel { fn from(entry: &Entry) -> Self {
type Error = String; UnitChannel::from(entry.name.as_str()).into()
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)),
}
} }
} }

View File

@ -312,17 +312,14 @@ impl Television {
.unwrap() .unwrap()
.send(Action::SelectAndExit)?, .send(Action::SelectAndExit)?,
Mode::RemoteControl => { Mode::RemoteControl => {
if let Ok(new_channel) = // FIXME: this is kind of shitty
// FIXME: this is kind of shitty let new_channel = TelevisionChannel::from(&entry);
TelevisionChannel::try_from(&entry) // this resets the RC picker
{ self.reset_picker_selection();
// this resets the RC picker self.reset_picker_input();
self.reset_picker_selection(); self.remote_control.find(EMPTY_STRING);
self.reset_picker_input(); self.mode = Mode::Channel;
self.remote_control.find(EMPTY_STRING); self.change_channel(new_channel);
self.mode = Mode::Channel;
self.change_channel(new_channel);
}
} }
Mode::SendToChannel => { Mode::SendToChannel => {
let new_channel = self let new_channel = self