mirror of
https://github.com/alexpasmantier/television.git
synced 2025-06-06 19:45:23 +00:00
refactor(cable): improve naming and documentation for prototypes.rs
(#487)
This commit is contained in:
parent
4385317e06
commit
e2f52b835d
@ -12,7 +12,7 @@ use crate::{
|
|||||||
|
|
||||||
/// Just a proxy struct to deserialize prototypes
|
/// Just a proxy struct to deserialize prototypes
|
||||||
#[derive(Debug, serde::Deserialize, Default)]
|
#[derive(Debug, serde::Deserialize, Default)]
|
||||||
pub struct ChannelPrototypes {
|
pub struct SerializedChannelPrototypes {
|
||||||
#[serde(rename = "cable_channel")]
|
#[serde(rename = "cable_channel")]
|
||||||
pub prototypes: Vec<CableChannelPrototype>,
|
pub prototypes: Vec<CableChannelPrototype>,
|
||||||
}
|
}
|
||||||
@ -58,13 +58,13 @@ pub fn load_cable_channels() -> Result<CableChannels> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let default_prototypes =
|
let default_prototypes =
|
||||||
toml::from_str::<ChannelPrototypes>(DEFAULT_CABLE_CHANNELS)
|
toml::from_str::<SerializedChannelPrototypes>(DEFAULT_CABLE_CHANNELS)
|
||||||
.expect("Failed to parse default cable channels");
|
.expect("Failed to parse default cable channels");
|
||||||
|
|
||||||
let prototypes = file_paths.iter().fold(
|
let prototypes = file_paths.iter().fold(
|
||||||
Vec::<CableChannelPrototype>::new(),
|
Vec::<CableChannelPrototype>::new(),
|
||||||
|mut acc, p| {
|
|mut acc, p| {
|
||||||
match toml::from_str::<ChannelPrototypes>(
|
match toml::from_str::<SerializedChannelPrototypes>(
|
||||||
&std::fs::read_to_string(p)
|
&std::fs::read_to_string(p)
|
||||||
.expect("Unable to read configuration file"),
|
.expect("Unable to read configuration file"),
|
||||||
) {
|
) {
|
||||||
|
@ -4,8 +4,36 @@ use std::{
|
|||||||
ops::Deref,
|
ops::Deref,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::cable::ChannelPrototypes;
|
use crate::cable::SerializedChannelPrototypes;
|
||||||
|
|
||||||
|
/// A prototype for a cable channel.
|
||||||
|
///
|
||||||
|
/// This can be seen as a cable channel specification, which is used to
|
||||||
|
/// create a cable channel.
|
||||||
|
///
|
||||||
|
/// The prototype contains the following fields:
|
||||||
|
/// - `name`: The name of the channel. This will be used to identify the
|
||||||
|
/// channel throughout the application and in UI menus.
|
||||||
|
/// - `source_command`: The command to run to get the source for the channel.
|
||||||
|
/// This is a shell command that will be run in the background.
|
||||||
|
/// - `interactive`: Whether the source command should be run in an interactive
|
||||||
|
/// shell. This is useful for commands that need the user's environment e.g.
|
||||||
|
/// `alias`.
|
||||||
|
/// - `preview_command`: The command to run on each entry to get the preview
|
||||||
|
/// for the channel. If this is not `None`, the channel will display a preview
|
||||||
|
/// pane with the output of this command.
|
||||||
|
/// - `preview_delimiter`: The delimiter to use to split an entry into
|
||||||
|
/// multiple parts that can then be referenced in the preview command (e.g.
|
||||||
|
/// `{1} + {2}`).
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
/// The default files channel might look something like this:
|
||||||
|
/// ```toml
|
||||||
|
/// [[cable_channel]]
|
||||||
|
/// name = "files"
|
||||||
|
/// source_command = "fd -t f"
|
||||||
|
/// preview_command = ":files:"
|
||||||
|
/// ```
|
||||||
#[derive(Clone, Debug, serde::Deserialize, PartialEq)]
|
#[derive(Clone, Debug, serde::Deserialize, PartialEq)]
|
||||||
pub struct CableChannelPrototype {
|
pub struct CableChannelPrototype {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
@ -38,6 +66,7 @@ impl CableChannelPrototype {
|
|||||||
const DEFAULT_PROTOTYPE_NAME: &str = "files";
|
const DEFAULT_PROTOTYPE_NAME: &str = "files";
|
||||||
const DEFAULT_SOURCE_COMMAND: &str = "fd -t f";
|
const DEFAULT_SOURCE_COMMAND: &str = "fd -t f";
|
||||||
const DEFAULT_PREVIEW_COMMAND: &str = ":files:";
|
const DEFAULT_PREVIEW_COMMAND: &str = ":files:";
|
||||||
|
pub const DEFAULT_DELIMITER: &str = " ";
|
||||||
|
|
||||||
impl Default for CableChannelPrototype {
|
impl Default for CableChannelPrototype {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
@ -51,8 +80,6 @@ impl Default for CableChannelPrototype {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const DEFAULT_DELIMITER: &str = " ";
|
|
||||||
|
|
||||||
#[allow(clippy::unnecessary_wraps)]
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
fn default_delimiter() -> Option<String> {
|
fn default_delimiter() -> Option<String> {
|
||||||
Some(DEFAULT_DELIMITER.to_string())
|
Some(DEFAULT_DELIMITER.to_string())
|
||||||
@ -64,6 +91,11 @@ impl Display for CableChannelPrototype {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A neat `HashMap` of cable channel prototypes indexed by their name.
|
||||||
|
///
|
||||||
|
/// This is used to store cable channel prototypes throughout the application
|
||||||
|
/// in a way that facilitates answering questions like "what's the prototype
|
||||||
|
/// for `files`?" or "does this channel exist?".
|
||||||
#[derive(Debug, serde::Deserialize)]
|
#[derive(Debug, serde::Deserialize)]
|
||||||
pub struct CableChannels(pub FxHashMap<String, CableChannelPrototype>);
|
pub struct CableChannels(pub FxHashMap<String, CableChannelPrototype>);
|
||||||
|
|
||||||
@ -86,8 +118,9 @@ impl Default for CableChannels {
|
|||||||
/// Fallback to the default cable channels specification (the template file
|
/// Fallback to the default cable channels specification (the template file
|
||||||
/// included in the repo).
|
/// included in the repo).
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let pts =
|
let pts = toml::from_str::<SerializedChannelPrototypes>(
|
||||||
toml::from_str::<ChannelPrototypes>(DEFAULT_CABLE_CHANNELS_FILE)
|
DEFAULT_CABLE_CHANNELS_FILE,
|
||||||
|
)
|
||||||
.expect("Unable to parse default cable channels");
|
.expect("Unable to parse default cable channels");
|
||||||
let mut channels = FxHashMap::default();
|
let mut channels = FxHashMap::default();
|
||||||
for prototype in pts.prototypes {
|
for prototype in pts.prototypes {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user