use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema, thiserror::Error)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum SourceError {
#[error("configuration for this source is invalid: {message}")]
Config {
message: String,
},
#[error("authentication for this source failed: {message}")]
Auth {
message: String,
},
#[error("the source refused the request: {message}")]
Refused {
message: String,
},
#[error("the source rate-limited the request{}", rate_limit_detail(.message))]
RateLimited {
retry_after_seconds: Option<u64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
message: Option<String>,
},
#[error("the source could not be reached: {message}")]
Unavailable {
message: String,
},
#[error("the source returned data this interface cannot represent: {message}")]
Malformed {
message: String,
},
}
fn rate_limit_detail(message: &Option<String>) -> String {
message
.as_ref()
.map(|said| format!(": {said}"))
.unwrap_or_default()
}