-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
And some random rustfmt changes to `matchers.rs` because it was not imported before.
- Loading branch information
Showing
7 changed files
with
111 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,94 +1,91 @@ | ||
use crate::prelude::*; | ||
use std::net::IpAddr; | ||
use std::ops::Deref; | ||
|
||
use ipnet::IpNet; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::prelude::*; | ||
|
||
#[derive(Debug,Clone,Serialize,Deserialize)] | ||
pub enum HostMatcher | ||
{ | ||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub enum HostMatcher { | ||
Hostname(Pattern), | ||
Ip(IpNet), | ||
} | ||
|
||
impl HostMatcher | ||
{ | ||
pub fn is_host(&self) -> bool | ||
{ | ||
impl HostMatcher { | ||
pub fn is_host(&self) -> bool { | ||
matches!(self, Self::Hostname(_)) | ||
} | ||
|
||
pub fn is_ip(&self) -> bool | ||
{ | ||
pub fn is_ip(&self) -> bool { | ||
matches!(self, Self::Ip(_)) | ||
} | ||
|
||
pub fn matches_host(&self, hostname: &str) -> bool | ||
{ | ||
match self | ||
{ | ||
pub fn matches_host(&self, hostname: &str) -> bool { | ||
match self { | ||
Self::Hostname(pat) => pat.matches(hostname), | ||
_ => false | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn matches_ip(&self, addr: &IpAddr) -> bool | ||
{ | ||
match self | ||
{ | ||
pub fn matches_ip(&self, addr: &IpAddr) -> bool { | ||
match self { | ||
Self::Ip(mask) => mask.contains(addr), | ||
_ => false | ||
_ => false, | ||
} | ||
} | ||
|
||
pub fn matches(&self, hostname: &str, addr: &IpAddr) -> bool | ||
{ | ||
match self | ||
{ | ||
pub fn matches(&self, hostname: &str, addr: &IpAddr) -> bool { | ||
match self { | ||
Self::Hostname(pat) => pat.matches(hostname), | ||
Self::Ip(mask) => mask.contains(addr), | ||
} | ||
} | ||
} | ||
|
||
pub struct UserHostMatcher | ||
{ | ||
pub struct UserHostMatcher { | ||
user: Pattern, | ||
host: HostMatcher | ||
host: HostMatcher, | ||
} | ||
|
||
impl UserHostMatcher | ||
{ | ||
pub fn matches(&self, username: &str, hostname: &str, addr: &IpAddr) -> bool | ||
{ | ||
impl UserHostMatcher { | ||
pub fn matches(&self, username: &str, hostname: &str, addr: &IpAddr) -> bool { | ||
self.user.matches(username) && self.host.matches(hostname, addr) | ||
} | ||
} | ||
|
||
pub struct IpMatcher(IpNet); | ||
|
||
impl IpMatcher | ||
{ | ||
pub fn matches(&self, addr: &IpAddr) -> bool | ||
{ | ||
impl IpMatcher { | ||
pub fn matches(&self, addr: &IpAddr) -> bool { | ||
self.0.contains(addr) | ||
} | ||
} | ||
|
||
pub struct ExactIpMatcher(IpAddr); | ||
|
||
impl ExactIpMatcher | ||
{ | ||
pub fn matches(&self, addr: &IpAddr) -> bool | ||
{ | ||
impl ExactIpMatcher { | ||
pub fn matches(&self, addr: &IpAddr) -> bool { | ||
&self.0 == addr | ||
} | ||
} | ||
|
||
pub struct NicknameMatcher(Pattern); | ||
|
||
impl NicknameMatcher | ||
{ | ||
pub fn matches(&self, nick: &Nickname) -> bool | ||
{ | ||
impl Deref for NicknameMatcher { | ||
type Target = Pattern; | ||
|
||
fn deref(&self) -> &Pattern { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl NicknameMatcher { | ||
pub fn new(pattern: Pattern) -> NicknameMatcher { | ||
NicknameMatcher(pattern) | ||
} | ||
|
||
pub fn matches(&self, nick: &Nickname) -> bool { | ||
self.0.matches(nick.as_ref()) | ||
} | ||
} | ||
} |