diff --git a/src/infrastructure/cmd/parser/bgp.rs b/src/infrastructure/cmd/parser/bgp.rs index e6f13a2..d4943da 100644 --- a/src/infrastructure/cmd/parser/bgp.rs +++ b/src/infrastructure/cmd/parser/bgp.rs @@ -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 { - parse_bgp_status(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_bgp_status(input) .finish() .map(|(_, status)| status) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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()); } @@ -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); } @@ -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, @@ -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, diff --git a/src/infrastructure/cmd/parser/ddns.rs b/src/infrastructure/cmd/parser/ddns.rs index 175a0ee..3c13de8 100644 --- a/src/infrastructure/cmd/parser/ddns.rs +++ b/src/infrastructure/cmd/parser/ddns.rs @@ -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 { - parse_ddns_status(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_ddns_status(input) .finish() .map(|(_, status)| status) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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] @@ -158,7 +158,7 @@ mod tests { "}; - let actual = parser.parse(input.to_string()).unwrap(); + let actual = parser.parse(input).unwrap(); assert_eq!(actual, vec![]); } @@ -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(), @@ -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(), diff --git a/src/infrastructure/cmd/parser/interface.rs b/src/infrastructure/cmd/parser/interface.rs index a6bfac7..0876297 100644 --- a/src/infrastructure/cmd/parser/interface.rs +++ b/src/infrastructure/cmd/parser/interface.rs @@ -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 { - parse_interfaces(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_interfaces(input) .finish() .map(|(_, interfaces)| interfaces) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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(), diff --git a/src/infrastructure/cmd/parser/load_balance.rs b/src/infrastructure/cmd/parser/load_balance.rs index 998e7f2..069c0a2 100644 --- a/src/infrastructure/cmd/parser/load_balance.rs +++ b/src/infrastructure/cmd/parser/load_balance.rs @@ -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 { - parse_load_balance_status(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_load_balance_status(input) .finish() .map(|(_, status)| status) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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 { - parse_load_balance_watchdog(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_load_balance_watchdog(input) .finish() .map(|(_, groups)| groups) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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()); } @@ -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![]); } @@ -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(), @@ -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(), @@ -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(), @@ -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(), @@ -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(), @@ -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(), diff --git a/src/infrastructure/cmd/parser/mod.rs b/src/infrastructure/cmd/parser/mod.rs index 566196b..9f4f911 100644 --- a/src/infrastructure/cmd/parser/mod.rs +++ b/src/infrastructure/cmd/parser/mod.rs @@ -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; + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result; } #[derive(Into)] diff --git a/src/infrastructure/cmd/parser/pppoe.rs b/src/infrastructure/cmd/parser/pppoe.rs index 2ff9387..e638a48 100644 --- a/src/infrastructure/cmd/parser/pppoe.rs +++ b/src/infrastructure/cmd/parser/pppoe.rs @@ -25,11 +25,11 @@ use crate::{ pub struct PPPoEParser; impl Parser for PPPoEParser { - type Input = (String, Vec); + type Input<'a> = (&'a str, &'a [Interface]); type Item = PPPoEClientSessionResult; - fn parse(&self, (input, interfaces): Self::Input) -> anyhow::Result { - parse_pppoe_client_sessions(&input, &interfaces) + fn parse(&self, (input, interfaces): Self::Input<'_>) -> anyhow::Result { + parse_pppoe_client_sessions(input, interfaces) .finish() .map(|(_, sessions)| sessions) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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![]); } @@ -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(), @@ -262,7 +262,7 @@ mod tests { Total sessions: 2 "}; - let interfaces = vec![ + let interfaces = &[ Interface { ifname: "pppoe0".to_string(), operstate: "UNKNOWN".to_string(), @@ -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(), @@ -329,7 +329,7 @@ mod tests { Total sessions: 2 "}; - let interfaces = vec![ + let interfaces = &[ Interface { ifname: "pppoe0".to_string(), operstate: "UNKNOWN".to_string(), @@ -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(), diff --git a/src/infrastructure/cmd/parser/version.rs b/src/infrastructure/cmd/parser/version.rs index 752fb52..f54d50a 100644 --- a/src/infrastructure/cmd/parser/version.rs +++ b/src/infrastructure/cmd/parser/version.rs @@ -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 { - parse_version(&input) + fn parse(&self, input: Self::Input<'_>) -> anyhow::Result { + parse_version(input) .finish() .map(|(_, version)| version) .map_err(|e| Error::new(e.input.to_string(), e.code)) @@ -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] @@ -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(), diff --git a/src/infrastructure/cmd/runner/bgp.rs b/src/infrastructure/cmd/runner/bgp.rs index e82891c..7621782 100644 --- a/src/infrastructure/cmd/runner/bgp.rs +++ b/src/infrastructure/cmd/runner/bgp.rs @@ -19,7 +19,7 @@ pub struct BGPRunner { impl BGPRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = BGPStatusResult> + Send + Sync, { pub fn new(command: VtyshCommand, executor: E, parser: P) -> Self { Self { @@ -31,7 +31,7 @@ where async fn ipv4(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["-c", "show ip bgp summary"]).await?; - let result = self.parser.parse(output)?.map(|mut status| { + let result = self.parser.parse(&output)?.map(|mut status| { status.neighbors.retain(|n| n.neighbor.is_ipv4()); status }); @@ -40,7 +40,7 @@ where async fn ipv6(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["-c", "show bgp ipv6 summary"]).await?; - let result = self.parser.parse(output)?.map(|mut status| { + let result = self.parser.parse(&output)?.map(|mut status| { status.neighbors.retain(|n| n.neighbor.is_ipv6()); status }); @@ -52,7 +52,7 @@ where impl Runner for BGPRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = BGPStatusResult> + Send + Sync, { type Item = (BGPStatusResult, BGPStatusResult); @@ -80,10 +80,10 @@ mod tests { BGPParser {} impl Parser for BGPParser { - type Input = String; + type Input<'a> = &'a str; type Item = BGPStatusResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -107,7 +107,7 @@ mod tests { mock_parser .expect_parse() .times(2) - .with(eq("".to_string())) + .with(eq("")) .returning(|_| Ok(None)); let runner = BGPRunner::new(command, mock_executor, mock_parser); @@ -150,7 +150,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq(ipv4_output.to_string())) + .with(eq(ipv4_output)) .returning(|_| Ok(Some(BGPStatus { router_id: "192.0.2.1".to_string(), local_as: 64496, @@ -205,7 +205,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq("".to_string())) + .with(eq("")) .returning(|_| Ok(None)); let runner = BGPRunner::new(command, mock_executor, mock_parser); @@ -301,12 +301,12 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq("".to_string())) + .with(eq("")) .returning(|_| Ok(None)); mock_parser .expect_parse() .times(1) - .with(eq(ipv6_output.to_string())) + .with(eq(ipv6_output)) .returning(|_| Ok(Some(BGPStatus { router_id: "192.0.2.1".to_string(), local_as: 64496, @@ -470,7 +470,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq(ipv4_output.to_string())) + .with(eq(ipv4_output)) .returning(|_| Ok(Some(BGPStatus { router_id: "192.0.2.1".to_string(), local_as: 64496, @@ -564,7 +564,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq(ipv6_output.to_string())) + .with(eq(ipv6_output)) .returning(|_| Ok(Some(BGPStatus { router_id: "192.0.2.1".to_string(), local_as: 64496, diff --git a/src/infrastructure/cmd/runner/ddns.rs b/src/infrastructure/cmd/runner/ddns.rs index f57d115..b013763 100644 --- a/src/infrastructure/cmd/runner/ddns.rs +++ b/src/infrastructure/cmd/runner/ddns.rs @@ -18,7 +18,7 @@ pub struct DdnsRunner { impl DdnsRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = DdnsStatusResult> + Send + Sync, { pub fn new(command: OpDdnsCommand, executor: E, parser: P) -> Self { Self { @@ -30,7 +30,7 @@ where async fn statuses(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["--show-status"]).await?; - let result = self.parser.parse(output)?; + let result = self.parser.parse(&output)?; Ok(result) } } @@ -39,7 +39,7 @@ where impl Runner for DdnsRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = DdnsStatusResult> + Send + Sync, { type Item = DdnsStatusResult; @@ -68,10 +68,10 @@ mod tests { DdnsParser {} impl Parser for DdnsParser { - type Input = String; + type Input<'a> = &'a str; type Item = DdnsStatusResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -103,7 +103,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq(output.to_string())) + .with(eq(output)) .returning(|_| Ok(vec![ DdnsStatus { interface: "eth0".to_string(), diff --git a/src/infrastructure/cmd/runner/load_balance.rs b/src/infrastructure/cmd/runner/load_balance.rs index 1405838..b4346d3 100644 --- a/src/infrastructure/cmd/runner/load_balance.rs +++ b/src/infrastructure/cmd/runner/load_balance.rs @@ -22,8 +22,8 @@ pub struct LoadBalanceRunner { impl LoadBalanceRunner where E: Executor + Send + Sync, - StatusParser: Parser + Send + Sync, - WatchdogParser: Parser + Send + Sync, + for<'a> StatusParser: Parser = &'a str, Item = LoadBalanceStatusResult> + Send + Sync, + for<'a> WatchdogParser: Parser = &'a str, Item = LoadBalanceWatchdogResult> + Send + Sync, { pub fn new(command: OpCommand, executor: E, status_parser: StatusParser, watchdog_parser: WatchdogParser) -> Self { Self { @@ -36,13 +36,13 @@ where async fn status(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["show", "load-balance", "status"]).await?; - let result = self.status_parser.parse(output)?; + let result = self.status_parser.parse(&output)?; Ok(result) } async fn watchdog(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["show", "load-balance", "watchdog"]).await?; - let result = self.watchdog_parser.parse(output)?; + let result = self.watchdog_parser.parse(&output)?; Ok(result) } } @@ -51,8 +51,8 @@ where impl Runner for LoadBalanceRunner where E: Executor + Send + Sync, - StatusParser: Parser + Send + Sync, - WatchdogParser: Parser + Send + Sync, + for<'a> StatusParser: Parser = &'a str, Item = LoadBalanceStatusResult> + Send + Sync, + for<'a> WatchdogParser: Parser = &'a str, Item = LoadBalanceWatchdogResult> + Send + Sync, { type Item = LoadBalanceStatusResult; @@ -106,10 +106,10 @@ mod tests { LoadBalanceStatusParser {} impl Parser for LoadBalanceStatusParser { - type Input = String; + type Input<'a> = &'a str; type Item = LoadBalanceStatusResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -117,10 +117,10 @@ mod tests { LoadBalanceWatchdogParser {} impl Parser for LoadBalanceWatchdogParser { - type Input = String; + type Input<'a> = &'a str; type Item = LoadBalanceWatchdogResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -204,7 +204,7 @@ mod tests { mock_status_parser .expect_parse() .times(1) - .with(eq(status_output.to_string())) + .with(eq(status_output)) .returning(|_| Ok(vec![ LoadBalanceStatus { name: "FAILOVER_01".to_string(), @@ -259,7 +259,7 @@ mod tests { mock_watchdog_parser .expect_parse() .times(1) - .with(eq(watchdog_output.to_string())) + .with(eq(watchdog_output)) .returning(|_| Ok(vec![ LoadBalanceWatchdog { name: "FAILOVER_01".to_string(), diff --git a/src/infrastructure/cmd/runner/pppoe.rs b/src/infrastructure/cmd/runner/pppoe.rs index dd38eff..2c9683f 100644 --- a/src/infrastructure/cmd/runner/pppoe.rs +++ b/src/infrastructure/cmd/runner/pppoe.rs @@ -25,8 +25,8 @@ pub struct PPPoERunner { impl PPPoERunner where E: Executor + Send + Sync, - PPPoEParser: Parser), Item = PPPoEClientSessionResult> + Send + Sync, - InterfaceParser: Parser + Send + Sync, + for<'a> PPPoEParser: Parser = (&'a str, &'a [Interface]), Item = PPPoEClientSessionResult> + Send + Sync, + for<'a> InterfaceParser: Parser = &'a str, Item = InterfaceResult> + Send + Sync, { pub fn new( op_command: OpCommand, @@ -44,15 +44,15 @@ where } } - async fn sessions(&self, interfaces: Vec) -> anyhow::Result { + async fn sessions(&self, interfaces: &[Interface]) -> anyhow::Result { let output = self.executor.output(&self.op_command, &["show", "pppoe-client"]).await?; - let result = self.pppoe_parser.parse((output, interfaces))?; + let result = self.pppoe_parser.parse((&output, interfaces))?; Ok(result) } async fn interfaces(&self) -> anyhow::Result { let output = self.executor.output(&self.ip_command, &["--brief", "addr", "show"]).await?; - let result = self.interface_parser.parse(output)?; + let result = self.interface_parser.parse(&output)?; Ok(result) } } @@ -61,14 +61,14 @@ where impl Runner for PPPoERunner where E: Executor + Send + Sync, - PPPoEParser: Parser), Item = PPPoEClientSessionResult> + Send + Sync, - InterfaceParser: Parser + Send + Sync, + for<'a> PPPoEParser: Parser = (&'a str, &'a [Interface]), Item = PPPoEClientSessionResult> + Send + Sync, + for<'a> InterfaceParser: Parser = &'a str, Item = InterfaceResult> + Send + Sync, { type Item = PPPoEClientSessionResult; async fn run(&self) -> anyhow::Result { let interfaces = self.interfaces().await?; - self.sessions(interfaces).await + self.sessions(&interfaces).await } } @@ -98,10 +98,10 @@ mod tests { PPPoEParser {} impl Parser for PPPoEParser { - type Input = (String, Vec); + type Input<'a> = (&'a str, &'a [Interface]); type Item = PPPoEClientSessionResult; - fn parse(&self, input: (String, Vec)) -> anyhow::Result<::Item>; + fn parse<'a>(&self, input: (&'a str, &'a [Interface])) -> anyhow::Result<::Item>; } } @@ -109,10 +109,10 @@ mod tests { InterfaceParser {} impl Parser for InterfaceParser { - type Input = String; + type Input<'a> = &'a str; type Item = InterfaceResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -136,7 +136,7 @@ mod tests { pppoe0 UP 203.0.113.1 peer 192.0.2.255/32 "#}; - let interfaces = vec![ + let interfaces = [ Interface { ifname: "lo".to_string(), operstate: "UNKNOWN".to_string(), @@ -187,7 +187,7 @@ mod tests { mock_pppoe_parser .expect_parse() .times(1) - .with(eq((pppoe_output.to_string(), interfaces))) + .withf(move |output| output == &(pppoe_output, &interfaces[..])) .returning(|_| Ok(vec![ PPPoEClientSession { user: "user01".to_string(), @@ -219,7 +219,7 @@ mod tests { mock_interface_parser .expect_parse() .times(1) - .with(eq(interface_output.to_string())) + .with(eq(interface_output)) .returning(|_| Ok(vec![ Interface { ifname: "lo".to_string(), diff --git a/src/infrastructure/cmd/runner/version.rs b/src/infrastructure/cmd/runner/version.rs index 63982e6..6df5fd0 100644 --- a/src/infrastructure/cmd/runner/version.rs +++ b/src/infrastructure/cmd/runner/version.rs @@ -18,7 +18,7 @@ pub struct VersionRunner { impl VersionRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = VersionResult> + Send + Sync, { pub fn new(command: OpCommand, executor: E, parser: P) -> Self { Self { @@ -30,7 +30,7 @@ where async fn version(&self) -> anyhow::Result { let output = self.executor.output(&self.command, &["show", "version"]).await?; - let result = self.parser.parse(output)?; + let result = self.parser.parse(&output)?; Ok(result) } } @@ -39,7 +39,7 @@ where impl Runner for VersionRunner where E: Executor + Send + Sync, - P: Parser + Send + Sync, + for<'a> P: Parser = &'a str, Item = VersionResult> + Send + Sync, { type Item = VersionResult; @@ -63,10 +63,10 @@ mod tests { VersionParser {} impl Parser for VersionParser { - type Input = String; + type Input<'a> = &'a str; type Item = VersionResult; - fn parse(&self, input: String) -> anyhow::Result<::Item>; + fn parse(&self, input: &str) -> anyhow::Result<::Item>; } } @@ -94,7 +94,7 @@ mod tests { mock_parser .expect_parse() .times(1) - .with(eq(output.to_string())) + .with(eq(output)) .returning(|_| Ok(Version { version: "v2.0.6".to_string(), build_id: "5208541".to_string(),