Skip to main content

tapo/responses/
decodable_result_ext.rs

1use base64::{Engine as _, engine::general_purpose};
2
3use crate::error::Error;
4
5/// Implemented by all Device Info Result variations.
6pub(crate) trait DecodableResultExt
7where
8    Self: Sized,
9{
10    /// Decodes a base64 encoded string from the result.
11    fn decode(self) -> Result<Self, Error>;
12}
13
14impl DecodableResultExt for serde_json::Value {
15    fn decode(self) -> Result<Self, Error> {
16        Ok(self)
17    }
18}
19
20pub(crate) fn decode_value(value: &str) -> anyhow::Result<String> {
21    let decoded_bytes = general_purpose::STANDARD.decode(value)?;
22    Ok(std::str::from_utf8(&decoded_bytes)?.to_string())
23}