Skip to content

Commit

Permalink
refactor: add json schema for config.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
Zel9278 committed Sep 28, 2024
1 parent f248902 commit 1331f0a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
config.toml
config.toml
2 changes: 2 additions & 0 deletions config.toml.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#:schema https://raw.githubusercontent.com/eoeo-org/cloudflare-ip-address-changer-rs/refs/heads/main/schema.json

[account]
auth_key = "Cloudflare Auth Key"
zone_id = "Cloudflare Zone ID"
Expand Down
60 changes: 60 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Config",
"type": "object",
"required": [
"account",
"dns"
],
"properties": {
"account": {
"$ref": "#/definitions/AccountConfig"
},
"dns": {
"$ref": "#/definitions/DnsConfig"
}
},
"definitions": {
"AccountConfig": {
"type": "object",
"required": [
"auth_key",
"zone_id"
],
"properties": {
"auth_key": {
"type": "string"
},
"zone_id": {
"type": "string"
}
}
},
"DnsConfig": {
"type": "object",
"required": [
"proxied",
"record",
"type"
],
"properties": {
"proxied": {
"type": "boolean"
},
"record": {
"type": "string"
},
"type": {
"$ref": "#/definitions/DnsType"
}
}
},
"DnsType": {
"type": "string",
"enum": [
"A",
"AAAA"
]
}
}
}
17 changes: 13 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use schemars::{schema_for, JsonSchema};
use schemars::{gen::SchemaSettings, JsonSchema};
use serde::{Deserialize, Serialize};
use toml;

Expand All @@ -17,19 +17,19 @@ impl DnsType {
}
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
pub(crate) struct Config {
pub(crate) account: AccountConfig,
pub(crate) dns: DnsConfig,
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub(crate) struct AccountConfig {
pub(crate) auth_key: String,
pub(crate) zone_id: String,
}

#[derive(Deserialize, Debug)]
#[derive(Serialize, Deserialize, JsonSchema, Debug)]
pub(crate) struct DnsConfig {
pub(crate) record: String,
pub(crate) r#type: DnsType,
Expand All @@ -43,3 +43,12 @@ impl Config {
config
}
}

pub fn generate_schema(is_debug: bool) {
let generator = SchemaSettings::draft07().into_generator();
let my_schema = generator.into_root_schema_for::<Config>();

if is_debug {
std::fs::write("schema.json", serde_json::to_string_pretty(&my_schema).unwrap()).unwrap();
}
}
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ async fn fetch_ip(client: &Client, url: &str) -> Result<String, Box<dyn Error>>

#[tokio::main]
async fn main() {
let is_debug = cfg!(debug_assertions);
config::generate_schema(is_debug);

let client = Client::new();

let mut config: Config = Config::new();
Expand Down

0 comments on commit 1331f0a

Please sign in to comment.