You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.4 KiB

//! Conversion formats
use super::*;
use std::{
ops::Deref,
};
/// A string of modified base64, appropriate for URL paths etc.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct ModifiedBase64String(String);
impl ModifiedBase64String
{
/// Get as a reference
#[inline(always)] pub fn as_str(&self) -> &ModifiedBase64
{
ModifiedBase64::new_unchecked(&self.0[..])
}
/// Get as a mutable reference
#[inline(always)] fn as_mut_str(&mut self) -> &mut ModifiedBase64
{
ModifiedBase64::new_unchecked_mut(&mut self.0[..])
}
/// Consume into regular base64 string
pub fn into_base64(mut self) -> String
{
self.as_mut_str().unmodify();
self.0
}
/// Create from a refular base64 string
pub fn from_base64(mut string: String) -> Self
{
ModifiedBase64::modify(&mut string[..]);
Self(string)
}
}
impl AsRef<ModifiedBase64> for ModifiedBase64String
{
#[inline] fn as_ref(&self) -> &ModifiedBase64
{
self.as_str()
}
}
impl Deref for ModifiedBase64String
{
type Target = ModifiedBase64;
#[inline] fn deref(&self) -> &Self::Target {
self.as_str()
}
}
/// A string slice of modified base64, appropriate for URL paths etc.
#[derive(Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize)]
#[repr(transparent)]
pub struct ModifiedBase64(str);
impl ModifiedBase64
{
#[inline(always)] fn new_unchecked<'a>(from: &'a str) -> &'a ModifiedBase64
{
unsafe {
std::mem::transmute(from)
}
}
#[inline(always)] fn new_unchecked_mut<'a>(from: &'a mut str) -> &'a mut ModifiedBase64
{
unsafe {
std::mem::transmute(from)
}
}
/// Get the underlying string slice
#[inline] pub fn as_str(&self) -> &str
{
unsafe {
std::mem::transmute::<&'_ Self, &'_ str>(self)
}
}
/// Get the underlying string slice as a mutable reference.
///
/// # This is not safe to expose in the API.
#[inline] fn as_mut_str(&mut self) -> &mut str
{
unsafe {
std::mem::transmute::<&'_ mut Self, &'_ mut str>(self)
}
}
fn unmodify(&mut self) -> &mut str
{
todo!();
self.as_mut_str()
}
/// Modify this base64 string into a mutable reference to self
pub fn modify(base64: &mut str) -> &mut Self
{
todo!();
Self::new_unchecked_mut(base64)
}
}
impl AsRef<ModifiedBase64> for ModifiedBase64
{
#[inline(always)] fn as_ref(&self) -> &ModifiedBase64
{
self
}
}