Skip to content

Commit

Permalink
Implement FromStr and Display based on Serialize and Deserialize for …
Browse files Browse the repository at this point in the history
…all configuration enum types
  • Loading branch information
pragmatrix committed Apr 19, 2022
1 parent abfe79f commit b255739
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/rest/config/devices.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{FolderId, Kibibytes, KibibytesPerSecond, PortNumber};
use crate::utils::impl_from_str_and_display;
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -96,6 +97,8 @@ pub enum Compression {
Never,
}

impl_from_str_and_display!(Compression);

impl Default for Compression {
fn default() -> Self {
Self::Metadata
Expand All @@ -110,6 +113,8 @@ pub enum Address {
Static(Url),
}

impl_from_str_and_display!(Address);

mod strings {
use crate::utils::named_unit_variant;
named_unit_variant!(dynamic);
Expand Down Expand Up @@ -189,4 +194,11 @@ mod tests {
"untrusted": false,
"remoteGUIPort": 0
}"#;

#[test]
fn address_from_str_to_display() {
let addr_str = "tcp://host:22000";
let addr: Address = addr_str.parse().unwrap();
assert_eq!(addr.to_string(), addr_str);
}
}
11 changes: 11 additions & 0 deletions src/rest/config/folders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{Count, FolderId, Kibibytes, MinDiskFree, Seconds};
use crate::utils::impl_from_str_and_display;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

Expand Down Expand Up @@ -172,6 +173,8 @@ pub enum FilesystemType {
Fake,
}

impl_from_str_and_display!(FilesystemType);

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[serde(rename_all(serialize = "lowercase", deserialize = "lowercase"))]
pub enum Type {
Expand All @@ -192,6 +195,8 @@ pub enum Type {
ReceiveEncrypted,
}

impl_from_str_and_display!(Type);

#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
#[serde(rename_all(serialize = "camelCase", deserialize = "camelCase"))]
pub struct Device {
Expand Down Expand Up @@ -236,6 +241,8 @@ pub enum PullOrder {
NewestFirst,
}

impl_from_str_and_display!(PullOrder);

impl Default for PullOrder {
fn default() -> Self {
Self::Random
Expand All @@ -258,6 +265,8 @@ pub enum BlockPullOrder {
InOrder,
}

impl_from_str_and_display!(BlockPullOrder);

impl Default for BlockPullOrder {
fn default() -> Self {
Self::Standard
Expand All @@ -275,6 +284,8 @@ pub enum CopyRangeMethod {
All,
}

impl_from_str_and_display!(CopyRangeMethod);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 6 additions & 2 deletions src/rest/config/gui.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use super::PortNumber;
use crate::utils::impl_from_str_and_display;
use serde::{Deserialize, Serialize};
use std::{
fmt::{Display, Write},
Expand All @@ -7,8 +9,6 @@ use std::{
str::FromStr,
};

use super::PortNumber;

/// The GUI configuration.
/// <https://docs.syncthing.net/users/config.html#gui-element>
#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
Expand Down Expand Up @@ -69,6 +69,8 @@ pub enum Address {
Path(PathBuf),
}

impl_from_str_and_display!(Address);

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
pub struct WildcardPort(PortNumber);

Expand Down Expand Up @@ -138,6 +140,8 @@ pub enum AuthMode {
Ldap,
}

impl_from_str_and_display!(AuthMode);

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 3 additions & 0 deletions src/rest/config/ldap.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::utils::impl_from_str_and_display;
use serde::{Deserialize, Serialize};

/// LDAP configuration options. The mechanism is described in detail under LDAP Authentication.
Expand Down Expand Up @@ -39,3 +40,5 @@ pub enum Transport {
/// StartTLS connection mode.
StartTls,
}

impl_from_str_and_display!(Transport);
9 changes: 9 additions & 0 deletions src/rest/config/options.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
Count, Hours, Kibibytes, KibibytesPerSecond, MinDiskFree, Minutes, PortNumber, Seconds,
};
use crate::utils::impl_from_str_and_display;
use ipnet::IpNet;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -194,6 +195,8 @@ pub enum ListenAddress {
Address(Url),
}

impl_from_str_and_display!(ListenAddress);

/// A URI to a global announce (discovery) server, or the word default to include the default
/// servers. The syntax for non-default entries is that of an HTTP or HTTPS URL. A number of options
/// may be added as query options to the URL: `insecure` to prevent certificate validation (required
Expand All @@ -207,6 +210,8 @@ pub enum AnnounceServer {
Address(Url),
}

impl_from_str_and_display!(AnnounceServer);

#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum StunServer {
Expand All @@ -216,6 +221,8 @@ pub enum StunServer {
Address(String),
}

impl_from_str_and_display!(StunServer);

#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, Serialize, Deserialize)]
#[serde(rename_all(serialize = "lowercase", deserialize = "lowercase"))]
pub enum DatabaseTuning {
Expand All @@ -224,6 +231,8 @@ pub enum DatabaseTuning {
Auto,
}

impl_from_str_and_display!(DatabaseTuning);

mod strings {
use crate::utils::named_unit_variant;
named_unit_variant!(default);
Expand Down
22 changes: 21 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,25 @@ macro_rules! named_unit_variant {
}
};
}

pub(crate) use named_unit_variant;

/// Implements `FromStr` and `Display` in terms of `Serialize` and `Deserialize` implementations.
macro_rules! impl_from_str_and_display {
($ty:ty) => {
impl std::str::FromStr for $ty {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
serde_json::from_value(serde_json::Value::String(s.into()))
}
}

impl std::fmt::Display for $ty {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let v = serde_json::to_value(&self).map_err(|_| std::fmt::Error)?;
v.as_str().ok_or(std::fmt::Error)?.fmt(f)
}
}
};
}

pub(crate) use impl_from_str_and_display;

0 comments on commit b255739

Please sign in to comment.