darkfi_serial/types/
url.rs1use std::io::{Error, Read, Result, Write};
20use url::Url;
21
22#[cfg(feature = "async")]
23use crate::{AsyncDecodable, AsyncEncodable};
24#[cfg(feature = "async")]
25use async_trait::async_trait;
26#[cfg(feature = "async")]
27use futures_lite::{AsyncRead, AsyncWrite};
28
29use crate::{Decodable, Encodable};
30
31impl Encodable for Url {
32 fn encode<S: Write>(&self, s: &mut S) -> Result<usize> {
33 self.as_str().encode(s)
34 }
35}
36
37#[cfg(feature = "async")]
38#[async_trait]
39impl AsyncEncodable for Url {
40 async fn encode_async<S: AsyncWrite + Unpin + Send>(&self, s: &mut S) -> Result<usize> {
41 self.as_str().encode_async(s).await
42 }
43}
44
45impl Decodable for Url {
46 fn decode<D: Read>(d: &mut D) -> Result<Self> {
47 let s: String = Decodable::decode(d)?;
48 match Url::parse(&s) {
49 Ok(v) => Ok(v),
50 Err(e) => Err(Error::other(e)),
51 }
52 }
53}
54
55#[cfg(feature = "async")]
56#[async_trait]
57impl AsyncDecodable for Url {
58 async fn decode_async<D: AsyncRead + Unpin + Send>(d: &mut D) -> Result<Self> {
59 let s: String = AsyncDecodable::decode_async(d).await?;
60 match Url::parse(&s) {
61 Ok(v) => Ok(v),
62 Err(e) => Err(Error::other(e)),
63 }
64 }
65}