(plugin: Plugin<PluginState>, args: Value)
| 181 | } |
| 182 | |
| 183 | async fn currencyrate(plugin: Plugin<PluginState>, args: Value) -> Result<Value, anyhow::Error> { |
| 184 | let (currency, source) = match args { |
| 185 | Value::Array(values) => { |
| 186 | if values.len() > 2 { |
| 187 | return Err(anyhow!( |
| 188 | "Too many arguments: Expected at most 2 arguments, got {}", |
| 189 | values.len() |
| 190 | )); |
| 191 | } |
| 192 | let currency = values |
| 193 | .first() |
| 194 | .ok_or_else(|| anyhow!("Missing currency"))? |
| 195 | .as_str() |
| 196 | .ok_or_else(|| anyhow!("currency must be a string"))? |
| 197 | .to_uppercase(); |
| 198 | let source = values.get(1).and_then(|v| { |
| 199 | v.as_str() |
| 200 | .map(str::to_owned) |
| 201 | .or_else(|| v.as_number().map(std::string::ToString::to_string)) |
| 202 | }); |
| 203 | (currency, source) |
| 204 | } |
| 205 | Value::Object(map) => { |
| 206 | if map.len() > 2 { |
| 207 | return Err(anyhow!( |
| 208 | "Too many arguments: Expected at most 2 arguments, got {}", |
| 209 | map.len() |
| 210 | )); |
| 211 | } |
| 212 | let currency = map |
| 213 | .get("currency") |
| 214 | .ok_or_else(|| anyhow!("Missing currency"))? |
| 215 | .as_str() |
| 216 | .ok_or_else(|| anyhow!("currency must be a string"))? |
| 217 | .to_uppercase(); |
| 218 | let source = map.get("source").and_then(|v| { |
| 219 | v.as_str() |
| 220 | .map(str::to_owned) |
| 221 | .or_else(|| v.as_number().map(std::string::ToString::to_string)) |
| 222 | }); |
| 223 | (currency, source) |
| 224 | } |
| 225 | _ => return Err(anyhow!("Arguments must be an array or dictionary")), |
| 226 | }; |
| 227 | |
| 228 | let oracle = plugin.state().oracle.lock().await; |
| 229 | oracle.currency_requested(¤cy).await; |
| 230 | |
| 231 | let rate = match source { |
| 232 | Some(source_name) => oracle |
| 233 | .get_source_rate(¤cy, &source_name) |
| 234 | .await |
| 235 | .map_err(|e| anyhow!("Error getting rate from source: {e}"))?, |
| 236 | None => oracle |
| 237 | .get_median_rate(¤cy) |
| 238 | .await |
| 239 | .map_err(|e| anyhow!("Error getting median rate: {e}"))?, |
| 240 | }; |
nothing calls this directly
no test coverage detected