Skip to content

Commit

Permalink
fix: ini parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
JettChenT committed Sep 8, 2024
1 parent d457347 commit 19bf534
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 38 deletions.
2 changes: 1 addition & 1 deletion burrow/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn dump_interface(conn: &Connection, config: &Config) -> Result<()> {
cif.private_key,
to_lst(&cif.dns),
to_lst(&cif.address),
cif.listen_port,
cif.listen_port.unwrap_or(51820),
cif.mtu
])?;
let interface_id = conn.last_insert_rowid();
Expand Down
32 changes: 16 additions & 16 deletions burrow/src/wireguard/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Peer {
pub struct Interface {
pub private_key: String,
pub address: Vec<String>,
pub listen_port: u32,
pub listen_port: Option<u32>,
pub dns: Vec<String>,
pub mtu: Option<u32>,
}
Expand Down Expand Up @@ -102,7 +102,7 @@ impl Default for Config {
interface: Interface {
private_key: "OEPVdomeLTxTIBvv3TYsJRge0Hp9NMiY0sIrhT8OWG8=".into(),
address: vec!["10.13.13.2/24".into()],
listen_port: 51820,
listen_port: Some(51820),
dns: Default::default(),
mtu: Default::default(),
},
Expand All @@ -118,23 +118,23 @@ impl Default for Config {
}
}

fn props_get<T>(props: &Properties, key: &str) -> T
fn props_get<T>(props: &Properties, key: &str) -> Result<T>
where
T: From<IniField>,
T: TryFrom<IniField, Error = anyhow::Error>,
{
IniField::from(props.get(key)).into()
IniField::try_from(props.get(key))?.try_into()
}

impl TryFrom<&Properties> for Interface {
type Error = anyhow::Error;

fn try_from(props: &Properties) -> Result<Self, Error> {
Ok(Self {
private_key: props_get(props, "PrivateKey"),
address: props_get(props, "Address"),
listen_port: props_get(props, "ListenPort"),
dns: props_get(props, "DNS"),
mtu: props_get(props, "MTU"),
private_key: props_get(props, "PrivateKey")?,
address: props_get(props, "Address")?,
listen_port: props_get(props, "ListenPort")?,
dns: props_get(props, "DNS")?,
mtu: props_get(props, "MTU")?,
})
}
}
Expand All @@ -144,12 +144,12 @@ impl TryFrom<&Properties> for Peer {

fn try_from(props: &Properties) -> Result<Self, Error> {
Ok(Self {
public_key: props_get(props, "PublicKey"),
preshared_key: props_get(props, "PresharedKey"),
allowed_ips: props_get(props, "AllowedIPs"),
endpoint: props_get(props, "Endpoint"),
persistent_keepalive: props_get(props, "PersistentKeepalive"),
name: props_get(props, "Name"),
public_key: props_get(props, "PublicKey")?,
preshared_key: props_get(props, "PresharedKey")?,
allowed_ips: props_get(props, "AllowedIPs")?,
endpoint: props_get(props, "Endpoint")?,
persistent_keepalive: props_get(props, "PersistentKeepalive")?,
name: props_get(props, "Name")?,
})
}
}
Expand Down
64 changes: 43 additions & 21 deletions burrow/src/wireguard/inifield.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,76 @@
use std::str::FromStr;

use anyhow::{Error, Result};

pub struct IniField(String);

impl FromStr for IniField {
type Err = anyhow::Error;
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}

impl From<IniField> for Vec<String> {
fn from(field: IniField) -> Self {
field.0.split(",").map(|s| s.to_string()).collect()
impl TryFrom<IniField> for Vec<String> {
type Error = Error;

fn try_from(field: IniField) -> Result<Self, Self::Error> {
Ok(field.0.split(',').map(|s| s.trim().to_string()).collect())
}
}

impl From<IniField> for u32 {
fn from(value: IniField) -> Self {
value.0.parse().unwrap()
impl TryFrom<IniField> for u32 {
type Error = Error;

fn try_from(value: IniField) -> Result<Self, Self::Error> {
value.0.parse().map_err(Error::from)
}
}

impl From<IniField> for Option<u32> {
fn from(value: IniField) -> Self {
Some(value.0.parse().unwrap())
impl TryFrom<IniField> for Option<u32> {
type Error = Error;

fn try_from(value: IniField) -> Result<Self, Self::Error> {
if value.0.is_empty() {
Ok(None)
} else {
value.0.parse().map(Some).map_err(Error::from)
}
}
}

impl From<IniField> for String {
fn from(value: IniField) -> Self {
value.0
impl TryFrom<IniField> for String {
type Error = Error;

fn try_from(value: IniField) -> Result<Self, Self::Error> {
Ok(value.0)
}
}

impl From<IniField> for Option<String> {
fn from(value: IniField) -> Self {
Some(value.0)
impl TryFrom<IniField> for Option<String> {
type Error = Error;

fn try_from(value: IniField) -> Result<Self, Self::Error> {
if value.0.is_empty() {
Ok(None)
} else {
Ok(Some(value.0))
}
}
}

impl<T> From<Option<T>> for IniField
impl<T> TryFrom<Option<T>> for IniField
where
T: ToString,
{
fn from(value: Option<T>) -> Self {
match value {
type Error = Error;

fn try_from(value: Option<T>) -> Result<Self, Self::Error> {
Ok(match value {
Some(v) => Self(v.to_string()),
None => Self("".to_string()),
}
None => Self(String::new()),
})
}
}

Expand Down

0 comments on commit 19bf534

Please sign in to comment.