Skip to content

Commit

Permalink
Use GATs for Input type in Parser (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
chitoku-k authored Nov 4, 2022
1 parent 70518c1 commit 065c638
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 106 deletions.
14 changes: 7 additions & 7 deletions src/infrastructure/cmd/parser/bgp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::{
pub struct BGPParser;

impl Parser for BGPParser {
type Input = String;
type Input<'a> = &'a str;
type Item = BGPStatusResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_bgp_status(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_bgp_status(input)
.finish()
.map(|(_, status)| status)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -179,7 +179,7 @@ mod tests {
let parser = BGPParser;
let input = "command not found";

let actual = parser.parse(input.to_string());
let actual = parser.parse(input);
assert!(actual.is_err());
}

Expand All @@ -188,7 +188,7 @@ mod tests {
let parser = BGPParser;
let input = "";

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, None);
}

Expand All @@ -214,7 +214,7 @@ mod tests {
Total number of Established sessions 4
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, Some(BGPStatus {
router_id: "192.0.2.1".to_string(),
local_as: 64496,
Expand Down Expand Up @@ -331,7 +331,7 @@ mod tests {
Total number of Established sessions 4
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, Some(BGPStatus {
router_id: "192.0.2.1".to_string(),
local_as: 64496,
Expand Down
14 changes: 7 additions & 7 deletions src/infrastructure/cmd/parser/ddns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ use crate::{
pub struct DdnsParser;

impl Parser for DdnsParser {
type Input = String;
type Input<'a> = &'a str;
type Item = DdnsStatusResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_ddns_status(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_ddns_status(input)
.finish()
.map(|(_, status)| status)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -147,7 +147,7 @@ mod tests {
let parser = DdnsParser;
let input = "";

assert!(parser.parse(input.to_string()).is_err());
assert!(parser.parse(input).is_err());
}

#[test]
Expand All @@ -158,7 +158,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![]);
}

Expand All @@ -174,7 +174,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
DdnsStatus {
interface: "eth0".to_string(),
Expand Down Expand Up @@ -210,7 +210,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
DdnsStatus {
interface: "eth0".to_string(),
Expand Down
8 changes: 4 additions & 4 deletions src/infrastructure/cmd/parser/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::{
pub struct InterfaceParser;

impl Parser for InterfaceParser {
type Input = String;
type Input<'a> = &'a str;
type Item = InterfaceResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_interfaces(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_interfaces(input)
.finish()
.map(|(_, interfaces)| interfaces)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -141,7 +141,7 @@ mod tests {
pppoe0 UP 203.0.113.1 peer 192.0.2.255/32
"#};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
Interface {
ifname: "lo".to_string(),
Expand Down
32 changes: 16 additions & 16 deletions src/infrastructure/cmd/parser/load_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ pub struct LoadBalanceStatusParser;
pub struct LoadBalanceWatchdogParser;

impl Parser for LoadBalanceStatusParser {
type Input = String;
type Input<'a> = &'a str;
type Item = LoadBalanceStatusResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_load_balance_status(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_load_balance_status(input)
.finish()
.map(|(_, status)| status)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand All @@ -48,11 +48,11 @@ impl Parser for LoadBalanceStatusParser {
}

impl Parser for LoadBalanceWatchdogParser {
type Input = String;
type Input<'a> = &'a str;
type Item = LoadBalanceWatchdogResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_load_balance_watchdog(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_load_balance_watchdog(input)
.finish()
.map(|(_, groups)| groups)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -390,13 +390,13 @@ mod tests {
let parser = LoadBalanceStatusParser;
let input = "";

let actual = parser.parse(input.to_string());
let actual = parser.parse(input);
assert!(actual.is_err());

let parser = LoadBalanceWatchdogParser;
let input = "";

let actual = parser.parse(input.to_string());
let actual = parser.parse(input);
assert!(actual.is_err());
}

Expand All @@ -405,13 +405,13 @@ mod tests {
let parser = LoadBalanceStatusParser;
let input = "load-balance is not configured";

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![]);

let parser = LoadBalanceWatchdogParser;
let input = "load-balance is not configured";

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![]);
}

Expand All @@ -427,7 +427,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceStatus {
name: "FAILOVER_01".to_string(),
Expand All @@ -442,7 +442,7 @@ mod tests {
let parser = LoadBalanceWatchdogParser;
let input = "Group FAILOVER_01";

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceWatchdog {
name: "FAILOVER_01".to_string(),
Expand Down Expand Up @@ -491,7 +491,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceStatus {
name: "FAILOVER_01".to_string(),
Expand Down Expand Up @@ -566,7 +566,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceWatchdog {
name: "FAILOVER_01".to_string(),
Expand Down Expand Up @@ -688,7 +688,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceStatus {
name: "FAILOVER_01".to_string(),
Expand Down Expand Up @@ -847,7 +847,7 @@ mod tests {
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, vec![
LoadBalanceWatchdog {
name: "FAILOVER_01".to_string(),
Expand Down
4 changes: 2 additions & 2 deletions src/infrastructure/cmd/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ pub mod pppoe;
pub mod version;

pub trait Parser {
type Input;
type Input<'a>;
type Item;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item>;
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item>;
}

#[derive(Into)]
Expand Down
26 changes: 13 additions & 13 deletions src/infrastructure/cmd/parser/pppoe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ use crate::{
pub struct PPPoEParser;

impl Parser for PPPoEParser {
type Input = (String, Vec<Interface>);
type Input<'a> = (&'a str, &'a [Interface]);
type Item = PPPoEClientSessionResult;

fn parse(&self, (input, interfaces): Self::Input) -> anyhow::Result<Self::Item> {
parse_pppoe_client_sessions(&input, &interfaces)
fn parse(&self, (input, interfaces): Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_pppoe_client_sessions(input, interfaces)
.finish()
.map(|(_, sessions)| sessions)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -190,18 +190,18 @@ mod tests {
fn empty() {
let parser = PPPoEParser;
let input = "";
let interfaces = vec![];
let interfaces = &[];

assert!(parser.parse((input.to_string(), interfaces)).is_err());
assert!(parser.parse((input, interfaces)).is_err());
}

#[test]
fn no_config() {
let parser = PPPoEParser;
let input = "No active PPPoE client sessions";
let interfaces = vec![];
let interfaces = &[];

let actual = parser.parse((input.to_string(), interfaces)).unwrap();
let actual = parser.parse((input, interfaces)).unwrap();
assert_eq!(actual, vec![]);
}

Expand All @@ -218,9 +218,9 @@ mod tests {
Total sessions: 2
"};
let interfaces = vec![];
let interfaces = &[];

let actual = parser.parse((input.to_string(), interfaces)).unwrap();
let actual = parser.parse((input, interfaces)).unwrap();
assert_eq!(actual, vec![
PPPoEClientSession {
user: "user01".to_string(),
Expand Down Expand Up @@ -262,7 +262,7 @@ mod tests {
Total sessions: 2
"};
let interfaces = vec![
let interfaces = &[
Interface {
ifname: "pppoe0".to_string(),
operstate: "UNKNOWN".to_string(),
Expand All @@ -287,7 +287,7 @@ mod tests {
},
];

let actual = parser.parse((input.to_string(), interfaces)).unwrap();
let actual = parser.parse((input, interfaces)).unwrap();
assert_eq!(actual, vec![
PPPoEClientSession {
user: "user01".to_string(),
Expand Down Expand Up @@ -329,7 +329,7 @@ mod tests {
Total sessions: 2
"};
let interfaces = vec![
let interfaces = &[
Interface {
ifname: "pppoe0".to_string(),
operstate: "UNKNOWN".to_string(),
Expand All @@ -354,7 +354,7 @@ mod tests {
},
];

let actual = parser.parse((input.to_string(), interfaces)).unwrap();
let actual = parser.parse((input, interfaces)).unwrap();
assert_eq!(actual, vec![
PPPoEClientSession {
user: "user01".to_string(),
Expand Down
10 changes: 5 additions & 5 deletions src/infrastructure/cmd/parser/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ use crate::{
pub struct VersionParser;

impl Parser for VersionParser {
type Input = String;
type Input<'a> = &'a str;
type Item = VersionResult;

fn parse(&self, input: Self::Input) -> anyhow::Result<Self::Item> {
parse_version(&input)
fn parse(&self, input: Self::Input<'_>) -> anyhow::Result<Self::Item> {
parse_version(input)
.finish()
.map(|(_, version)| version)
.map_err(|e| Error::new(e.input.to_string(), e.code))
Expand Down Expand Up @@ -107,7 +107,7 @@ mod tests {
let parser = VersionParser;
let input = "";

assert!(parser.parse(input.to_string()).is_err());
assert!(parser.parse(input).is_err());
}

#[test]
Expand All @@ -123,7 +123,7 @@ mod tests {
Uptime: 01:00:00 up 1:00, 1 user, load average: 1.00, 1.00, 1.00
"};

let actual = parser.parse(input.to_string()).unwrap();
let actual = parser.parse(input).unwrap();
assert_eq!(actual, Version {
version: "v2.0.6".to_string(),
build_id: "5208541".to_string(),
Expand Down
Loading

0 comments on commit 065c638

Please sign in to comment.