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
use regex::Regex;
use serde::{Serialize, Deserialize};
use crate::types::{PlotParameters, DiagramError, Format};
use std::error::Error;
use crate::types::WSDEnum;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
struct WebSequenceDiagramResponse {
img: String,
errors: Vec<String>,
}
pub struct WSDResult {
pub diagram: Vec<u8>,
pub errors: Vec<DiagramError>,
pub actual_format: Format
}
pub fn get_diagram(spec: &str, parameters: &PlotParameters) -> Result<WSDResult, Box<Error>> {
let mut params = vec![
("message".to_owned(), spec.to_owned()),
("style".to_owned(), parameters.style.wsd_value()),
("format".to_owned(), parameters.format.wsd_value()),
("apiVersion".to_owned(), "1".to_owned()),
];
if let Some(ref api_key) = parameters.api_key {
params.push(("apikey".to_owned(), api_key.clone()));
}
if let Some(ref paper_size) = parameters.paper_size {
params.push(("paper".to_owned(), paper_size.wsd_value()));
}
if let Some(ref paper_orientation) = parameters.paper_orientation {
params.push(("landscape".to_owned(), paper_orientation.wsd_value()));
}
if let Some(ref scale) = parameters.scale {
params.push(("scale".to_owned(), format!("{}", scale)));
}
let first_request_url = "http://www.websequencediagrams.com/index.php";
let first_response = reqwest::Client::new()
.post(first_request_url)
.form(¶ms)
.send();
let first_response: WebSequenceDiagramResponse = match first_response {
Ok(mut r) => {
let mut v = vec![];
std::io::copy(&mut r, &mut v)
.map_err(|err| format!("error reading response from server {} : {:?}", first_request_url, err))?;
if !r.status().is_success() {
return Err(format!(
"Error response from server: {} HTTP code={:?} response={}",
first_request_url,
r.status(),
String::from_utf8_lossy(&v)
).into())
}
serde_json::from_reader(&v[..])
.map_err(|err|
format!(
"Cannot deserialize websequencegiagram response: {:?} Response: {}",
err,
String::from_utf8_lossy(&v)
)
)
}
Err(err) => {
Err(format!("error sending request to {} : {}", first_request_url, err))
}
}?;
let actual_format = determine_actual_format(&first_response.img)
.map_err(|err| format!("cannot determine actual format from url: {} : {:?}", &first_response.img, err))?;
let second_request_url = format!("http://www.websequencediagrams.com/index.php{}", first_response.img);
let mut second_response = reqwest::Client::new()
.get(&second_request_url)
.send()
.map_err(|err| format!("Error sending request for diagram to {} : {:?}", second_request_url, err))?;
if !second_response.status().is_success() {
return Err(format!("Request for diagram was unsuccesfull url: {} code: {:?}", second_request_url, second_response.status()).into());
}
let mut data = vec![];
std::io::copy(&mut second_response, &mut data)
.map_err(|err|
format!("Error reading diagram from {} : {:?}", second_request_url, err)
)?;
let errors_parsed = first_response.errors
.iter()
.map(|error| DiagramError::from_wsd_error_str(error));
let mut errors = vec![];
for error in errors_parsed {
match error {
Ok(error) => errors.push(error),
Err(err) => return Err(format!("cannot parse wsd error message {:?}",err).into())
}
}
Ok(WSDResult {
diagram: data,
errors,
actual_format,
})
}
fn determine_actual_format(url: &str) -> Result<Format, Box<Error>> {
let re = Regex::new(r"(?ix)
\?
(?P<format>\w+) # format
=
.*
")?;
let caps = if let Some(caps) = re.captures(url) {
caps
} else {
return Err("Error parsing diagram url.".into())
};
let format_str = if let Some(format_match) = caps.name("format"){
format_match.as_str()
} else {
return Err("Error parsing diagram url. Group `format` not found".into())
};
match Format::from_str(format_str) {
Some(x) => Ok(x),
None => {
Err(format!("unknown format in diagram url. Known formats are: {}. Got: {}", Format::help_str(), format_str).into())
}
}
}
#[cfg(test)]
mod tests {
use crate::types::{DiagramError, Format};
use crate::client::determine_actual_format;
#[test]
fn determine_actual_format_test() {
assert_eq!(determine_actual_format("?png=mscKTO107").unwrap(), Format::Png);
assert_eq!(determine_actual_format("?pdf=mscKTO107").unwrap(), Format::Pdf);
assert_eq!(determine_actual_format("?svg=mscKTO107").unwrap(), Format::Svg);
assert!(determine_actual_format("?xxx=mscKTO107").is_err());
}
}