You've already forked authentication-service
mirror of
https://github.com/matrix-org/matrix-authentication-service.git
synced 2025-07-29 22:01:14 +03:00
Codegen enums from IANA registries
This commit is contained in:
241
crates/iana-codegen/src/jose.rs
Normal file
241
crates/iana-codegen/src/jose.rs
Normal file
@ -0,0 +1,241 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::EnumEntry;
|
||||
|
||||
#[derive(Debug, Deserialize, PartialEq, Eq)]
|
||||
enum Usage {
|
||||
#[serde(rename = "alg")]
|
||||
Alg,
|
||||
#[serde(rename = "enc")]
|
||||
Enc,
|
||||
#[serde(rename = "JWK")]
|
||||
Jwk,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
enum Requirements {
|
||||
Required,
|
||||
#[serde(rename = "Recommended+")]
|
||||
RecommendedPlus,
|
||||
Recommended,
|
||||
#[serde(rename = "Recommended-")]
|
||||
RecommendedMinus,
|
||||
Optional,
|
||||
Prohibited,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebEncryptionSignatureAlgorithm {
|
||||
#[serde(rename = "Algorithm Name")]
|
||||
name: String,
|
||||
#[serde(rename = "Algorithm Description")]
|
||||
description: String,
|
||||
#[serde(rename = "Algorithm Usage Location(s)")]
|
||||
usage: Usage,
|
||||
#[serde(rename = "JOSE Implementation Requirements")]
|
||||
requirements: Requirements,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
#[serde(rename = "Algorithm Analysis Document(s)")]
|
||||
analysis: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebEncryptionSignatureAlgorithm {
|
||||
const URL: &'static str =
|
||||
"https://www.iana.org/assignments/jose/web-signature-encryption-algorithms.csv";
|
||||
const SECTIONS: &'static [&'static str] =
|
||||
&["JsonWebSignatureAlgorithm", "JsonWebEncryptionAlgorithm"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
match self.usage {
|
||||
Usage::Alg => Some("JsonWebSignatureAlgorithm"),
|
||||
Usage::Enc => Some("JsonWebEncryptionAlgorithm"),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebEncryptionCompressionAlgorithm {
|
||||
#[serde(rename = "Compression Algorithm Value")]
|
||||
value: String,
|
||||
#[serde(rename = "Compression Algorithm Description")]
|
||||
description: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebEncryptionCompressionAlgorithm {
|
||||
const URL: &'static str =
|
||||
"https://www.iana.org/assignments/jose/web-encryption-compression-algorithms.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["JsonWebEncryptionCompressionAlgorithm"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("JsonWebEncryptionCompressionAlgorithm")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.value
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebKeyType {
|
||||
#[serde(rename = "\"kty\" Parameter Value")]
|
||||
value: String,
|
||||
#[serde(rename = "Key Type Description")]
|
||||
description: String,
|
||||
#[serde(rename = "JOSE Implementation Requirements")]
|
||||
requirements: Requirements,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebKeyType {
|
||||
const URL: &'static str = "https://www.iana.org/assignments/jose/web-key-types.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["JsonWebKeyType"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("JsonWebKeyType")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.value
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebKeyEllipticCurve {
|
||||
#[serde(rename = "Curve Name")]
|
||||
name: String,
|
||||
#[serde(rename = "Curve Description")]
|
||||
description: String,
|
||||
#[serde(rename = "JOSE Implementation Requirements")]
|
||||
requirements: Requirements,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebKeyEllipticCurve {
|
||||
const URL: &'static str = "https://www.iana.org/assignments/jose/web-key-elliptic-curve.csv";
|
||||
const SECTIONS: &'static [&'static str] =
|
||||
&["JsonWebKeyEcEllipticCurve", "JsonWebKeyOkpEllipticCurve"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
if self.name.starts_with("P-") || self.name == "secp256k1" {
|
||||
Some("JsonWebKeyEcEllipticCurve")
|
||||
} else {
|
||||
Some("JsonWebKeyOkpEllipticCurve")
|
||||
}
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebKeyUse {
|
||||
#[serde(rename = "Use Member Value")]
|
||||
value: String,
|
||||
#[serde(rename = "Use Description")]
|
||||
description: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebKeyUse {
|
||||
const URL: &'static str = "https://www.iana.org/assignments/jose/web-key-use.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["JsonWebKeyUse"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("JsonWebKeyUse")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.value
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct WebKeyOperation {
|
||||
#[serde(rename = "Key Operation Value")]
|
||||
name: String,
|
||||
#[serde(rename = "Key Operation Description")]
|
||||
description: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for WebKeyOperation {
|
||||
const URL: &'static str = "https://www.iana.org/assignments/jose/web-key-operations.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["JsonWebKeyOperation"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("JsonWebKeyOperation")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(&self.description)
|
||||
}
|
||||
}
|
219
crates/iana-codegen/src/main.rs
Normal file
219
crates/iana-codegen/src/main.rs
Normal file
@ -0,0 +1,219 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use std::{collections::HashMap, fmt::Display, path::PathBuf, sync::Arc};
|
||||
|
||||
use reqwest::Client;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tracing::Level;
|
||||
|
||||
pub mod jose;
|
||||
pub mod oauth;
|
||||
pub mod traits;
|
||||
|
||||
#[derive(Debug)]
|
||||
struct File {
|
||||
client: Arc<Client>,
|
||||
registry_name: &'static str,
|
||||
registry_url: &'static str,
|
||||
sections: Vec<&'static str>,
|
||||
sources: Vec<&'static str>,
|
||||
items: HashMap<&'static str, Vec<EnumMember>>,
|
||||
}
|
||||
|
||||
fn resolve_path(relative: PathBuf) -> PathBuf {
|
||||
let crate_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let workspace_root = crate_root.parent().unwrap().parent().unwrap();
|
||||
workspace_root.join(relative)
|
||||
}
|
||||
|
||||
impl File {
|
||||
#[tracing::instrument(skip(client))]
|
||||
fn new(registry_name: &'static str, registry_url: &'static str, client: Arc<Client>) -> Self {
|
||||
tracing::info!("Generating file from IANA registry");
|
||||
Self {
|
||||
client,
|
||||
registry_name,
|
||||
registry_url,
|
||||
sources: Vec::new(),
|
||||
sections: Vec::new(),
|
||||
items: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all, fields(url))]
|
||||
async fn load<T: EnumEntry>(mut self) -> anyhow::Result<Self> {
|
||||
tracing::Span::current().record("url", &T::URL);
|
||||
self.sections.extend_from_slice(T::SECTIONS);
|
||||
self.sources.push(T::URL);
|
||||
for (key, value) in T::fetch(&self.client).await? {
|
||||
self.items.entry(key).or_default().push(value);
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip_all)]
|
||||
async fn write(&self, path: PathBuf) -> anyhow::Result<()> {
|
||||
let mut file = tokio::fs::OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open(path)
|
||||
.await?;
|
||||
|
||||
tracing::info!("Writing file");
|
||||
file.write_all(format!("{}", self).as_bytes()).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for File {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
writeln!(
|
||||
f,
|
||||
r#"// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Enums from the {:?} IANA registry
|
||||
//! See <{}>
|
||||
//!
|
||||
//! Generated from:"#,
|
||||
self.registry_name, self.registry_url,
|
||||
)?;
|
||||
|
||||
for source in &self.sources {
|
||||
writeln!(f, "//! - <{}>", source)?;
|
||||
}
|
||||
|
||||
writeln!(
|
||||
f,
|
||||
r#"
|
||||
// Do not edit this file manually
|
||||
|
||||
use schemars::JsonSchema;
|
||||
use serde::{{Deserialize, Serialize}};
|
||||
"#
|
||||
)?;
|
||||
|
||||
for key in &self.sections {
|
||||
let list = if let Some(list) = self.items.get(key) {
|
||||
list
|
||||
} else {
|
||||
continue;
|
||||
};
|
||||
|
||||
writeln!(
|
||||
f,
|
||||
"#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]"
|
||||
)?;
|
||||
write!(f, "pub enum {} {{", key)?;
|
||||
for member in list {
|
||||
writeln!(f)?;
|
||||
if let Some(description) = &member.description {
|
||||
writeln!(f, " /// {}", description)?;
|
||||
}
|
||||
writeln!(f, " #[serde(rename = \"{}\")]", member.value)?;
|
||||
writeln!(f, " {},", member.enum_name)?;
|
||||
}
|
||||
writeln!(f, "}}")?;
|
||||
writeln!(f)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
use self::{jose::*, oauth::*, traits::*};
|
||||
|
||||
#[tracing::instrument(skip(client))]
|
||||
async fn generate_jose(client: &Arc<Client>, path: PathBuf) -> anyhow::Result<()> {
|
||||
let path = resolve_path(path);
|
||||
let client = client.clone();
|
||||
|
||||
let file = File::new(
|
||||
"JSON Object Signing and Encryption",
|
||||
"https://www.iana.org/assignments/jose/jose.xhtml",
|
||||
client.clone(),
|
||||
)
|
||||
.load::<WebEncryptionSignatureAlgorithm>()
|
||||
.await?
|
||||
.load::<WebEncryptionCompressionAlgorithm>()
|
||||
.await?
|
||||
.load::<WebKeyType>()
|
||||
.await?
|
||||
.load::<WebKeyEllipticCurve>()
|
||||
.await?
|
||||
.load::<WebKeyUse>()
|
||||
.await?
|
||||
.load::<WebKeyOperation>()
|
||||
.await?;
|
||||
|
||||
file.write(path).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tracing::instrument(skip(client))]
|
||||
async fn generate_oauth(client: &Arc<Client>, path: PathBuf) -> anyhow::Result<()> {
|
||||
let path = resolve_path(path);
|
||||
let client = client.clone();
|
||||
|
||||
let file = File::new(
|
||||
"OAuth Parameters",
|
||||
"https://www.iana.org/assignments/jose/jose.xhtml",
|
||||
client.clone(),
|
||||
)
|
||||
.load::<TokenTypeHint>()
|
||||
.await?
|
||||
.load::<AuthorizationEndpointResponseType>()
|
||||
.await?
|
||||
.load::<TokenEndpointAuthenticationMethod>()
|
||||
.await?
|
||||
.load::<PkceCodeChallengeMethod>()
|
||||
.await?;
|
||||
|
||||
file.write(path).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
tracing_subscriber::fmt()
|
||||
.with_max_level(Level::INFO)
|
||||
.pretty()
|
||||
.init();
|
||||
|
||||
let client = Client::builder().user_agent("iana-parser/0.0.1").build()?;
|
||||
let client = Arc::new(client);
|
||||
|
||||
let iana_crate_root = PathBuf::from("crates/iana/");
|
||||
|
||||
generate_jose(&client, iana_crate_root.join("src/jose.rs")).await?;
|
||||
generate_oauth(&client, iana_crate_root.join("src/oauth.rs")).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
116
crates/iana-codegen/src/oauth.rs
Normal file
116
crates/iana-codegen/src/oauth.rs
Normal file
@ -0,0 +1,116 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::EnumEntry;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TokenTypeHint {
|
||||
#[serde(rename = "Hint Value")]
|
||||
name: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for TokenTypeHint {
|
||||
const URL: &'static str =
|
||||
"https://www.iana.org/assignments/oauth-parameters/token-type-hint.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["OAuthTokenTypeHint"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("OAuthTokenTypeHint")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct AuthorizationEndpointResponseType {
|
||||
#[serde(rename = "Name")]
|
||||
name: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for AuthorizationEndpointResponseType {
|
||||
const URL: &'static str = "https://www.iana.org/assignments/oauth-parameters/endpoint.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["OAuthAuthorizationEndpointResponseType"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("OAuthAuthorizationEndpointResponseType")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct TokenEndpointAuthenticationMethod {
|
||||
#[serde(rename = "Token Endpoint Authentication Method Name")]
|
||||
name: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for TokenEndpointAuthenticationMethod {
|
||||
const URL: &'static str =
|
||||
"https://www.iana.org/assignments/oauth-parameters/token-endpoint-auth-method.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["OAuthTokenEndpointAuthenticationMethod"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("OAuthTokenEndpointAuthenticationMethod")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct PkceCodeChallengeMethod {
|
||||
#[serde(rename = "Code Challenge Method Parameter Name")]
|
||||
name: String,
|
||||
#[serde(rename = "Change Controller")]
|
||||
change_controller: String,
|
||||
#[serde(rename = "Reference")]
|
||||
reference: String,
|
||||
}
|
||||
|
||||
impl EnumEntry for PkceCodeChallengeMethod {
|
||||
const URL: &'static str =
|
||||
"https://www.iana.org/assignments/oauth-parameters/pkce-code-challenge-method.csv";
|
||||
const SECTIONS: &'static [&'static str] = &["PkceCodeChallengeMethod"];
|
||||
|
||||
fn key(&self) -> Option<&'static str> {
|
||||
Some("PkceCodeChallengeMethod")
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
&self.name
|
||||
}
|
||||
}
|
74
crates/iana-codegen/src/traits.rs
Normal file
74
crates/iana-codegen/src/traits.rs
Normal file
@ -0,0 +1,74 @@
|
||||
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use anyhow::Context;
|
||||
use async_trait::async_trait;
|
||||
use convert_case::{Case, Casing};
|
||||
use reqwest::Client;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct EnumMember {
|
||||
pub value: String,
|
||||
pub description: Option<String>,
|
||||
pub enum_name: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait EnumEntry: DeserializeOwned + Send + Sync {
|
||||
const URL: &'static str;
|
||||
const SECTIONS: &'static [&'static str];
|
||||
|
||||
fn key(&self) -> Option<&'static str>;
|
||||
fn name(&self) -> &str;
|
||||
fn description(&self) -> Option<&str> {
|
||||
None
|
||||
}
|
||||
fn enum_name(&self) -> String {
|
||||
self.name().replace('+', "_").to_case(Case::Pascal)
|
||||
}
|
||||
|
||||
async fn fetch(client: &Client) -> anyhow::Result<Vec<(&'static str, EnumMember)>> {
|
||||
tracing::info!("Fetching CSV");
|
||||
let body = client
|
||||
.get(Self::URL)
|
||||
.send()
|
||||
.await
|
||||
.context(format!("can't the CSV at {}", Self::URL))?
|
||||
.bytes()
|
||||
.await
|
||||
.context(format!("can't the CSV body at {}", Self::URL))?;
|
||||
|
||||
let parsed: Result<Vec<_>, _> = csv::Reader::from_reader(body.as_ref())
|
||||
.into_deserialize()
|
||||
.filter_map(|item: Result<Self, _>| {
|
||||
item.map(|item| {
|
||||
item.key().map(|key| {
|
||||
(
|
||||
key,
|
||||
EnumMember {
|
||||
value: item.name().to_string(),
|
||||
description: item.description().map(ToString::to_string),
|
||||
enum_name: item.enum_name(),
|
||||
},
|
||||
)
|
||||
})
|
||||
})
|
||||
.transpose()
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ok(parsed.context(format!("can't parse the CSV at {}", Self::URL))?)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user