diff --git a/.gitignore b/.gitignore index db38427..4a3b37d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /target -config.toml \ No newline at end of file +config.toml diff --git a/config.toml.example b/config.toml.example index 13cc0f2..56d2e45 100644 --- a/config.toml.example +++ b/config.toml.example @@ -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" diff --git a/schema.json b/schema.json new file mode 100644 index 0000000..833cb4c --- /dev/null +++ b/schema.json @@ -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" + ] + } + } +} \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index 735455e..11fb3ca 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use schemars::{schema_for, JsonSchema}; +use schemars::{gen::SchemaSettings, JsonSchema}; use serde::{Deserialize, Serialize}; use toml; @@ -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, @@ -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::(); + + if is_debug { + std::fs::write("schema.json", serde_json::to_string_pretty(&my_schema).unwrap()).unwrap(); + } +} diff --git a/src/main.rs b/src/main.rs index e0932f6..8529121 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,6 +33,9 @@ async fn fetch_ip(client: &Client, url: &str) -> Result> #[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();