Skip to content

Commit 0ccae93

Browse files
committed
Refactoring
1 parent 328f692 commit 0ccae93

File tree

1 file changed

+38
-42
lines changed

1 file changed

+38
-42
lines changed

src/prosafe_switch.rs

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -57,31 +57,20 @@ impl QueryRequest {
5757
}
5858

5959
// ---------------------------------------------------------------------------------------------------------------------
60-
// PortStats
60+
// QueryResponse
6161
// ---------------------------------------------------------------------------------------------------------------------
6262

63-
#[derive(Debug)]
64-
pub struct PortStats {
65-
pub stats: Vec<PortStat>,
66-
}
63+
struct QueryResponse;
6764

68-
#[derive(Debug)]
69-
pub struct PortStat {
70-
pub port_no: u8,
71-
pub recv_bytes: u64,
72-
pub send_bytes: u64,
73-
pub error_pkts: u64,
74-
}
75-
76-
impl PortStats {
77-
fn decode(dat: &[u8]) -> Result<Self, Error> {
65+
impl QueryResponse {
66+
fn decode(dat: &[u8]) -> Result<Vec<Vec<u8>>, Error> {
7867
let (_, rest) = bytes(&[0x01, 0x02])
7968
.and(be_u16())
8069
.and(be_u16())
8170
.and(skip_count(26, any()))
8271
.parse(dat)
8372
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
84-
let mut stats = Vec::new();
73+
let mut ret = Vec::new();
8574
let mut buf = rest;
8675
while buf.len() != 0 {
8776
let ((cmd, len), rest) = be_u16()
@@ -99,9 +88,38 @@ impl PortStats {
9988
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
10089
buf = rest;
10190

91+
ret.push(dat);
92+
}
93+
94+
Ok(ret)
95+
}
96+
}
97+
98+
// ---------------------------------------------------------------------------------------------------------------------
99+
// PortStats
100+
// ---------------------------------------------------------------------------------------------------------------------
101+
102+
#[derive(Debug)]
103+
pub struct PortStats {
104+
pub stats: Vec<PortStat>,
105+
}
106+
107+
#[derive(Debug)]
108+
pub struct PortStat {
109+
pub port_no: u8,
110+
pub recv_bytes: u64,
111+
pub send_bytes: u64,
112+
pub error_pkts: u64,
113+
}
114+
115+
impl PortStats {
116+
fn decode(dat: &[u8]) -> Result<Self, Error> {
117+
let dat = QueryResponse::decode(dat)?;
118+
let mut stats = Vec::new();
119+
for d in dat {
102120
let ((port_no, metrics), _rest) = any()
103121
.and(count::<Vec<_>, _>(6, be_u64()))
104-
.parse(&dat as &[u8])
122+
.parse(&d as &[u8])
105123
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
106124

107125
let stat = PortStat {
@@ -144,33 +162,12 @@ pub enum Link {
144162

145163
impl SpeedStats {
146164
fn decode(dat: &[u8]) -> Result<Self, Error> {
147-
let (_, rest) = bytes(&[0x01, 0x02])
148-
.and(be_u16())
149-
.and(be_u16())
150-
.and(skip_count(26, any()))
151-
.parse(dat)
152-
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
165+
let dat = QueryResponse::decode(dat)?;
153166
let mut stats = Vec::new();
154-
let mut buf = rest;
155-
while buf.len() != 0 {
156-
let ((cmd, len), rest) = be_u16()
157-
.and(be_u16())
158-
.parse(buf)
159-
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
160-
buf = rest;
161-
162-
if cmd == 0xffff {
163-
break;
164-
}
165-
166-
let (dat, rest) = count::<Vec<_>, _>(len as usize, any())
167-
.parse(buf)
168-
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
169-
buf = rest;
170-
167+
for d in dat {
171168
let ((port_no, metrics), _rest) = any()
172169
.and(count::<Vec<_>, _>(2, any()))
173-
.parse(&dat as &[u8])
170+
.parse(&d as &[u8])
174171
.map_err(|x| format_err!("failed to parse: {:?}", x))?;
175172

176173
let link = match metrics[0] {
@@ -188,7 +185,6 @@ impl SpeedStats {
188185
port_no: port_no,
189186
link: link,
190187
};
191-
192188
stats.push(stat);
193189
}
194190

0 commit comments

Comments
 (0)