Use custom error type for ddr2osu

This commit is contained in:
Simon Bruder 2020-08-07 13:01:06 +02:00
parent a7efcb2033
commit ee84247a1d
No known key found for this signature in database
GPG key ID: 6F03E0000CC5B62F

View file

@ -1,14 +1,22 @@
use std::fmt; use std::fmt;
use std::str::FromStr; use std::str::FromStr;
use anyhow::{anyhow, Result};
use clap::Clap; use clap::Clap;
use log::{debug, info, trace, warn}; use log::{debug, info, trace, warn};
use thiserror::Error;
use crate::ddr::ssq; use crate::ddr::ssq;
use crate::osu::beatmap; use crate::osu::beatmap;
use crate::osu::types::*; use crate::osu::types::*;
#[derive(Debug, Error)]
pub enum Error {
#[error("invalid range format (expected “start:end”, found {0})")]
InvalidRangeFormat(String),
#[error(transparent)]
InvalidFloat(#[from] std::num::ParseFloatError),
}
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct ConfigRange(f32, f32); pub struct ConfigRange(f32, f32);
@ -26,12 +34,12 @@ impl fmt::Display for ConfigRange {
} }
impl FromStr for ConfigRange { impl FromStr for ConfigRange {
type Err = anyhow::Error; type Err = Error;
fn from_str(string: &str) -> Result<Self> { fn from_str(string: &str) -> Result<Self, Error> {
match string.split(':').collect::<Vec<&str>>()[..] { match string.split(':').collect::<Vec<&str>>()[..] {
[start, end] => Ok(ConfigRange(start.parse::<f32>()?, end.parse::<f32>()?)), [start, end] => Ok(ConfigRange(start.parse::<f32>()?, end.parse::<f32>()?)),
_ => Err(anyhow!("Invalid range format (expected start:end)")), _ => Err(Error::InvalidRangeFormat(string.to_string())),
} }
} }
} }
@ -349,7 +357,7 @@ impl ConvertedChart {
} }
impl ssq::SSQ { impl ssq::SSQ {
pub fn to_beatmaps(&self, config: &Config) -> Result<Vec<beatmap::Beatmap>> { pub fn to_beatmaps(&self, config: &Config) -> Result<Vec<beatmap::Beatmap>, Error> {
debug!("Configuration: {:?}", config); debug!("Configuration: {:?}", config);
let mut timing_points = beatmap::TimingPoints(Vec::new()); let mut timing_points = beatmap::TimingPoints(Vec::new());