#![allow(clippy::module_name_repetitions)]
use crate::error::{Error as IriError, ErrorKind};
use crate::pct_encoding::{fragment_map, pct_encode};
use crate::{parse, ValidateStr};
use crate::{Normalize, PercentEncoding};
use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Fragment(String);
impl Default for Fragment {
fn default() -> Self {
Self(String::default())
}
}
impl Display for Fragment {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "#{}", self.0)
}
}
impl FromStr for Fragment {
type Err = IriError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if Self::is_valid(s) {
Ok(Self(s.to_string()))
} else {
Err(ErrorKind::ParseFragmentError(s.to_string()).into())
}
}
}
impl ValidateStr for Fragment {
fn is_valid(s: &str) -> bool {
parse::is_ifragment(s)
}
}
impl Normalize for Fragment {}
impl PercentEncoding for Fragment {
fn encode(&self, for_uri: bool) -> Self
where
Self: Sized,
{
Self(pct_encode(&self.0, fragment_map(), for_uri))
}
}
impl Fragment {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn value(&self) -> &String {
&self.0
}
}