use std::{convert::TryFrom, fmt::Display};
use crate::{ErrorKind, Uri};
use super::InputSource;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct Request {
pub uri: Uri,
pub source: InputSource,
pub element: Option<String>,
pub attribute: Option<String>,
}
impl Request {
#[inline]
#[must_use]
pub const fn new(
uri: Uri,
source: InputSource,
element: Option<String>,
attribute: Option<String>,
) -> Self {
Request {
uri,
source,
element,
attribute,
}
}
}
impl Display for Request {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({})", self.uri, self.source)
}
}
impl TryFrom<Uri> for Request {
type Error = ErrorKind;
fn try_from(uri: Uri) -> Result<Self, Self::Error> {
Ok(Request::new(
uri.clone(),
InputSource::RemoteUrl(Box::new(uri.url)),
None,
None,
))
}
}
impl TryFrom<String> for Request {
type Error = ErrorKind;
fn try_from(s: String) -> Result<Self, Self::Error> {
let uri = Uri::try_from(s.as_str())?;
Ok(Request::new(uri, InputSource::String(s), None, None))
}
}
impl TryFrom<&str> for Request {
type Error = ErrorKind;
fn try_from(s: &str) -> Result<Self, Self::Error> {
let uri = Uri::try_from(s)?;
Ok(Request::new(
uri,
InputSource::String(s.to_owned()),
None,
None,
))
}
}