replaced box dyn error with rberror

This commit is contained in:
Alex 2023-10-20 21:07:41 +02:00
parent 0f5ed27d26
commit 3703d731d1
17 changed files with 91 additions and 61 deletions

View file

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
* Replaced Box dyn Error with RbError enum
## [0.6.1] 2023-10-20
### Fixed

View file

@ -24,7 +24,7 @@ radiobrowser = { version = "*", features = ["blocking"] }
use radiobrowser::blocking::RadioBrowserAPI;
use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), RbError> {
let mut api = RadioBrowserAPI::new()?;
let servers = RadioBrowserAPI::get_default_servers()?;
println!("Servers: {:?}", servers);
@ -50,7 +50,7 @@ use radiobrowser::StationOrder;
use std::error::Error;
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<(), RbError> {
let mut api = RadioBrowserAPI::new().await?;
let stations = api
.get_stations()

View file

@ -1,10 +1,10 @@
use futures::join;
use radiobrowser::RadioBrowserAPI;
use radiobrowser::RbError;
use radiobrowser::StationOrder;
use std::error::Error;
#[async_std::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> Result<(), RbError> {
let mut api = RadioBrowserAPI::new().await?;
let countries = api.get_countries().send();
let languages = api.get_languages().send();

View file

@ -1,7 +1,6 @@
use radiobrowser::blocking::RadioBrowserAPI;
use std::error::Error;
use radiobrowser::{blocking::RadioBrowserAPI, RbError};
fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), RbError> {
let mut api = RadioBrowserAPI::new()?;
let servers = RadioBrowserAPI::get_default_servers()?;
println!("Servers: {:?}", servers);

View file

@ -2,6 +2,7 @@ use crate::ApiStationClickResult;
use crate::ApiStationHistory;
use crate::ApiStationVoteResult;
use crate::ApiStatus;
use crate::RbError;
use crate::external::post_api;
use crate::ApiConfig;
use crate::CountrySearchBuilder;
@ -11,7 +12,6 @@ use crate::TagSearchBuilder;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::error::Error;
use rand::seq::SliceRandom;
use rand::thread_rng;
@ -27,10 +27,10 @@ use async_std_resolver::{config, resolver};
///
/// Example
/// ```rust
/// use std::error::Error;
/// use radiobrowser::RbError;
/// use radiobrowser::RadioBrowserAPI;
/// #[async_std::main]
/// async fn main() -> Result<(), Box<dyn Error>> {
/// async fn main() -> Result<(), RbError> {
/// let mut api = RadioBrowserAPI::new().await?;
/// Ok(())
/// }
@ -45,7 +45,7 @@ impl RadioBrowserAPI {
/// Create a new instance of a radiobrowser api client.
/// It will fetch a list of radiobrowser server with get_default_servers()
/// and save it internally.
pub async fn new() -> Result<Self, Box<dyn Error>> {
pub async fn new() -> Result<Self, RbError> {
Ok(RadioBrowserAPI {
servers: RadioBrowserAPI::get_default_servers().await?,
current: 0,
@ -54,7 +54,7 @@ impl RadioBrowserAPI {
/// Create a new instance of a radiobrowser api client from
/// a single dns name. Use this is you want to connect to a single named server.
pub async fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
pub async fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, RbError> {
Ok(RadioBrowserAPI {
servers: vec![dnsname.as_ref().to_string()],
current: 0,
@ -63,7 +63,7 @@ impl RadioBrowserAPI {
/// Create a new instance of a radiobrowser api client from
/// a dns srv record which may have multiple dns A/AAAA records.
pub async fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, Box<dyn Error>> {
pub async fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, RbError> {
Ok(RadioBrowserAPI {
servers: RadioBrowserAPI::get_servers_from_dns_srv(srvname).await?,
current: 0,
@ -83,12 +83,12 @@ impl RadioBrowserAPI {
async fn post_api<P: DeserializeOwned, A: AsRef<str>>(
&mut self,
endpoint: A,
) -> Result<P, Box<dyn Error>> {
) -> Result<P, RbError> {
let mapjson = HashMap::new();
post_api(self.get_current_server(), endpoint.as_ref(), mapjson).await
}
pub async fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, Box<dyn Error>> {
pub async fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, RbError> {
let query = match last_change_uuid {
Some(uuid) => format!("/json/stations/changed?limit={}&lastchangeuuid={}", limit, uuid),
None => format!("/json/stations/changed?limit={}", limit)
@ -96,21 +96,21 @@ impl RadioBrowserAPI {
Ok(self.post_api(query).await?)
}
pub async fn get_server_config(&mut self) -> Result<ApiConfig, Box<dyn Error>> {
pub async fn get_server_config(&mut self) -> Result<ApiConfig, RbError> {
Ok(self.post_api("/json/config").await?)
}
pub async fn get_server_status(&mut self) -> Result<ApiStatus, Box<dyn Error>> {
pub async fn get_server_status(&mut self) -> Result<ApiStatus, RbError> {
Ok(self.post_api("/json/stats").await?)
}
/// Add a click to a station found by stationuuid
pub async fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, Box<dyn Error>> {
pub async fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, RbError> {
Ok(self.post_api(format!("/json/url/{}",stationuuid.as_ref())).await?)
}
/// Add a vote to a station found by a stationuuid
pub async fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, Box<dyn Error>> {
pub async fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, RbError> {
Ok(self.post_api(format!("/json/vote/{}",stationuuid.as_ref())).await?)
}
@ -134,16 +134,16 @@ impl RadioBrowserAPI {
&mut self,
endpoint: P,
mapjson: HashMap<String, String>,
) -> Result<Q, Box<dyn Error>> {
) -> Result<Q, RbError> {
post_api(self.get_current_server(), endpoint, mapjson).await
}
pub async fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
pub async fn get_default_servers() -> Result<Vec<String>, RbError> {
trace!("get_default_servers()");
RadioBrowserAPI::get_servers_from_dns_srv("_api._tcp.radio-browser.info").await
}
async fn get_servers_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Vec<String>, Box<dyn Error>> {
async fn get_servers_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Vec<String>, RbError> {
trace!("get_servers_from_dns_srv()");
let resolver = resolver(
config::ResolverConfig::default(),

View file

@ -1,4 +1,5 @@
use crate::ApiStationHistory;
use crate::RbError;
use crate::blocking::stationsearchbuilder::StationSearchBuilder;
use crate::blocking::CountrySearchBuilder;
use crate::blocking::LanguageSearchBuilder;
@ -10,7 +11,6 @@ use crate::ApiConfig;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::error::Error;
#[derive(Clone, Debug)]
pub struct RadioBrowserAPI {
@ -20,40 +20,40 @@ pub struct RadioBrowserAPI {
use async_std::task;
impl RadioBrowserAPI {
pub fn new() -> Result<Self, Box<dyn Error>> {
pub fn new() -> Result<Self, RbError> {
task::block_on(async { crate::RadioBrowserAPI::new().await })
.map(|api| RadioBrowserAPI { api })
}
pub fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, Box<dyn Error>> {
pub fn new_from_dns_a<P: AsRef<str>>(dnsname: P) -> Result<Self, RbError> {
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_a(dnsname).await })
.map(|api| RadioBrowserAPI { api })
}
pub fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, Box<dyn Error>> {
pub fn new_from_dns_srv<P: AsRef<str>>(srvname: P) -> Result<Self, RbError> {
task::block_on(async { crate::RadioBrowserAPI::new_from_dns_srv(srvname).await })
.map(|api| RadioBrowserAPI { api })
}
pub fn get_server_status(&mut self) -> Result<ApiStatus, Box<dyn Error>> {
pub fn get_server_status(&mut self) -> Result<ApiStatus, RbError> {
task::block_on(async { self.api.get_server_status().await })
}
pub fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, Box<dyn Error>> {
pub fn get_station_changes(&mut self, limit: u64, last_change_uuid: Option<String>) -> Result<Vec<ApiStationHistory>, RbError> {
task::block_on(async { self.api.get_station_changes(limit, last_change_uuid).await })
}
pub fn get_server_config(&mut self) -> Result<ApiConfig, Box<dyn Error>> {
pub fn get_server_config(&mut self) -> Result<ApiConfig, RbError> {
task::block_on(async { self.api.get_server_config().await })
}
/// Add a click to a station found by stationuuid
pub fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, Box<dyn Error>> {
pub fn station_click<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationClickResult, RbError> {
task::block_on(async { self.api.station_click(stationuuid).await })
}
/// Add a vote to a station found by a stationuuid
pub fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, Box<dyn Error>> {
pub fn station_vote<P: AsRef<str>>(&mut self, stationuuid: P) -> Result<ApiStationVoteResult, RbError> {
task::block_on(async { self.api.station_vote(stationuuid).await })
}
@ -77,11 +77,11 @@ impl RadioBrowserAPI {
&mut self,
endpoint: P,
mapjson: HashMap<String, String>,
) -> Result<Q, Box<dyn Error>> {
) -> Result<Q, RbError> {
task::block_on(async { self.api.send(endpoint, mapjson).await })
}
pub fn get_default_servers() -> Result<Vec<String>, Box<dyn Error>> {
pub fn get_default_servers() -> Result<Vec<String>, RbError> {
task::block_on(async { crate::RadioBrowserAPI::get_default_servers().await })
}
}

View file

@ -1,6 +1,5 @@
use crate::ApiCountry;
use crate::{ApiCountry, RbError};
use async_std::task;
use std::error::Error;
#[derive(Clone, Debug)]
pub struct CountrySearchBuilder {
@ -48,7 +47,7 @@ impl CountrySearchBuilder {
}
}
pub fn send(self) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
pub fn send(self) -> Result<Vec<ApiCountry>, RbError> {
task::block_on(async { self.builder.send().await })
}
}

View file

@ -1,6 +1,5 @@
use crate::ApiLanguage;
use crate::{ApiLanguage, RbError};
use async_std::task;
use std::error::Error;
#[derive(Clone, Debug)]
pub struct LanguageSearchBuilder {
@ -48,7 +47,7 @@ impl LanguageSearchBuilder {
}
}
pub fn send(self) -> Result<Vec<ApiLanguage>, Box<dyn Error>> {
pub fn send(self) -> Result<Vec<ApiLanguage>, RbError> {
task::block_on(async { self.builder.send().await })
}
}

View file

@ -1,7 +1,6 @@
use crate::structs::ApiStation;
use crate::StationOrder;
use crate::{StationOrder, RbError};
use async_std::task;
use std::error::Error;
#[derive(Clone, Debug)]
pub struct StationSearchBuilder {
@ -151,7 +150,7 @@ impl StationSearchBuilder {
}
}
pub fn send(self) -> Result<Vec<ApiStation>, Box<dyn Error>> {
pub fn send(self) -> Result<Vec<ApiStation>, RbError> {
task::block_on(async { self.builder.send().await })
}
}

View file

@ -1,6 +1,5 @@
use crate::ApiTag;
use crate::{ApiTag, RbError};
use async_std::task;
use std::error::Error;
#[derive(Clone, Debug)]
pub struct TagSearchBuilder {
@ -48,7 +47,7 @@ impl TagSearchBuilder {
}
}
pub fn send(self) -> Result<Vec<ApiTag>, Box<dyn Error>> {
pub fn send(self) -> Result<Vec<ApiTag>, RbError> {
task::block_on(async { self.builder.send().await })
}
}

View file

@ -1,9 +1,8 @@
use crate::ApiCountry;
use crate::RadioBrowserAPI;
use crate::RbError;
use std::fmt::Display;
use std::collections::HashMap;
use std::error::Error;
pub enum CountryOrder {
Name,
@ -69,7 +68,7 @@ impl CountrySearchBuilder {
self
}
pub async fn send(mut self) -> Result<Vec<ApiCountry>, Box<dyn Error>> {
pub async fn send(mut self) -> Result<Vec<ApiCountry>, RbError> {
if let Some(filter) = self.filter {
Ok(self
.api

View file

@ -1,13 +1,14 @@
use reqwest;
use serde::de::DeserializeOwned;
use std::collections::HashMap;
use std::error::Error;
use crate::RbError;
pub async fn post_api<P: DeserializeOwned, A: AsRef<str>, B: AsRef<str>>(
server: A,
endpoint: B,
mapjson: HashMap<String, String>,
) -> Result<P, Box<dyn Error>> {
) -> Result<P, RbError> {
static APP_USER_AGENT: &str = concat!("radiobrowser-lib-rust/", env!("CARGO_PKG_VERSION"),);
let client = reqwest::Client::builder()

View file

@ -1,9 +1,8 @@
use crate::ApiLanguage;
use crate::RadioBrowserAPI;
use crate::RbError;
use std::fmt::Display;
use std::collections::HashMap;
use std::error::Error;
pub enum LanguageOrder {
Name,
@ -69,7 +68,7 @@ impl LanguageSearchBuilder {
self
}
pub async fn send(mut self) -> Result<Vec<ApiLanguage>, Box<dyn Error>> {
pub async fn send(mut self) -> Result<Vec<ApiLanguage>, RbError> {
if let Some(filter) = self.filter {
Ok(self
.api

View file

@ -7,9 +7,9 @@
//! ```
//! ```rust
//! use radiobrowser::blocking::RadioBrowserAPI;
//! use std::error::Error;
//! use radiobrowser::RbError;
//!
//! fn main() -> Result<(), Box<dyn Error>> {
//! fn main() -> Result<(), RbError> {
//! let api = RadioBrowserAPI::new()?;
//! let servers = RadioBrowserAPI::get_default_servers()?;
//! println!("Servers: {:?}", servers);
@ -23,13 +23,13 @@
//!
//! # Example async
//! ```rust
//! use std::error::Error;
//! use radiobrowser::RbError;
//! use futures::join;
//! use radiobrowser::RadioBrowserAPI;
//! use radiobrowser::StationOrder;
//!
//! #[async_std::main]
//! async fn main() -> Result<(), Box<dyn Error>> {
//! async fn main() -> Result<(), RbError> {
//! let mut api = RadioBrowserAPI::new().await?;
//! let countries = api.get_countries().send();
//! let languages = api.get_languages().send();
@ -60,7 +60,9 @@ mod countrysearchbuilder;
mod languagesearchbuilder;
mod tagsearchbuilder;
mod structs;
mod rb_error;
pub use rb_error::RbError;
pub use api::RadioBrowserAPI;
pub use structs::ApiConfig;
pub use structs::ApiCountry;

30
src/rb_error.rs Normal file
View file

@ -0,0 +1,30 @@
use async_std_resolver::ResolveError;
#[derive(Debug)]
pub enum RbError {
Reqwest(reqwest::Error),
Resolve(ResolveError),
}
impl std::error::Error for RbError {}
impl std::fmt::Display for RbError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RbError::Reqwest(err) => write!(f, "Reqwest: {}", err),
RbError::Resolve(err) => write!(f, "Resolve: {}", err),
}
}
}
impl From<reqwest::Error> for RbError {
fn from(value: reqwest::Error) -> Self {
RbError::Reqwest(value)
}
}
impl From<ResolveError> for RbError {
fn from(value: ResolveError) -> Self {
RbError::Resolve(value)
}
}

View file

@ -1,9 +1,9 @@
use crate::RbError;
use crate::api::RadioBrowserAPI;
use crate::structs::ApiStation;
use std::fmt::Display;
use std::collections::HashMap;
use std::error::Error;
pub enum StationOrder {
Name,
@ -206,7 +206,7 @@ impl StationSearchBuilder {
self
}
pub async fn send(mut self) -> Result<Vec<ApiStation>, Box<dyn Error>> {
pub async fn send(mut self) -> Result<Vec<ApiStation>, RbError> {
Ok(self.api.send("/json/stations/search", self.map).await?)
}
}

View file

@ -1,9 +1,9 @@
use crate::ApiTag;
use crate::RadioBrowserAPI;
use crate::RbError;
use std::fmt::Display;
use std::collections::HashMap;
use std::error::Error;
pub enum TagOrder {
Name,
@ -69,7 +69,7 @@ impl TagSearchBuilder {
self
}
pub async fn send(mut self) -> Result<Vec<ApiTag>, Box<dyn Error>> {
pub async fn send(mut self) -> Result<Vec<ApiTag>, RbError> {
if let Some(filter) = self.filter {
Ok(self
.api