use crate::http::Header;
#[derive(Debug, thiserror::Error)]
pub enum ConnectOptionsError {
#[error("path must not be empty")]
EmptyPath,
}
#[derive(Debug)]
#[non_exhaustive]
pub struct ConnectOptions<'a, 'b> {
pub(crate) path: &'a str,
pub headers: &'a [Header<'b>],
}
impl<'a, 'b> Default for ConnectOptions<'a, 'b> {
fn default() -> Self {
Self::default()
}
}
impl<'a, 'b> ConnectOptions<'a, 'b> {
pub fn new(path: &'a str) -> Result<Self, ConnectOptionsError> {
Self::default().with_path(path)
}
pub const fn new_unchecked(path: &'a str) -> Self {
Self::default().with_path_unchecked(path)
}
pub const fn path(&self) -> &str {
self.path
}
pub fn with_path(mut self, path: &'a str) -> Result<Self, ConnectOptionsError> {
if path.trim().is_empty() {
return Err(ConnectOptionsError::EmptyPath);
};
self.path = path.trim();
Ok(self)
}
pub const fn with_path_unchecked(mut self, path: &'a str) -> Self {
self.path = path;
self
}
pub const fn headers(&self) -> &[Header<'b>] {
self.headers
}
pub const fn with_headers(mut self, headers: &'a [Header<'b>]) -> Self {
self.headers = headers;
self
}
const fn default() -> Self {
Self {
path: "/",
headers: &[],
}
}
}
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct AcceptOptions<'a, 'b> {
pub headers: &'a [Header<'b>],
}
impl<'a, 'b> AcceptOptions<'a, 'b> {
pub const fn with_headers(mut self, headers: &'a [Header<'b>]) -> Self {
self.headers = headers;
self
}
pub const fn headers(&self) -> &[Header<'b>] {
self.headers
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_path() {
let error = ConnectOptions::new("").unwrap_err();
assert!(matches!(error, ConnectOptionsError::EmptyPath));
let error = ConnectOptions::new(" ").unwrap_err();
assert!(matches!(error, ConnectOptionsError::EmptyPath));
}
#[test]
fn path_is_trimmed() {
let options = ConnectOptions::new(" /test ").unwrap();
assert_eq!(options.path(), "/test");
let options = ConnectOptions::new("/test").unwrap();
assert_eq!(options.path(), "/test");
}
}