trello-to-deck/src/api_result.rs
Paul Campbell 6d234609e7
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 2m18s
Test / build (map[name:stable]) (push) Successful in 2m17s
Release Please / Release-plz (push) Failing after 27s
feat: add command 'nextcloud card add-attachment'
2024-12-18 17:21:01 +00:00

46 lines
1.2 KiB
Rust

//
use kxio::{net::Response, print::Printer};
use crate::{e, s};
#[derive(Debug)]
pub(crate) struct APIResult<T>
where
T: for<'a> serde::Deserialize<'a>,
{
pub(crate) text: String,
pub(crate) result: Result<T, kxio::Error>,
}
impl<T> APIResult<T>
where
T: for<'a> serde::Deserialize<'a>,
{
pub async fn new(response: kxio::net::Result<Response>, prt: &Printer) -> 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!(prt, "{e}: {text}");
e
})
.map_err(kxio::net::Error::from)
.map_err(kxio::Error::from);
Self { text, result }
}
Err(e) => {
e!(prt, "err: {e:#?}");
Self::error(e.into())
}
}
}
pub fn error(err: kxio::Error) -> Self {
Self {
text: s!(""),
result: Err(err),
}
}
}