From e6711968cc63e5cc1514d9a112f832287d24e7ef Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Thu, 28 Apr 2022 17:04:43 +0530 Subject: [PATCH] Expose types, Correct malformed responses from mikrotik. added repository url --- Cargo.toml | 1 + src/interface/mod.rs | 1 + src/interface/types.rs | 4 ++-- src/ip/mod.rs | 2 ++ src/ip/types.rs | 22 +++++++++++++++------- src/lib.rs | 1 + src/serde_helpers/mod.rs | 14 ++++++++++++++ src/system/mod.rs | 1 + 8 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 src/serde_helpers/mod.rs diff --git a/Cargo.toml b/Cargo.toml index f1a0114..059fd6a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.0.6" edition = "2021" description = "REST API Wrapper for Mikrotik API" license = "MIT" +repository = "https://github.com/ishanjain28/mikrotik" [dev-dependencies] tokio-test = "0.4.2" diff --git a/src/interface/mod.rs b/src/interface/mod.rs index 6424145..05859ef 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -2,5 +2,6 @@ mod interface; mod types; pub use interface::*; +pub use types::*; const BASE: &str = "rest/interface"; diff --git a/src/interface/types.rs b/src/interface/types.rs index 241ee41..8b82167 100644 --- a/src/interface/types.rs +++ b/src/interface/types.rs @@ -7,7 +7,7 @@ pub struct Interface { #[serde(rename = ".id")] pub id: String, #[serde(rename = "actual-mtu")] - pub actual_mtu: String, + pub actual_mtu: Option, pub comment: Option, #[serde(rename = "default-name")] pub default_name: Option, @@ -29,7 +29,7 @@ pub struct Interface { pub mac_address: Option, #[serde(rename = "max-l2mtu")] pub max_l2mtu: Option, - pub mtu: String, + pub mtu: Option, pub name: String, pub running: String, #[serde(rename = "rx-byte")] diff --git a/src/ip/mod.rs b/src/ip/mod.rs index 567674e..ec1a3b3 100644 --- a/src/ip/mod.rs +++ b/src/ip/mod.rs @@ -1,4 +1,6 @@ pub mod dhcp_server; mod types; +pub use types::*; + const BASE: &str = "rest/ip"; diff --git a/src/ip/types.rs b/src/ip/types.rs index dbd196b..0adb669 100644 --- a/src/ip/types.rs +++ b/src/ip/types.rs @@ -1,3 +1,4 @@ +use crate::serde_helpers::deserialize_bool; use serde_derive::Deserialize; use serde_derive::Serialize; @@ -9,10 +10,13 @@ pub struct DhcpServer { #[serde(rename = "address-pool")] pub address_pool: String, pub authoritative: String, - pub disabled: String, - pub dynamic: String, + #[serde(deserialize_with = "deserialize_bool")] + pub disabled: bool, + #[serde(deserialize_with = "deserialize_bool")] + pub dynamic: bool, pub interface: String, - pub invalid: String, + #[serde(deserialize_with = "deserialize_bool")] + pub invalid: bool, #[serde(rename = "lease-script")] pub lease_script: String, #[serde(rename = "lease-time")] @@ -30,12 +34,14 @@ pub struct Lease { pub address: String, #[serde(rename = "address-lists")] pub address_lists: String, - pub blocked: String, + #[serde(deserialize_with = "deserialize_bool")] + pub blocked: bool, #[serde(rename = "client-id")] pub client_id: Option, #[serde(rename = "dhcp-option")] pub dhcp_option: String, - pub disabled: String, + #[serde(deserialize_with = "deserialize_bool")] + pub disabled: bool, pub dynamic: String, #[serde(rename = "host-name")] pub host_name: Option, @@ -43,7 +49,8 @@ pub struct Lease { pub last_seen: String, #[serde(rename = "mac-address")] pub mac_address: String, - pub radius: String, + #[serde(deserialize_with = "deserialize_bool")] + pub radius: bool, pub server: String, pub status: String, #[serde(rename = "active-address")] @@ -72,7 +79,8 @@ pub struct Network { #[serde(rename = "dns-server")] pub dns_server: String, pub domain: Option, - pub dynamic: String, + #[serde(deserialize_with = "deserialize_bool")] + pub dynamic: bool, pub gateway: String, pub netmask: String, #[serde(rename = "ntp-server")] diff --git a/src/lib.rs b/src/lib.rs index ef40a44..dfe90cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ mod client; pub mod interface; pub mod ip; +mod serde_helpers; pub mod system; pub use client::*; diff --git a/src/serde_helpers/mod.rs b/src/serde_helpers/mod.rs new file mode 100644 index 0000000..81f09d0 --- /dev/null +++ b/src/serde_helpers/mod.rs @@ -0,0 +1,14 @@ +use serde::{de::Error, Deserialize, Deserializer}; + +pub fn deserialize_bool<'de, D>(deserializer: D) -> Result +where + D: Deserializer<'de>, +{ + let s: &str = Deserialize::deserialize(deserializer)?; + + match s { + "true" => Ok(true), + "false" => Ok(false), + _ => Err(Error::unknown_variant(s, &["true", "false"])), + } +} diff --git a/src/system/mod.rs b/src/system/mod.rs index ac081b0..e9b0ecd 100644 --- a/src/system/mod.rs +++ b/src/system/mod.rs @@ -2,5 +2,6 @@ mod health; mod types; pub use health::*; +pub use types::*; const BASE: &str = "rest/system";