File: lib.rs

package info (click to toggle)
icann-rdap-client 0.0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 988 kB
  • sloc: makefile: 2
file content (113 lines) | stat: -rw-r--r-- 2,884 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#![allow(dead_code)] // TODO remove this at some point
#![allow(rustdoc::bare_urls)]
#![doc = include_str!("../README.md")]
use std::{fmt::Display, sync::PoisonError};

use {
    iana::iana_request::IanaResponseError,
    icann_rdap_common::{
        dns_types::DomainNameError, httpdata::HttpData, iana::BootstrapRegistryError,
        response::RdapResponseError,
    },
    thiserror::Error,
};

pub mod gtld;
pub mod http;
pub mod iana;
pub mod md;
pub mod rdap;
pub mod rpsl;

/// Basics necessary for a simple clients.
pub mod prelude {
    #[doc(inline)]
    pub use crate::http::create_client;
    #[doc(inline)]
    pub use crate::http::ClientConfig;
    #[doc(inline)]
    pub use crate::iana::MemoryBootstrapStore;
    #[doc(inline)]
    pub use crate::rdap::rdap_bootstrapped_request;
    #[doc(inline)]
    pub use crate::rdap::rdap_request;
    #[doc(inline)]
    pub use crate::rdap::rdap_url_request;
    #[doc(inline)]
    pub use crate::rdap::QueryType;
    #[doc(inline)]
    pub use crate::RdapClientError;
}

/// Error returned by RDAP client functions and methods.
#[derive(Error, Debug)]
pub enum RdapClientError {
    #[error("Query value is not valid.")]
    InvalidQueryValue,

    #[error("Ambiguous query type.")]
    AmbiguousQueryType,

    #[error(transparent)]
    Response(#[from] RdapResponseError),

    #[error(transparent)]
    Client(#[from] reqwest::Error),

    #[error("Error parsing response")]
    ParsingError(Box<ParsingErrorInfo>),

    #[error(transparent)]
    Json(#[from] serde_json::Error),

    #[error("RwLock Poison Error")]
    Poison,

    #[error("Bootstrap unavailable")]
    BootstrapUnavailable,

    #[error(transparent)]
    BootstrapError(#[from] BootstrapRegistryError),

    #[error(transparent)]
    IanaResponse(#[from] IanaResponseError),

    #[error(transparent)]
    IoError(#[from] std::io::Error),

    #[error(transparent)]
    DomainNameError(#[from] DomainNameError),
}

impl<T> From<PoisonError<T>> for RdapClientError {
    fn from(_err: PoisonError<T>) -> Self {
        Self::Poison
    }
}

/// Describes the error that occurs when parsing RDAP responses.
#[derive(Debug)]
pub struct ParsingErrorInfo {
    pub text: String,
    pub http_data: HttpData,
    pub error: serde_json::Error,
}

impl Display for ParsingErrorInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Error: {}\n,Content Length: {}\nContent Type: {}\nUrl: {}\nText:\n{}\n",
            self.error,
            self.http_data
                .content_length
                .map_or("No content length given".to_string(), |n| n.to_string()),
            self.http_data
                .content_type
                .clone()
                .unwrap_or("No content type given".to_string()),
            self.http_data.host,
            self.text
        )
    }
}