use miette::Diagnostic;
use thiserror::Error;
pub use url::Url;
pub use urlencoding::{decode, encode};
use crate::{
auth::{label::Label, scheme::SCHEME},
otp::Type,
};
#[derive(Debug, Error, Diagnostic)]
#[error("failed to parse OTP URL")]
#[diagnostic(code(otp_std::auth::url), help("make sure the OTP URL is valid"))]
pub struct Error(#[from] pub url::ParseError);
pub fn parse<S: AsRef<str>>(string: S) -> Result<Url, Error> {
Url::parse(string.as_ref()).map_err(Error)
}
pub const BASE_ALWAYS_VALID: &str = "OTP base URL is always valid";
pub fn base(type_of: Type, label: &Label<'_>) -> Url {
let string = format!("{SCHEME}://{type_of}/{label}");
parse(string).expect(BASE_ALWAYS_VALID)
}