style: warn enum_glob_use

This commit is contained in:
cyqsimon
2024-04-14 19:36:50 +08:00
committed by GitHub
parent ed98d0f843
commit 7f882ccaee
3 changed files with 47 additions and 45 deletions

View File

@@ -20,6 +20,7 @@ members = [
] ]
[workspace.lints.clippy] [workspace.lints.clippy]
enum_glob_use = "warn"
use_self = "deny" use_self = "deny"
multiple_inherent_impl = "deny" multiple_inherent_impl = "deny"

View File

@@ -339,17 +339,16 @@ pub enum CommandParseError {
impl fmt::Display for CommandParseError { impl fmt::Display for CommandParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use CommandParseError::*;
let formatted = match self { let formatted = match self {
NoSuchCommand { cmd } => format!("No such command \"{cmd}\""), Self::NoSuchCommand { cmd } => format!("No such command \"{cmd}\""),
InsufficientArgs { cmd, hint } => { Self::InsufficientArgs { cmd, hint } => {
if let Some(hint_str) = hint { if let Some(hint_str) = hint {
format!("\"{cmd}\" requires additional arguments: {hint_str}") format!("\"{cmd}\" requires additional arguments: {hint_str}")
} else { } else {
format!("\"{cmd}\" requires additional arguments") format!("\"{cmd}\" requires additional arguments")
} }
} }
BadEnumArg { Self::BadEnumArg {
arg, arg,
accept, accept,
optional, optional,
@@ -361,7 +360,7 @@ impl fmt::Display for CommandParseError {
format!("Argument \"{arg}\" should be one of {accept}") format!("Argument \"{arg}\" should be one of {accept}")
} }
} }
ArgParseError { arg, err } => format!("Error with argument \"{arg}\": {err}"), Self::ArgParseError { arg, err } => format!("Error with argument \"{arg}\": {err}"),
}; };
write!(f, "{formatted}") write!(f, "{formatted}")
} }
@@ -399,7 +398,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
if let Some((command, args)) = components.split_first() { if let Some((command, args)) = components.split_first() {
let command = handle_aliases(command); let command = handle_aliases(command);
use CommandParseError::*; use CommandParseError as E;
let command = match command { let command = match command {
"quit" => Command::Quit, "quit" => Command::Quit,
"playpause" => Command::TogglePlay, "playpause" => Command::TogglePlay,
@@ -413,7 +412,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"update" => Command::UpdateLibrary, "update" => Command::UpdateLibrary,
"add" => match args.first().cloned() { "add" => match args.first().cloned() {
Some("current") => Ok(Command::AddCurrent), Some("current") => Ok(Command::AddCurrent),
Some(arg) => Err(BadEnumArg { Some(arg) => Err(E::BadEnumArg {
arg: arg.into(), arg: arg.into(),
accept: vec!["current".into()], accept: vec!["current".into()],
optional: true, optional: true,
@@ -423,7 +422,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"save" => match args.first().cloned() { "save" => match args.first().cloned() {
Some("queue") => Ok(Command::SaveQueue), Some("queue") => Ok(Command::SaveQueue),
Some("current") => Ok(Command::SaveCurrent), Some("current") => Ok(Command::SaveCurrent),
Some(arg) => Err(BadEnumArg { Some(arg) => Err(E::BadEnumArg {
arg: arg.into(), arg: arg.into(),
accept: vec!["queue".into(), "current".into()], accept: vec!["queue".into(), "current".into()],
optional: true, optional: true,
@@ -432,7 +431,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
}?, }?,
"delete" => Command::Delete, "delete" => Command::Delete,
"focus" => { "focus" => {
let &target = args.first().ok_or(InsufficientArgs { let &target = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("queue|search|library".into()), hint: Some("queue|search|library".into()),
})?; })?;
@@ -441,7 +440,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
} }
"seek" => { "seek" => {
if args.is_empty() { if args.is_empty() {
return Err(InsufficientArgs { return Err(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("a duration".into()), hint: Some("a duration".into()),
}); });
@@ -460,12 +459,12 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
// accept raw milliseconds // accept raw milliseconds
Ok(millis) => millis, Ok(millis) => millis,
Err(_) => parse_duration::parse(&duration_raw) // accept fancy duration Err(_) => parse_duration::parse(&duration_raw) // accept fancy duration
.map_err(|err| ArgParseError { .map_err(|err| E::ArgParseError {
arg: duration_raw.clone(), arg: duration_raw.clone(),
err: err.to_string(), err: err.to_string(),
}) })
.and_then(|dur| { .and_then(|dur| {
dur.as_millis().try_into().map_err(|_| ArgParseError { dur.as_millis().try_into().map_err(|_| E::ArgParseError {
arg: duration_raw.clone(), arg: duration_raw.clone(),
err: "Duration value too large".into(), err: "Duration value too large".into(),
}) })
@@ -478,7 +477,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
.map(|millis| SeekDirection::Relative(-millis)), .map(|millis| SeekDirection::Relative(-millis)),
_ => Ok(SeekDirection::Absolute(unsigned_millis)), _ => Ok(SeekDirection::Absolute(unsigned_millis)),
} }
.map_err(|_| ArgParseError { .map_err(|_| E::ArgParseError {
arg: duration_raw, arg: duration_raw,
err: "Duration value too large".into(), err: "Duration value too large".into(),
})?; })?;
@@ -487,7 +486,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"volup" => { "volup" => {
let amount = match args.first() { let amount = match args.first() {
Some(&amount_raw) => { Some(&amount_raw) => {
amount_raw.parse::<u16>().map_err(|err| ArgParseError { amount_raw.parse::<u16>().map_err(|err| E::ArgParseError {
arg: amount_raw.into(), arg: amount_raw.into(),
err: err.to_string(), err: err.to_string(),
})? })?
@@ -499,7 +498,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"voldown" => { "voldown" => {
let amount = match args.first() { let amount = match args.first() {
Some(&amount_raw) => { Some(&amount_raw) => {
amount_raw.parse::<u16>().map_err(|err| ArgParseError { amount_raw.parse::<u16>().map_err(|err| E::ArgParseError {
arg: amount_raw.into(), arg: amount_raw.into(),
err: err.to_string(), err: err.to_string(),
})? })?
@@ -515,7 +514,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
} }
Some("track" | "once" | "single") => Ok(Some(RepeatSetting::RepeatTrack)), Some("track" | "once" | "single") => Ok(Some(RepeatSetting::RepeatTrack)),
Some("none" | "off") => Ok(Some(RepeatSetting::None)), Some("none" | "off") => Ok(Some(RepeatSetting::None)),
Some(arg) => Err(BadEnumArg { Some(arg) => Err(E::BadEnumArg {
arg: arg.into(), arg: arg.into(),
accept: vec![ accept: vec![
"list".into(), "list".into(),
@@ -537,7 +536,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
let switch = match args.first().cloned() { let switch = match args.first().cloned() {
Some("on") => Ok(Some(true)), Some("on") => Ok(Some(true)),
Some("off") => Ok(Some(false)), Some("off") => Ok(Some(false)),
Some(arg) => Err(BadEnumArg { Some(arg) => Err(E::BadEnumArg {
arg: arg.into(), arg: arg.into(),
accept: vec!["on".into(), "off".into()], accept: vec!["on".into(), "off".into()],
optional: true, optional: true,
@@ -548,14 +547,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
} }
#[cfg(feature = "share_clipboard")] #[cfg(feature = "share_clipboard")]
"share" => { "share" => {
let &target_mode_raw = args.first().ok_or(InsufficientArgs { let &target_mode_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("selected|current".into()), hint: Some("selected|current".into()),
})?; })?;
let target_mode = match target_mode_raw { let target_mode = match target_mode_raw {
"selected" => Ok(TargetMode::Selected), "selected" => Ok(TargetMode::Selected),
"current" => Ok(TargetMode::Current), "current" => Ok(TargetMode::Current),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: target_mode_raw.into(), arg: target_mode_raw.into(),
accept: vec!["selected".into(), "current".into()], accept: vec!["selected".into(), "current".into()],
optional: false, optional: false,
@@ -565,14 +564,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
} }
"back" => Command::Back, "back" => Command::Back,
"open" => { "open" => {
let &target_mode_raw = args.first().ok_or(InsufficientArgs { let &target_mode_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("selected|current".into()), hint: Some("selected|current".into()),
})?; })?;
let target_mode = match target_mode_raw { let target_mode = match target_mode_raw {
"selected" => Ok(TargetMode::Selected), "selected" => Ok(TargetMode::Selected),
"current" => Ok(TargetMode::Current), "current" => Ok(TargetMode::Current),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: target_mode_raw.into(), arg: target_mode_raw.into(),
accept: vec!["selected".into(), "current".into()], accept: vec!["selected".into(), "current".into()],
optional: false, optional: false,
@@ -581,14 +580,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Command::Open(target_mode) Command::Open(target_mode)
} }
"goto" => { "goto" => {
let &goto_mode_raw = args.first().ok_or(InsufficientArgs { let &goto_mode_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("album|artist".into()), hint: Some("album|artist".into()),
})?; })?;
let goto_mode = match goto_mode_raw { let goto_mode = match goto_mode_raw {
"album" => Ok(GotoMode::Album), "album" => Ok(GotoMode::Album),
"artist" => Ok(GotoMode::Artist), "artist" => Ok(GotoMode::Artist),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: goto_mode_raw.into(), arg: goto_mode_raw.into(),
accept: vec!["album".into(), "artist".into()], accept: vec!["album".into(), "artist".into()],
optional: false, optional: false,
@@ -597,19 +596,19 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Command::Goto(goto_mode) Command::Goto(goto_mode)
} }
"move" => { "move" => {
let &move_mode_raw = args.first().ok_or(InsufficientArgs { let &move_mode_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("a direction".into()), hint: Some("a direction".into()),
})?; })?;
let move_mode = { let move_mode = {
use MoveMode::*; use MoveMode as M;
match move_mode_raw { match move_mode_raw {
"playing" => Ok(Playing), "playing" => Ok(M::Playing),
"top" | "pageup" | "up" => Ok(Up), "top" | "pageup" | "up" => Ok(M::Up),
"bottom" | "pagedown" | "down" => Ok(Down), "bottom" | "pagedown" | "down" => Ok(M::Down),
"leftmost" | "pageleft" | "left" => Ok(Left), "leftmost" | "pageleft" | "left" => Ok(M::Left),
"rightmost" | "pageright" | "right" => Ok(Right), "rightmost" | "pageright" | "right" => Ok(M::Right),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: move_mode_raw.into(), arg: move_mode_raw.into(),
accept: vec![ accept: vec![
"playing".into(), "playing".into(),
@@ -638,7 +637,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Some(&amount_raw) => amount_raw Some(&amount_raw) => amount_raw
.parse::<f32>() .parse::<f32>()
.map(MoveAmount::Float) .map(MoveAmount::Float)
.map_err(|err| ArgParseError { .map_err(|err| E::ArgParseError {
arg: amount_raw.into(), arg: amount_raw.into(),
err: err.to_string(), err: err.to_string(),
})?, })?,
@@ -651,7 +650,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Some(&amount_raw) => amount_raw Some(&amount_raw) => amount_raw
.parse::<i32>() .parse::<i32>()
.map(MoveAmount::Integer) .map(MoveAmount::Integer)
.map_err(|err| ArgParseError { .map_err(|err| E::ArgParseError {
arg: amount_raw.into(), arg: amount_raw.into(),
err: err.to_string(), err: err.to_string(),
})?, })?,
@@ -664,14 +663,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Command::Move(move_mode, move_amount) Command::Move(move_mode, move_amount)
} }
"shift" => { "shift" => {
let &shift_dir_raw = args.first().ok_or(InsufficientArgs { let &shift_dir_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("up|down".into()), hint: Some("up|down".into()),
})?; })?;
let shift_dir = match shift_dir_raw { let shift_dir = match shift_dir_raw {
"up" => Ok(ShiftMode::Up), "up" => Ok(ShiftMode::Up),
"down" => Ok(ShiftMode::Down), "down" => Ok(ShiftMode::Down),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: shift_dir_raw.into(), arg: shift_dir_raw.into(),
accept: vec!["up".into(), "down".into()], accept: vec!["up".into(), "down".into()],
optional: false, optional: false,
@@ -680,7 +679,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
let amount = match args.get(1) { let amount = match args.get(1) {
Some(&amount_raw) => { Some(&amount_raw) => {
let amount = let amount =
amount_raw.parse::<i32>().map_err(|err| ArgParseError { amount_raw.parse::<i32>().map_err(|err| E::ArgParseError {
arg: amount_raw.into(), arg: amount_raw.into(),
err: err.to_string(), err: err.to_string(),
})?; })?;
@@ -703,12 +702,12 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
Some("") | None => Ok(InsertSource::Clipboard), Some("") | None => Ok(InsertSource::Clipboard),
// if clipboard feature is disabled and args is empty // if clipboard feature is disabled and args is empty
#[cfg(not(feature = "share_clipboard"))] #[cfg(not(feature = "share_clipboard"))]
None => Err(InsufficientArgs { None => Err(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("a Spotify URL".into()), hint: Some("a Spotify URL".into()),
}), }),
Some(url) => SpotifyUrl::from_url(url).map(InsertSource::Input).ok_or( Some(url) => SpotifyUrl::from_url(url).map(InsertSource::Input).ok_or(
ArgParseError { E::ArgParseError {
arg: url.into(), arg: url.into(),
err: "Invalid Spotify URL".into(), err: "Invalid Spotify URL".into(),
}, },
@@ -720,14 +719,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
if !args.is_empty() { if !args.is_empty() {
Ok(Command::NewPlaylist(args.join(" "))) Ok(Command::NewPlaylist(args.join(" ")))
} else { } else {
Err(InsufficientArgs { Err(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("a name".into()), hint: Some("a name".into()),
}) })
}? }?
} }
"sort" => { "sort" => {
let &key_raw = args.first().ok_or(InsufficientArgs { let &key_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("a sort key".into()), hint: Some("a sort key".into()),
})?; })?;
@@ -737,7 +736,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"album" => Ok(SortKey::Album), "album" => Ok(SortKey::Album),
"added" => Ok(SortKey::Added), "added" => Ok(SortKey::Added),
"artist" => Ok(SortKey::Artist), "artist" => Ok(SortKey::Artist),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: key_raw.into(), arg: key_raw.into(),
accept: vec![ accept: vec![
"title".into(), "title".into(),
@@ -752,7 +751,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
let direction = match args.get(1).copied() { let direction = match args.get(1).copied() {
Some("a" | "asc" | "ascending") => Ok(SortDirection::Ascending), Some("a" | "asc" | "ascending") => Ok(SortDirection::Ascending),
Some("d" | "desc" | "descending") => Ok(SortDirection::Descending), Some("d" | "desc" | "descending") => Ok(SortDirection::Descending),
Some(direction_raw) => Err(BadEnumArg { Some(direction_raw) => Err(E::BadEnumArg {
arg: direction_raw.into(), arg: direction_raw.into(),
accept: vec![ accept: vec![
"a".into(), "a".into(),
@@ -770,14 +769,14 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
} }
"logout" => Command::Logout, "logout" => Command::Logout,
"similar" => { "similar" => {
let &target_mode_raw = args.first().ok_or(InsufficientArgs { let &target_mode_raw = args.first().ok_or(E::InsufficientArgs {
cmd: command.into(), cmd: command.into(),
hint: Some("selected|current".into()), hint: Some("selected|current".into()),
})?; })?;
let target_mode = match target_mode_raw { let target_mode = match target_mode_raw {
"selected" => Ok(TargetMode::Selected), "selected" => Ok(TargetMode::Selected),
"current" => Ok(TargetMode::Current), "current" => Ok(TargetMode::Current),
_ => Err(BadEnumArg { _ => Err(E::BadEnumArg {
arg: target_mode_raw.into(), arg: target_mode_raw.into(),
accept: vec!["selected".into(), "current".into()], accept: vec!["selected".into(), "current".into()],
optional: false, optional: false,
@@ -789,7 +788,7 @@ pub fn parse(input: &str) -> Result<Vec<Command>, CommandParseError> {
"exec" => Command::Execute(args.join(" ")), "exec" => Command::Execute(args.join(" ")),
"reconnect" => Command::Reconnect, "reconnect" => Command::Reconnect,
_ => { _ => {
return Err(NoSuchCommand { return Err(E::NoSuchCommand {
cmd: command.into(), cmd: command.into(),
}) })
} }

View File

@@ -1,3 +1,5 @@
#![allow(clippy::enum_glob_use)]
use cursive::theme::BaseColor::*; use cursive::theme::BaseColor::*;
use cursive::theme::Color::*; use cursive::theme::Color::*;
use cursive::theme::PaletteColor::*; use cursive::theme::PaletteColor::*;