artichoke_backend/extn/stdlib/uri/
mod.rs

1use std::ffi::CStr;
2
3use crate::extn::prelude::*;
4
5const IP_SOCKET_CSTR: &CStr = c"IPSocket";
6const IP_ADDR_CSTR: &CStr = c"IPAddr";
7const URI_CSTR: &CStr = c"URI";
8
9static URI_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri.rb");
10static URI_COMMON_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/common.rb");
11static URI_FILE_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/file.rb");
12static URI_FTP_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/ftp.rb");
13static URI_GENERIC_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/generic.rb");
14static URI_HTTP_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/http.rb");
15static URI_HTTPS_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/https.rb");
16static URI_LDAP_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/ldap.rb");
17static URI_LDAPS_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/ldaps.rb");
18static URI_MAILTO_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/mailto.rb");
19static URI_RFC2396_PARSER_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/rfc2396_parser.rb");
20static URI_RFC3986_PARSER_RUBY_SOURCE: &[u8] = include_bytes!("vendor/uri/rfc3986_parser.rb");
21
22pub fn init(interp: &mut Artichoke) -> InitializeResult<()> {
23    let spec = class::Spec::new("IPSocket", IP_SOCKET_CSTR, None, None)?;
24    interp.def_class::<IpSocket>(spec)?;
25
26    let spec = class::Spec::new("IPAddr", IP_ADDR_CSTR, None, None)?;
27    interp.def_class::<IpAddr>(spec)?;
28
29    let spec = module::Spec::new(interp, "URI", URI_CSTR, None)?;
30    interp.def_module::<Uri>(spec)?;
31
32    interp.def_rb_source_file("uri.rb", URI_RUBY_SOURCE)?;
33    interp.def_rb_source_file("uri/common.rb", URI_COMMON_RUBY_SOURCE)?;
34    interp.def_rb_source_file("uri/file.rb", URI_FILE_RUBY_SOURCE)?;
35    interp.def_rb_source_file("uri/ftp.rb", URI_FTP_RUBY_SOURCE)?;
36    interp.def_rb_source_file("uri/generic.rb", URI_GENERIC_RUBY_SOURCE)?;
37    interp.def_rb_source_file("uri/http.rb", URI_HTTP_RUBY_SOURCE)?;
38    interp.def_rb_source_file("uri/https.rb", URI_HTTPS_RUBY_SOURCE)?;
39    interp.def_rb_source_file("uri/ldap.rb", URI_LDAP_RUBY_SOURCE)?;
40    interp.def_rb_source_file("uri/ldaps.rb", URI_LDAPS_RUBY_SOURCE)?;
41    interp.def_rb_source_file("uri/mailto.rb", URI_MAILTO_RUBY_SOURCE)?;
42    interp.def_rb_source_file("uri/rfc2396_parser.rb", URI_RFC2396_PARSER_RUBY_SOURCE)?;
43    interp.def_rb_source_file("uri/rfc3986_parser.rb", URI_RFC3986_PARSER_RUBY_SOURCE)?;
44
45    Ok(())
46}
47
48#[derive(Debug, Clone, Copy)]
49pub struct IpSocket;
50
51#[derive(Debug, Clone, Copy)]
52pub struct IpAddr;
53
54#[derive(Debug, Clone, Copy)]
55pub struct Uri;
56
57#[cfg(test)]
58mod tests {
59    use crate::test::prelude::*;
60
61    const SUBJECT: &str = "URI";
62    const FUNCTIONAL_TEST: &[u8] = include_bytes!("uri_test.rb");
63
64    #[test]
65    fn functional() {
66        let mut interp = interpreter();
67        let result = interp.eval(FUNCTIONAL_TEST);
68        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
69        let result = interp.eval(b"spec");
70        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
71    }
72}