use const_macros::const_early;
use miette::Diagnostic;
use thiserror::Error;
use crate::{auth::url::Url, macros::errors};
pub const SCHEME: &str = "otpauth";
#[derive(Debug, Error, Diagnostic)]
#[error("unexpected scheme `{scheme}`; expected `{SCHEME}`")]
#[diagnostic(code(otp_std::auth::scheme), help("make sure the scheme is correct"))]
pub struct Error {
pub scheme: String,
}
impl Error {
pub const fn new(scheme: String) -> Self {
Self { scheme }
}
}
errors! {
Type = Error,
Hack = $,
error => new(scheme => to_owned),
}
pub fn check<S: AsRef<str>>(scheme: S) -> Result<(), Error> {
fn check_inner(scheme: &str) -> Result<(), Error> {
const_early!(scheme != SCHEME => error!(scheme));
Ok(())
}
check_inner(scheme.as_ref())
}
pub fn check_url(url: &Url) -> Result<(), Error> {
check(url.scheme())
}