Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 01d16aaf rédigé par Kubat's avatar Kubat
Parcourir les fichiers

RUST: Move the priority level definition from lkt-rs to the amalib

parent e12f15dc
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
......@@ -16,6 +16,49 @@ pub use uri::*;
pub(crate) use commons::{log::*, *};
pub(crate) use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LektorPriorityLevel {
Add = 1,
Suggest = 2,
Insert = 3,
Pick = 4,
Enforce = 5,
}
impl std::str::FromStr for LektorPriorityLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use LektorPriorityLevel::*;
Ok(match s {
"add" | "1" => Add,
"suggest" | "2" => Suggest,
"insert" | "3" => Insert,
"pick" | "4" => Pick,
"enforce" | "5" => Enforce,
_ => return Err(format!("unknown priority level: {s}")),
})
}
}
impl Default for LektorPriorityLevel {
fn default() -> Self {
LektorPriorityLevel::Add
}
}
impl std::fmt::Display for LektorPriorityLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
LektorPriorityLevel::Add => "1",
LektorPriorityLevel::Suggest => "2",
LektorPriorityLevel::Insert => "3",
LektorPriorityLevel::Pick => "4",
LektorPriorityLevel::Enforce => "5",
})
}
}
/// Whever the query uri should be only the direct query or must include the
/// first links.
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
......
use crate::{config::LktConfig, types::*};
use crate::config::LktConfig;
use amalib::LektorPriorityLevel;
use clap::{Parser, Subcommand};
use std::ops::Range;
......@@ -116,12 +117,11 @@ enum SubCommand {
, short = 'c'
, long = "clear"
, id = "CLEAR_LVL"
, default_missing_value = PriorityLevel::Enforce
, help = concat!( "Clear the queue and set the state to stopped. "
, "You can also pass a level to clear up to. The level can be passed as a numeric or as its string representation. "
, "[default value: 5, possible values: 1..5]"
))]
clear: Option<Option<PriorityLevel>>,
clear: Option<Option<LektorPriorityLevel>>,
#[arg( action = clap::ArgAction::Set
, exclusive = true
......@@ -147,13 +147,12 @@ enum SubCommand {
, short = 'z'
, long = "shuffle"
, id = "SHUFFLE_LVL"
, default_missing_value = PriorityLevel::Enforce
, help = concat!( "Shuffle the queue. If lektord was paused it will unpause but if it was stopped it won't start. "
, "If it exists, the current kara will be placed in the first place in the queue. "
, "You can also pass a level to shuffle up to. The level can be passed as a numeric or as its string representation. "
, "[default value: 5, possible values: 1..5]"
))]
shuffle: Option<Option<PriorityLevel>>,
shuffle: Option<Option<LektorPriorityLevel>>,
},
// Search commands
......@@ -345,13 +344,13 @@ pub enum LktQueueCommand {
},
Add {
priority: PriorityLevel,
priority: LektorPriorityLevel,
query: amalib::LektorUri,
},
Crop,
Clear {
up_to_lvl: PriorityLevel,
up_to_lvl: LektorPriorityLevel,
},
SwapPositions {
......@@ -364,7 +363,7 @@ pub enum LktQueueCommand {
},
Shuffle {
up_to_lvl: PriorityLevel,
up_to_lvl: LektorPriorityLevel,
},
}
......@@ -454,8 +453,8 @@ impl LktCommand {
SubCommand::Queue { pause: Some(None), .. } => Queue(LktQueueCommand::TogglePause),
SubCommand::Queue { pause: Some(Some(true)), .. } => Queue(LktQueueCommand::Pause),
SubCommand::Queue { pause: Some(Some(false)), .. } => Queue(LktQueueCommand::UnPause),
SubCommand::Queue { shuffle: Some(lvl), .. } => Queue(LktQueueCommand::Shuffle { up_to_lvl: lvl.unwrap_or(PriorityLevel::Enforce) }),
SubCommand::Queue { clear: Some(lvl), .. } => Queue(LktQueueCommand::Clear { up_to_lvl: lvl.unwrap_or(PriorityLevel::Enforce) }),
SubCommand::Queue { shuffle: Some(lvl), .. } => Queue(LktQueueCommand::Shuffle { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }),
SubCommand::Queue { clear: Some(lvl), .. } => Queue(LktQueueCommand::Clear { up_to_lvl: lvl.unwrap_or(LektorPriorityLevel::Enforce) }),
SubCommand::Queue { seek: Some(id), .. } => { Queue(LktQueueCommand::SeekIdInQueue { id }) }
SubCommand::Queue { pos: Some(None), .. } => Queue(LktQueueCommand::List { range: 0..config.search.default_queue_count }),
SubCommand::Queue { pos: Some(Some(range)), .. } => Queue(LktQueueCommand::List { range }),
......@@ -469,13 +468,13 @@ impl LktCommand {
}
SubCommand::Queue { add: Some(args), .. } => match &args[..] {
[] => sterr!("invalid queue add command, expected `priority query...` or `query...`, got nothing"),
[priority, query @ .. ] if !query.is_empty() && priority.parse::<PriorityLevel>().is_ok() => Queue(LktQueueCommand::Add {
[priority, query @ .. ] if !query.is_empty() && priority.parse::<LektorPriorityLevel>().is_ok() => Queue(LktQueueCommand::Add {
priority: priority.parse().expect("something went wrong"),
query: amalib::LektorUriBuilder::default()
.query_type(config.search.query_type).strings(query.iter().cloned()).try_build()
.map_err(|e| format!("invalid query in queue add command: {e}"))?
}),
query => Queue(LktQueueCommand::Add { priority: PriorityLevel::Add,
query => Queue(LktQueueCommand::Add { priority: LektorPriorityLevel::Add,
query: amalib::LektorUriBuilder::default()
.query_type(config.search.query_type).strings(query.iter().cloned()).try_build()
.map_err(|e| format!("invalid query in queue add command: {e}"))?
......
mod args;
mod config;
mod parsers;
mod types;
use crate::{args::*, config::LktConfig};
use amalib::{LektorQuery, LektorResponse};
......
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PriorityLevel {
Add = 1,
Suggest = 2,
Insert = 3,
Pick = 4,
Enforce = 5,
}
impl std::str::FromStr for PriorityLevel {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use PriorityLevel::*;
Ok(match s {
"add" | "1" => Add,
"suggest" | "2" => Suggest,
"insert" | "3" => Insert,
"pick" | "4" => Pick,
"enforce" | "5" => Enforce,
_ => return Err(format!("unknown priority level: {s}")),
})
}
}
impl From<&str> for PriorityLevel {
fn from(value: &str) -> Self {
PriorityLevel::from_str(value).expect("conversion failed")
}
}
impl Default for PriorityLevel {
fn default() -> Self {
PriorityLevel::Add
}
}
impl std::fmt::Display for PriorityLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(match self {
PriorityLevel::Add => "1",
PriorityLevel::Suggest => "2",
PriorityLevel::Insert => "3",
PriorityLevel::Pick => "4",
PriorityLevel::Enforce => "5",
})
}
}
impl From<clap::builder::OsStr> for PriorityLevel {
fn from(value: clap::builder::OsStr) -> Self {
let value = value
.to_str()
.unwrap_or_else(|| panic!("invalid utf8 string: {}", value.to_string_lossy()));
PriorityLevel::from(value)
}
}
impl From<PriorityLevel> for clap::builder::OsStr {
fn from(val: PriorityLevel) -> Self {
clap::builder::OsStr::from(match val {
PriorityLevel::Add => "1",
PriorityLevel::Suggest => "2",
PriorityLevel::Insert => "3",
PriorityLevel::Pick => "4",
PriorityLevel::Enforce => "5",
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterPlaylist {
playlist: String,
query: String,
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter