use std::convert::TryFrom;
#[cfg(feature = "serde")]
use serde::Serialize;
use validated_slice::{OwnedSliceSpec, SliceSpec};
use crate::{
types::IriCreationError,
validate::iri::{fragment, Error},
};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
#[allow(clippy::derive_hash_xor_eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct IriFragmentStr(str);
impl IriFragmentStr {
pub fn new(s: &str) -> Result<&Self, Error> {
TryFrom::try_from(s)
}
pub fn from_prefixed(s: &str) -> Result<&Self, Error> {
if s.as_bytes().get(0) != Some(&b'#') {
return Err(Error::new());
}
TryFrom::try_from(&s[1..])
}
pub(crate) unsafe fn new_unchecked(s: &str) -> &Self {
debug_assert_eq!(StrSpec::validate(s), Ok(()));
StrSpec::from_inner_unchecked(s)
}
pub fn as_str(&self) -> &str {
self.as_ref()
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct IriFragmentString(String);
impl IriFragmentString {
pub(crate) unsafe fn new_unchecked(s: String) -> Self {
debug_assert_eq!(StrSpec::validate(&s), Ok(()));
StringSpec::from_inner_unchecked(s)
}
pub fn shrink_to_fit(&mut self) {
self.0.shrink_to_fit()
}
}
impl_basics! {
Slice {
spec: StrSpec,
custom: IriFragmentStr,
validator: fragment,
error: Error,
},
Owned {
spec: StringSpec,
custom: IriFragmentString,
error: IriCreationError<String>,
},
}
impl_serde! {
expecting: "an IRI fragment",
slice: IriFragmentStr,
owned: IriFragmentString,
}