use crate::prelude::FmtResult;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::convert::Infallible;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
pub const RED_URL: &str = "https://redacted.sh";
pub const RED_URL_CH: &str = "https://redacted.ch";
pub const OPS_URL: &str = "https://orpheus.network";
pub const RED_TRACKER_URL: &str = "https://flacsfor.me";
pub const OPS_TRACKER_URL: &str = "https://home.opsfet.ch";
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(from = "String", into = "String")]
pub enum Indexer {
#[default]
Red,
Pth,
Ops,
Other(String),
}
impl Indexer {
#[must_use]
pub fn as_lowercase(&self) -> &str {
match self {
Indexer::Red => "red",
Indexer::Pth => "pth",
Indexer::Ops => "ops",
Indexer::Other(value) => value,
}
}
#[must_use]
pub fn to_uppercase(&self) -> String {
self.as_lowercase().to_uppercase()
}
pub fn match_with_alts(&self, other: &Indexer) -> bool {
self == other || (self == &Indexer::Red && other == &Indexer::Pth)
}
}
impl From<&str> for Indexer {
fn from(value: &str) -> Self {
let lowercase = value.to_lowercase();
match lowercase.as_str() {
"red" => Indexer::Red,
"pth" => Indexer::Pth,
"ops" => Indexer::Ops,
_ => Indexer::Other(lowercase),
}
}
}
impl From<String> for Indexer {
fn from(value: String) -> Self {
Indexer::from(value.as_str())
}
}
impl From<Indexer> for String {
fn from(value: Indexer) -> Self {
value.as_lowercase().to_owned()
}
}
impl FromStr for Indexer {
type Err = Infallible;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Ok(Indexer::from(value))
}
}
impl Ord for Indexer {
fn cmp(&self, other: &Self) -> Ordering {
self.as_lowercase().cmp(other.as_lowercase())
}
}
impl PartialOrd for Indexer {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Display for Indexer {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
self.to_uppercase().fmt(f)
}
}