surrealdb_core/fnc/
parse.rs

1pub mod email {
2
3	use crate::err::Error;
4	use crate::sql::value::Value;
5	use addr::email::Host;
6
7	pub fn host((string,): (String,)) -> Result<Value, Error> {
8		// Parse the email address
9		Ok(match addr::parse_email_address(&string) {
10			// Return the host part
11			Ok(v) => match v.host() {
12				Host::Domain(name) => name.as_str().into(),
13				Host::IpAddr(ip_addr) => ip_addr.to_string().into(),
14			},
15			Err(_) => Value::None,
16		})
17	}
18
19	pub fn user((string,): (String,)) -> Result<Value, Error> {
20		// Parse the email address
21		Ok(match addr::parse_email_address(&string) {
22			// Return the user part
23			Ok(v) => v.user().into(),
24			Err(_) => Value::None,
25		})
26	}
27
28	#[cfg(test)]
29	mod tests {
30		#[test]
31		fn host() {
32			let input = (String::from("john.doe@example.com"),);
33			let value = super::host(input).unwrap();
34			assert_eq!(value, "example.com".into());
35		}
36
37		#[test]
38		fn user() {
39			let input = (String::from("john.doe@example.com"),);
40			let value = super::user(input).unwrap();
41			assert_eq!(value, "john.doe".into());
42		}
43	}
44}
45
46pub mod url {
47
48	use crate::err::Error;
49	use crate::sql::value::Value;
50	use url::Url;
51
52	pub fn domain((string,): (String,)) -> Result<Value, Error> {
53		match Url::parse(&string) {
54			Ok(v) => match v.domain() {
55				Some(v) => Ok(v.into()),
56				None => Ok(Value::None),
57			},
58			Err(_) => Ok(Value::None),
59		}
60	}
61
62	pub fn fragment((string,): (String,)) -> Result<Value, Error> {
63		// Parse the URL
64		match Url::parse(&string) {
65			Ok(v) => match v.fragment() {
66				Some(v) => Ok(v.into()),
67				None => Ok(Value::None),
68			},
69			Err(_) => Ok(Value::None),
70		}
71	}
72
73	pub fn host((string,): (String,)) -> Result<Value, Error> {
74		// Parse the URL
75		match Url::parse(&string) {
76			Ok(v) => match v.host_str() {
77				Some(v) => Ok(v.into()),
78				None => Ok(Value::None),
79			},
80			Err(_) => Ok(Value::None),
81		}
82	}
83
84	pub fn path((string,): (String,)) -> Result<Value, Error> {
85		// Parse the URL
86		match Url::parse(&string) {
87			Ok(v) => Ok(v.path().into()),
88			Err(_) => Ok(Value::None),
89		}
90	}
91
92	pub fn port((string,): (String,)) -> Result<Value, Error> {
93		// Parse the URL
94		match Url::parse(&string) {
95			Ok(v) => match v.port_or_known_default() {
96				Some(v) => Ok(v.into()),
97				None => Ok(Value::None),
98			},
99			Err(_) => Ok(Value::None),
100		}
101	}
102
103	pub fn query((string,): (String,)) -> Result<Value, Error> {
104		// Parse the URL
105		match Url::parse(&string) {
106			Ok(v) => match v.query() {
107				Some(v) => Ok(v.into()),
108				None => Ok(Value::None),
109			},
110			Err(_) => Ok(Value::None),
111		}
112	}
113
114	pub fn scheme((string,): (String,)) -> Result<Value, Error> {
115		// Parse the URL
116		match Url::parse(&string) {
117			Ok(v) => Ok(v.scheme().into()),
118			Err(_) => Ok(Value::None),
119		}
120	}
121
122	#[cfg(test)]
123	mod tests {
124		use crate::sql::value::Value;
125
126		#[test]
127		fn port_default_port_specified() {
128			let value = super::port(("http://www.google.com:80".to_string(),)).unwrap();
129			assert_eq!(value, 80.into());
130		}
131
132		#[test]
133		fn port_nondefault_port_specified() {
134			let value = super::port(("http://www.google.com:8080".to_string(),)).unwrap();
135			assert_eq!(value, 8080.into());
136		}
137
138		#[test]
139		fn port_no_port_specified() {
140			let value = super::port(("http://www.google.com".to_string(),)).unwrap();
141			assert_eq!(value, 80.into());
142		}
143
144		#[test]
145		fn port_no_scheme_no_port_specified() {
146			let value = super::port(("www.google.com".to_string(),)).unwrap();
147			assert_eq!(value, Value::None);
148		}
149	}
150}