File: lib.rs

package info (click to toggle)
rust-wadl 0.5.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: xml: 5,612; makefile: 2
file content (195 lines) | stat: -rw-r--r-- 5,170 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#![deny(missing_docs)]
//! # WADL
//!
//! A crate for parsing WADL files and generating Rust code from them.

pub mod ast;
#[cfg(feature = "codegen")]
pub mod codegen;
mod parse;

/// The MIME type of WADL files.
pub const WADL_MIME_TYPE: &str = "application/vnd.sun.wadl+xml";

pub use parse::{parse, parse_bytes, parse_file, parse_string, Error as ParseError};

use url::Url;

/// The root of the web service.
pub trait Resource {
    /// The URL of the resource
    fn url(&self) -> &Url;
}

#[cfg(feature = "async")]
/// Asynchronous features
pub mod r#async {
    use super::*;

    /// A client for a WADL API
    #[async_trait::async_trait]
    pub trait Client: Sync + Send {
        /// Create a new request builder
        async fn request(&self, method: reqwest::Method, url: url::Url) -> reqwest::RequestBuilder;
    }

    #[async_trait::async_trait]
    impl Client for reqwest::Client {
        async fn request(&self, method: reqwest::Method, url: url::Url) -> reqwest::RequestBuilder {
            self.request(method, url)
        }
    }

    /// Get the WADL AST from a URL.
    pub async fn get_wadl_resource_by_href(
        client: &dyn Client,
        href: &url::Url,
    ) -> Result<crate::ast::Resource, Error> {
        let mut req = client.request(reqwest::Method::GET, href.clone()).await;

        req = req.header(reqwest::header::ACCEPT, super::WADL_MIME_TYPE);

        let res = req.send().await?;

        let text = res.text().await?;

        let application = super::parse_string(&text)?;

        let resource = application.get_resource_by_href(href).unwrap();

        Ok(resource.clone())
    }
}

#[cfg(feature = "blocking")]
/// Blocking features
pub mod blocking {
    use super::*;

    /// A client for a WADL API
    pub trait Client {
        /// Create a new request builder
        fn request(
            &self,
            method: reqwest::Method,
            url: url::Url,
        ) -> reqwest::blocking::RequestBuilder;
    }

    impl Client for reqwest::blocking::Client {
        fn request(
            &self,
            method: reqwest::Method,
            url: url::Url,
        ) -> reqwest::blocking::RequestBuilder {
            self.request(method, url)
        }
    }

    /// Get the WADL AST from a URL.
    pub fn get_wadl_resource_by_href(
        client: &dyn Client,
        href: &url::Url,
    ) -> Result<crate::ast::Resource, Error> {
        let mut req = client.request(reqwest::Method::GET, href.clone());

        req = req.header(reqwest::header::ACCEPT, WADL_MIME_TYPE);

        let res = req.send()?;

        let text = res.text()?;

        let application = parse_string(&text)?;

        let resource = application.get_resource_by_href(href).unwrap();

        Ok(resource.clone())
    }
}

#[derive(Debug)]
/// The error type for this crate.
pub enum Error {
    /// The URL is invalid.
    InvalidUrl,

    /// A reqwest error occurred.
    Reqwest(reqwest::Error),

    /// The URL could not be parsed.
    Url(url::ParseError),

    /// The JSON could not be parsed.
    Json(serde_json::Error),

    /// The WADL could not be parsed.
    Wadl(ParseError),

    /// The response status was not handled by the library.
    UnhandledStatus(reqwest::StatusCode),

    /// The response content type was not handled by the library.
    UnhandledContentType(Option<mime::Mime>),

    /// An I/O error occurred.
    Io(std::io::Error),
}

impl From<std::io::Error> for Error {
    fn from(err: std::io::Error) -> Self {
        Error::Io(err)
    }
}

impl From<serde_json::Error> for Error {
    fn from(err: serde_json::Error) -> Self {
        Error::Json(err)
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::InvalidUrl => write!(f, "Invalid URL"),
            Error::Reqwest(err) => write!(f, "Reqwest error: {}", err),
            Error::Url(err) => write!(f, "URL error: {}", err),
            Error::Json(err) => write!(f, "JSON error: {}", err),
            Error::Wadl(err) => write!(f, "WADL error: {}", err),
            Error::UnhandledContentType(Some(c)) => write!(f, "Unhandled content type: {}", c),
            Error::UnhandledContentType(None) => write!(f, "No content type"),
            Error::UnhandledStatus(s) => write!(f, "Unhandled status: {}", s),
            Error::Io(err) => write!(f, "IO error: {}", err),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::Reqwest(err) => Some(err),
            Error::Url(err) => Some(err),
            Error::Json(err) => Some(err),
            Error::Wadl(err) => Some(err),
            Error::Io(err) => Some(err),
            _ => None,
        }
    }
}

impl From<reqwest::Error> for Error {
    fn from(err: reqwest::Error) -> Self {
        Error::Reqwest(err)
    }
}

impl From<url::ParseError> for Error {
    fn from(err: url::ParseError) -> Self {
        Error::Url(err)
    }
}

impl From<ParseError> for Error {
    fn from(err: ParseError) -> Self {
        Error::Wadl(err)
    }
}