1use const_macros::const_early;
4
5use miette::Diagnostic;
6use thiserror::Error;
7
8use crate::{auth::url::Url, macros::errors};
9
10pub const SCHEME: &str = "otpauth";
12
13#[derive(Debug, Error, Diagnostic)]
17#[error("unexpected scheme `{scheme}`; expected `{SCHEME}`")]
18#[diagnostic(code(otp_std::auth::scheme), help("make sure the scheme is correct"))]
19pub struct Error {
20 pub scheme: String,
22}
23
24impl Error {
25 pub const fn new(scheme: String) -> Self {
27 Self { scheme }
28 }
29}
30
31errors! {
32 Type = Error,
33 Hack = $,
34 error => new(scheme => to_owned),
35}
36
37pub fn check<S: AsRef<str>>(scheme: S) -> Result<(), Error> {
43 fn check_inner(scheme: &str) -> Result<(), Error> {
44 const_early!(scheme != SCHEME => error!(scheme));
45
46 Ok(())
47 }
48
49 check_inner(scheme.as_ref())
50}
51
52pub fn check_url(url: &Url) -> Result<(), Error> {
58 check(url.scheme())
59}