#![allow(clippy::module_name_repetitions)]
use crate::error::{Component, Error as IriError, ErrorKind, Result as IriResult};
use crate::pct_encoding::{pct_encode, query_map};
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 Query(String);
impl Default for Query {
fn default() -> Self {
Self(String::new())
}
}
impl Display for Query {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "?{}", self.0)
}
}
impl FromStr for Query {
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::InvalidChar(Component::Query).into())
}
}
}
impl ValidateStr for Query {
fn is_valid(s: &str) -> bool {
parse::is_iquery(s)
}
}
impl Normalize for Query {
fn normalize(self) -> IriResult<Self> {
unimplemented!()
}
}
impl PercentEncoding for Query {
fn encode(&self, for_uri: bool) -> Self
where
Self: Sized,
{
Self(pct_encode(&self.0, query_map(), for_uri))
}
}
impl Query {
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn value(&self) -> &String {
&self.0
}
}