1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use std::fmt::Formatter;

use crate::ast::authority::Authority;
use crate::ast::path::Path;
use crate::ast::query::Query;
use crate::ast::scheme::Scheme;
use crate::parser::parsers::{Elms, uri_parsers, UriParseError};

pub type Fragment = String;

#[derive(Debug, Clone, PartialEq, Hash)]
pub struct Uri {
  schema: Scheme,
  authority: Option<Authority>,
  path: Path,
  query: Option<Query>,
  fragment: Option<String>,
}

impl Default for Uri {
  fn default() -> Self {
    Uri {
      schema: Scheme::default(),
      authority: Option::default(),
      path: Path::default(),
      query: Option::default(),
      fragment: Option::default(),
    }
  }
}

impl std::fmt::Display for Uri {
  fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "{}:{}{}{}{}",
      self.schema.to_string(),
      self
        .authority
        .as_ref()
        .map(|a| format!("//{}", a.to_string()))
        .unwrap_or("".to_string()),
      self.path.to_string(),
      self
        .query
        .as_ref()
        .map(|q| format!("?{}", q.to_string()))
        .unwrap_or("".to_string()),
      self
        .fragment
        .as_ref()
        .map(|s| format!("#{}", s))
        .unwrap_or("".to_string())
    )
  }
}

impl Uri {
  pub fn parse(text: &str) -> Result<Uri, nom::Err<UriParseError>> {
    uri_parsers::uri(Elms::new(text.as_bytes())).map(|(_, v)| v)
  }

  pub fn new(
    schema: Scheme,
    authority: Option<Authority>,
    path: Path,
    query: Option<Query>,
    fragment: Option<Fragment>,
  ) -> Self {
    Self {
      schema,
      authority,
      path,
      query,
      fragment,
    }
  }

  pub fn schema(&self) -> &Scheme {
    &self.schema
  }

  pub fn authority(&self) -> Option<&Authority> {
    self.authority.as_ref()
  }

  pub fn path(&self) -> &Path {
    &self.path
  }

  pub fn path_as_opt(&self) -> Option<&Path> {
    if self.path.is_empty() {
      None
    } else {
      Some(&self.path)
    }
  }

  pub fn query(&self) -> Option<&Query> {
    self.query.as_ref()
  }

  pub fn fragment(&self) -> Option<&Fragment> {
    self.fragment.as_ref()
  }
}

#[cfg(test)]
mod test {
  use std::env;

  use crate::{Uri};

  fn init() {
    env::set_var("RUST_LOG", "debug");
    let _ = env_logger::builder().is_test(true).try_init();
  }

  #[test]
  fn test_parse() {
    init();
    let s = "http://user1:pass1@localhost:8080/example?key1=value1&key2=value2&key1=value2#f1";
    match Uri::parse(s) {
      Ok(uri) => println!("{:?}", uri),
      Err(e) => println!("{:?}", e),
    }
  }
}