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
use reqwest;
use serde_json;
use std;
use std::str::FromStr;
use types::*;
use errors::*;
#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)]
pub enum SearchSortOrder {
Name,
Love,
Popular,
Newest,
Hot
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)]
pub enum SearchFilter {
Vr,
SoundOutput,
SoundInput,
Webcam,
MultiPass,
MusicStream
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct SearchParams<'a> {
pub string: &'a str,
pub sort_order: SearchSortOrder,
pub filters: Vec<SearchFilter>,
}
pub struct Client {
pub api_key: String,
pub rest_client: reqwest::Client,
}
impl FromStr for SearchSortOrder {
type Err = ();
fn from_str(s: &str) -> std::result::Result<SearchSortOrder,()> {
match s {
"Name" => Ok(SearchSortOrder::Name),
"Love" => Ok(SearchSortOrder::Love),
"Popular" => Ok(SearchSortOrder::Popular),
"Newest" => Ok(SearchSortOrder::Newest),
"Hot" => Ok(SearchSortOrder::Hot),
_ => Err(()),
}
}
}
impl FromStr for SearchFilter {
type Err = ();
fn from_str(s: &str) -> std::result::Result<SearchFilter,()> {
match s {
"VR" => Ok(SearchFilter::Vr),
"SoundOutput" => Ok(SearchFilter::SoundOutput),
"SoundInput" => Ok(SearchFilter::SoundInput),
"Webcam" => Ok(SearchFilter::Webcam),
"MultiPass" => Ok(SearchFilter::MultiPass),
"MusicStream" => Ok(SearchFilter::MusicStream),
_ => Err(()),
}
}
}
impl Client {
pub fn new(api_key: &str) -> Client {
Client {
api_key: api_key.to_string(),
rest_client: reqwest::Client::new(),
}
}
pub fn search(&self, params: &SearchParams) -> Result<Vec<String>> {
let query_str = format!("https://www.shadertoy.com/api/v1/shaders{}?sort={}&{}key={}",
match params.string.is_empty() {
false => format!("/query/{}", params.string),
true => String::from(""),
},
format!("{:?}", params.sort_order).to_lowercase(),
params.filters.iter().map(|f|
format!("filter={:?}&", f).to_lowercase()).collect::<String>(),
self.api_key);
let json_str = self.rest_client.get(&query_str).send()?.text()?;
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct SearchResult {
#[serde(default)]
#[serde(rename = "Error")]
error: String,
#[serde(default)]
#[serde(rename = "Shaders")]
shaders: u64,
#[serde(default)]
#[serde(rename = "Results")]
results: Vec<String>,
}
match serde_json::from_str::<SearchResult>(&json_str) {
Ok(r) => {
if !r.error.is_empty() {
bail!("Shadertoy REST search query returned error: {}", r.error);
}
Ok(r.results)
},
Err(err) => {
Err(Error::from(err)).chain_err(|| "JSON parsing of Shadertoy search result failed")
}
}
}
pub fn get_shader(&self, shader_id: &str) -> Result<Shader> {
let json_str = self.rest_client
.get(&format!("https://www.shadertoy.com/api/v1/shaders/{}?key={}", shader_id, self.api_key))
.send()?
.text()?;
#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
struct ShaderRoot {
#[serde(default)]
#[serde(rename = "Error")]
error: String,
#[serde(rename = "Shader")]
shader: Shader,
}
match serde_json::from_str::<ShaderRoot>(&json_str) {
Ok(r) => {
if !r.error.is_empty() {
bail!("Shadertoy REST shader query returned error: {}", r.error);
}
Ok(r.shader)
},
Err(err) => {
Err(Error::from(err)).chain_err(|| "JSON parsing of Shadertoy shader rootfailed")
}
}
}
}