use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct RfcGroup {
pub name: String,
#[serde(rename = "type")]
pub kind: String,
pub acronym: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RfcAuthor {
pub name: String,
pub email: String,
pub affiliation: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct RfcRevision {
pub name: String,
pub rev: String,
pub published: DateTime<Utc>,
url: String,
}
impl RfcRevision {
pub fn url(&self) -> String {
format!("https://datatracker.ietf.org{}", self.url)
}
}
#[derive(Deserialize, Serialize, Debug)]
pub struct Rfc {
pub name: String,
pub rev: String,
pub pages: u32,
pub time: String,
pub group: RfcGroup,
pub expires: Option<String>,
pub title: String,
#[serde(rename = "abstract")]
pub description: String,
pub aliases: Vec<String>,
pub state: String,
pub intended_std_level: Option<String>,
pub std_level: String,
pub authors: Vec<RfcAuthor>,
pub shepherd: Option<String>,
pub ad: Option<String>,
pub iesg_state: String,
pub rfceditor_state: Option<String>,
pub iana_review_state: Option<String>,
pub iana_action_state: Option<String>,
pub stream: Option<String>,
}
impl Rfc {
pub fn get_txt_url(&self) -> String {
format!("https://www.rfc-editor.org/rfc/{}.txt", self.name)
}
pub fn get_pdf_url(&self) -> String {
format!("https://www.rfc-editor.org/rfc/{}.txt.pdf", self.name)
}
pub fn get_html_url(&self) -> String {
format!("https://www.rfc-editor.org/rfc/{}.html", self.name)
}
}
pub async fn query_rfc(number: u32) -> Result<Rfc, reqwest::Error> {
Ok(reqwest::get(format!(
"https://datatracker.ietf.org/doc/rfc{}/doc.json",
number
))
.await?
.json()
.await?)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_query_rfc_failure() {
tokio_test::block_on(async {
let rfc = query_rfc(9999999).await;
assert!(rfc.is_err());
})
}
}