feat: add Proton VPN config parsing support#129
Conversation
Signed-off-by: Enes Öztürk <enes.ozturk@unikie.com>
8a024a1 to
5b8b0e2
Compare
slakkala
left a comment
There was a problem hiding this comment.
Approving, left some suggestions that might help make the code more readable.
| // Handle comment lines (# key = value or # standalone comment) | ||
| if let Some((key, value)) = comment.split_once('=') { | ||
| let key = key.trim(); | ||
| let value = value.trim().to_string(); |
There was a problem hiding this comment.
This is done for unknown keys now too; doesn't probably matter much though.
| } | ||
| i => return Err(format!("Unexpected interface name {i}.")), | ||
| }, | ||
| LineType::Comment(comment) => { |
There was a problem hiding this comment.
I think the below would simplify a bit with guards, LineType::Comment(comment) if is_in_interface => { ... } and same for is_in_peer. The nesting is getting somewhat deep as it is now.
| enum LineType { | ||
| Section(String), | ||
| Attribute(String, String), | ||
| Comment(String), |
There was a problem hiding this comment.
Since not implementing a generic ini parser, maybe also have ExtAttribute(String, String) for commented key-values.
|
|
||
| // We can be either in interface section or in peer section | ||
| let mut is_in_interface = false; | ||
| let mut is_in_peer = false; |
There was a problem hiding this comment.
Maybe rather use an enum for this to make it clear that these are mutually exclusive, something like:
#[derive(Copy, Clone, PartialEq)]
enum Section {
None,
Interface,
Peer,
}
let mut section = Section::None;| @@ -99,6 +99,7 @@ pub fn parse_config(s: &str) -> Result<WireguardConfig, String> { | |||
| enum LineType { | |||
There was a problem hiding this comment.
There's no real need to allocate all of this, could make it:
enum LineType<'a> {
Section(&'a str),
Attribute(&'a str, &'a str),
...
}As a simplified middle-step could keep just the attribute keys as references, as their owned values are never used.
If making values references too, it would probably help to extend the MutOptionExt to have something like:
fn set(&mut self, new: impl Into<T>) {
*self = Some(new.into());
}|
I have taken note of the comments, and I will address all of them in the next PR. |
Add support for parsing Proton VPN generated WireGuard config files:
# Key for <user>as interface name[Peer]as peer name (e.g.,NL-FREE#241,AT#133)