trello-to-deck/src/api_result.rs
Paul Campbell c8f9780226
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 2m14s
Test / build (map[name:stable]) (push) Successful in 2m57s
Release Please / Release-plz (push) Failing after 21s
feat(trello): add basics of trello config model
2024-12-12 22:34:51 +00:00

32 lines
938 B
Rust

//
use kxio::net::Response;
use crate::{e, s};
pub struct APIResult<T> {
pub text: String,
pub result: Result<T, kxio::net::Error>,
}
impl<T: for<'a> serde::Deserialize<'a>> APIResult<T> {
pub async fn new(response: kxio::net::Result<Response>) -> Self {
match response {
Ok(response) => {
let text = response.text().await.unwrap_or_default();
let text = if text.is_empty() { s!("null") } else { text };
let result = serde_json::from_str::<T>(&text)
.map_err(|e| e.to_string())
.map_err(|e| {
e!("{e}: {text}");
e
})
.map_err(kxio::net::Error::from);
Self { text, result }
}
Err(e) => Self {
text: s!(""),
result: Err(e),
},
}
}
}