use std::fmt::{Debug, Display};
use std::ops::Deref;
use std::sync::Arc;
use serde::{ser, Serialize};
use super::Url;
#[derive(Debug, Clone, Serialize)]
pub struct Nonce(String);
impl AsRef<str> for Nonce {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl From<String> for Nonce {
fn from(value: String) -> Self {
Nonce(value)
}
}
impl From<&str> for Nonce {
fn from(value: &str) -> Self {
Nonce(value.to_string())
}
}
#[derive(Debug, Clone)]
pub struct AccountKeyIdentifier(Arc<Url>);
impl ser::Serialize for AccountKeyIdentifier {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.0.deref().serialize(serializer)
}
}
impl From<Url> for AccountKeyIdentifier {
fn from(value: Url) -> Self {
AccountKeyIdentifier(Arc::new(value))
}
}
impl AccountKeyIdentifier {
pub fn to_url(&self) -> Url {
self.0.deref().clone()
}
}
impl AsRef<str> for AccountKeyIdentifier {
fn as_ref(&self) -> &str {
self.0.as_str()
}
}
impl AsRef<[u8]> for AccountKeyIdentifier {
fn as_ref(&self) -> &[u8] {
self.0.as_str().as_bytes()
}
}
impl Display for AccountKeyIdentifier {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_ref())
}
}
#[derive(Debug, Clone, Serialize)]
pub struct RequestHeader {
#[serde(skip_serializing_if = "Option::is_none")]
nonce: Option<Nonce>,
url: Url,
}
impl RequestHeader {
pub fn new(url: Url, nonce: Option<Nonce>) -> Self {
Self { nonce, url }
}
pub fn replace_nonce(&mut self, nonce: Nonce) {
self.nonce = Some(nonce);
}
}