api_free_reseau_fr/
lib.rs

1#![doc = include_str!("../README.md")]
2
3use derive_more::{Deref, DerefMut, Display, Error, From};
4
5pub(crate) const BASE_URL: &str = "https://www.free-reseau.fr/outils/api";
6
7pub(crate) mod client;
8pub(crate) mod parser;
9pub(crate) mod request;
10
11pub use client::Client;
12pub use request::*;
13
14/// Returned by the [`Client`] is a error occured
15#[derive(Debug, Display, From, Error)]
16pub enum Error {
17    ///Return if a [`reqwest`] occured
18    #[display("{_0}")]
19    Reqwest(#[from] reqwest::Error),
20    ///Return when target is non existent
21    #[display("Nonexistent {_0} was provided `{_1}`")]
22    NonExistent(TargetType, String),
23}
24
25/// Store a NRA as a String. For example: `mon75`
26#[derive(Debug, Clone, Display, Deref, DerefMut, PartialEq, Eq, PartialOrd, Ord)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
28pub struct NRA(pub String);
29
30impl NRA {
31    /// Create a new NRA with the provided fields
32    pub fn new(target: impl Into<String>, departement_number: u16) -> Self {
33        Self(format!("{}{}", target.into(), departement_number,))
34    }
35}
36impl From<&str> for NRA {
37    fn from(value: &str) -> Self {
38        Self(value.to_string())
39    }
40}
41
42/// Store a DSLAM String. For example: `mon75-1`
43#[derive(Debug, Clone, Display, Deref, DerefMut, PartialEq, Eq, PartialOrd, Ord)]
44#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
45pub struct DSLAM(pub String);
46
47impl DSLAM {
48    /// Create a new DSLAM with the provided fields
49    pub fn new(target: impl Into<String>, departement_number: u16, target_id: u16) -> Self {
50        Self(format!(
51            "{}{}-{}",
52            target.into(),
53            departement_number,
54            target_id
55        ))
56    }
57}
58
59impl From<&str> for DSLAM {
60    fn from(value: &str) -> Self {
61        Self(value.to_string())
62    }
63}
64
65/// Store a Departement. For example: `75`
66#[derive(Debug, Clone, Copy, Display, Deref, DerefMut, PartialEq, Eq, PartialOrd, Ord)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub struct Departement(pub u16);
69
70impl Departement {
71    /// Create a new Departement with the provided field
72    pub fn new(target: u16) -> Self {
73        Self(target)
74    }
75}
76
77/// Represent the 3 types of request possible
78#[derive(Debug, Display, From, Error, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
79#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
80pub enum TargetType {
81    #[display("DSLAM")]
82    DSLAM,
83    #[display("NRA")]
84    NRA,
85    #[display("DEPARTEMENT")]
86    DEPARTEMENT,
87}
88
89/// Status of a [`Departement`]
90#[derive(Debug, Display, From, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub enum DepartementStatus {
93    #[display("OK")]
94    OK,
95    #[display("DEGRADED")]
96    DEGRADED,
97}
98
99impl From<bool> for DepartementStatus {
100    fn from(value: bool) -> Self {
101        match value {
102            true => DepartementStatus::OK,
103            false => DepartementStatus::DEGRADED,
104        }
105    }
106}
107
108/// Status of a [`NRA`]
109#[derive(Debug, Display, From, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
110#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
111pub enum NRAStatus {
112    #[display("OK")]
113    OK,
114    #[display("DEGRADED")]
115    DEGRADED,
116}
117
118impl From<bool> for NRAStatus {
119    fn from(value: bool) -> Self {
120        match value {
121            true => NRAStatus::OK,
122            false => NRAStatus::DEGRADED,
123        }
124    }
125}
126
127/// Status of a [`DSLAM`]
128#[derive(Debug, Display, From, Error, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
130pub enum DSLAMStatus {
131    #[display("ONLINE")]
132    ONLINE,
133    #[display("OFFLINE")]
134    OFFLINE,
135}
136
137impl From<bool> for DSLAMStatus {
138    fn from(value: bool) -> Self {
139        match value {
140            true => DSLAMStatus::ONLINE,
141            false => DSLAMStatus::OFFLINE,
142        }
143    }
144}
145
146/// Result of a API request
147#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
148#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
149pub enum Response {
150    /// Reponse for a [`DSLAM`]
151    DSLAM {
152        /// target fullname
153        target: String,
154        target_status: DSLAMStatus,
155    },
156    /// Reponse for a [`NRA`]
157    NRA {
158        /// target fullname
159        target: String,
160        target_status: NRAStatus,
161    },
162    /// Reponse for a [`Departement`]
163    DEPARTEMENT {
164        /// target fullname
165        target: String,
166        target_status: DepartementStatus,
167    },
168    /// Error returned
169    Err {
170        /// target fullname
171        target: String,
172        /// target type
173        target_type: TargetType,
174        /// erro type
175        error_message: String,
176    },
177}