Skip to content

Commit 9d885c6

Browse files
committed
Run cargo clippy and cargo fmt
Signed-off-by: Gris Ge <[email protected]>
1 parent fc3dc0c commit 9d885c6

File tree

12 files changed

+156
-74
lines changed

12 files changed

+156
-74
lines changed

.rustfmt.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
max_width = 80
2+
wrap_comments = true
3+
reorder_imports = true
4+
edition = "2021"

examples/list_generic_family.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: MIT
22

3-
use netlink_packet_core::{NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST};
3+
use netlink_packet_core::{
4+
NetlinkMessage, NetlinkPayload, NLM_F_DUMP, NLM_F_REQUEST,
5+
};
46
use netlink_packet_generic::{
57
ctrl::{nlas::GenlCtrlAttrs, GenlCtrl, GenlCtrlCmd},
68
GenlMessage,
@@ -35,7 +37,8 @@ fn main() {
3537
loop {
3638
let buf = &rxbuf[offset..];
3739
// Parse the message
38-
let msg = <NetlinkMessage<GenlMessage<GenlCtrl>>>::deserialize(buf).unwrap();
40+
let msg = <NetlinkMessage<GenlMessage<GenlCtrl>>>::deserialize(buf)
41+
.unwrap();
3942

4043
match msg.payload {
4144
NetlinkPayload::Done => break 'outer,
@@ -45,7 +48,7 @@ fn main() {
4548
}
4649
}
4750
NetlinkPayload::Error(err) => {
48-
eprintln!("Received a netlink error message: {:?}", err);
51+
eprintln!("Received a netlink error message: {err:?}");
4952
return;
5053
}
5154
_ => {}
@@ -103,11 +106,8 @@ fn print_entry(entry: Vec<GenlCtrlAttrs>) {
103106
.expect("Cannot find HdrSize attribute");
104107

105108
if hdrsize == 0 {
106-
println!("0x{:04x} {} [Version {}]", family_id, family_name, version);
109+
println!("0x{family_id:04x} {family_name} [Version {version}]");
107110
} else {
108-
println!(
109-
"0x{:04x} {} [Version {}] [Header {} bytes]",
110-
family_id, family_name, version, hdrsize
111-
);
111+
println!("0x{family_id:04x} {family_name} [Version {version}] [Header {hdrsize} bytes]");
112112
}
113113
}

src/buffer.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ impl<F> ParseableParametrized<[u8], u16> for GenlMessage<F>
1515
where
1616
F: ParseableParametrized<[u8], GenlHeader> + Debug,
1717
{
18-
fn parse_with_param(buf: &[u8], message_type: u16) -> Result<Self, DecodeError> {
18+
fn parse_with_param(
19+
buf: &[u8],
20+
message_type: u16,
21+
) -> Result<Self, DecodeError> {
1922
let buf = GenlBuffer::new_checked(buf)?;
2023
Self::parse_with_param(&buf, message_type)
2124
}
@@ -26,7 +29,10 @@ where
2629
F: ParseableParametrized<[u8], GenlHeader> + Debug,
2730
T: AsRef<[u8]> + ?Sized,
2831
{
29-
fn parse_with_param(buf: &GenlBuffer<&'a T>, message_type: u16) -> Result<Self, DecodeError> {
32+
fn parse_with_param(
33+
buf: &GenlBuffer<&'a T>,
34+
message_type: u16,
35+
) -> Result<Self, DecodeError> {
3036
let header = GenlHeader::parse(buf)?;
3137
let payload_buf = buf.payload();
3238
Ok(GenlMessage::new(

src/constants.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ pub const CTRL_ATTR_OP_UNSPEC: u16 = libc::CTRL_ATTR_OP_UNSPEC as u16;
3232
pub const CTRL_ATTR_OP_ID: u16 = libc::CTRL_ATTR_OP_ID as u16;
3333
pub const CTRL_ATTR_OP_FLAGS: u16 = libc::CTRL_ATTR_OP_FLAGS as u16;
3434

35-
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: u16 = libc::CTRL_ATTR_MCAST_GRP_UNSPEC as u16;
35+
pub const CTRL_ATTR_MCAST_GRP_UNSPEC: u16 =
36+
libc::CTRL_ATTR_MCAST_GRP_UNSPEC as u16;
3637
pub const CTRL_ATTR_MCAST_GRP_NAME: u16 = libc::CTRL_ATTR_MCAST_GRP_NAME as u16;
3738
pub const CTRL_ATTR_MCAST_GRP_ID: u16 = libc::CTRL_ATTR_MCAST_GRP_ID as u16;
3839

src/ctrl/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ impl TryFrom<u8> for GenlCtrlCmd {
7575
CTRL_CMD_GETPOLICY => GetPolicy,
7676
cmd => {
7777
return Err(DecodeError::from(format!(
78-
"Unknown control command: {}",
79-
cmd
78+
"Unknown control command: {cmd}"
8079
)))
8180
}
8281
})
@@ -121,7 +120,10 @@ impl Emitable for GenlCtrl {
121120
}
122121

123122
impl ParseableParametrized<[u8], GenlHeader> for GenlCtrl {
124-
fn parse_with_param(buf: &[u8], header: GenlHeader) -> Result<Self, DecodeError> {
123+
fn parse_with_param(
124+
buf: &[u8],
125+
header: GenlHeader,
126+
) -> Result<Self, DecodeError> {
125127
Ok(Self {
126128
cmd: header.cmd.try_into()?,
127129
nlas: parse_ctrlnlas(buf)?,

src/ctrl/nlas/mcast.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,25 @@ impl Nla for McastGrpAttrs {
4646
}
4747
}
4848

49-
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for McastGrpAttrs {
49+
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
50+
for McastGrpAttrs
51+
{
5052
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
5153
let payload = buf.value();
5254
Ok(match buf.kind() {
53-
CTRL_ATTR_MCAST_GRP_NAME => {
54-
Self::Name(parse_string(payload).context("invalid CTRL_ATTR_MCAST_GRP_NAME value")?)
55+
CTRL_ATTR_MCAST_GRP_NAME => Self::Name(
56+
parse_string(payload)
57+
.context("invalid CTRL_ATTR_MCAST_GRP_NAME value")?,
58+
),
59+
CTRL_ATTR_MCAST_GRP_ID => Self::Id(
60+
parse_u32(payload)
61+
.context("invalid CTRL_ATTR_MCAST_GRP_ID value")?,
62+
),
63+
kind => {
64+
return Err(DecodeError::from(format!(
65+
"Unknown NLA type: {kind}"
66+
)))
5567
}
56-
CTRL_ATTR_MCAST_GRP_ID => {
57-
Self::Id(parse_u32(payload).context("invalid CTRL_ATTR_MCAST_GRP_ID value")?)
58-
}
59-
kind => return Err(DecodeError::from(format!("Unknown NLA type: {}", kind))),
6068
})
6169
}
6270
}

src/ctrl/nlas/mod.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ impl Nla for GenlCtrlAttrs {
4545
HdrSize(v) => size_of_val(v),
4646
MaxAttr(v) => size_of_val(v),
4747
Ops(nlas) => nlas.iter().map(|op| op.as_slice().buffer_len()).sum(),
48-
McastGroups(nlas) => nlas.iter().map(|op| op.as_slice().buffer_len()).sum(),
48+
McastGroups(nlas) => {
49+
nlas.iter().map(|op| op.as_slice().buffer_len()).sum()
50+
}
4951
Policy(nla) => nla.buffer_len(),
5052
OpPolicy(nla) => nla.buffer_len(),
5153
Op(v) => size_of_val(v),
@@ -100,31 +102,40 @@ impl Nla for GenlCtrlAttrs {
100102
}
101103
}
102104

103-
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for GenlCtrlAttrs {
105+
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
106+
for GenlCtrlAttrs
107+
{
104108
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
105109
let payload = buf.value();
106110
Ok(match buf.kind() {
107-
CTRL_ATTR_FAMILY_ID => {
108-
Self::FamilyId(parse_u16(payload).context("invalid CTRL_ATTR_FAMILY_ID value")?)
109-
}
111+
CTRL_ATTR_FAMILY_ID => Self::FamilyId(
112+
parse_u16(payload)
113+
.context("invalid CTRL_ATTR_FAMILY_ID value")?,
114+
),
110115
CTRL_ATTR_FAMILY_NAME => Self::FamilyName(
111-
parse_string(payload).context("invalid CTRL_ATTR_FAMILY_NAME value")?,
116+
parse_string(payload)
117+
.context("invalid CTRL_ATTR_FAMILY_NAME value")?,
118+
),
119+
CTRL_ATTR_VERSION => Self::Version(
120+
parse_u32(payload)
121+
.context("invalid CTRL_ATTR_VERSION value")?,
122+
),
123+
CTRL_ATTR_HDRSIZE => Self::HdrSize(
124+
parse_u32(payload)
125+
.context("invalid CTRL_ATTR_HDRSIZE value")?,
126+
),
127+
CTRL_ATTR_MAXATTR => Self::MaxAttr(
128+
parse_u32(payload)
129+
.context("invalid CTRL_ATTR_MAXATTR value")?,
112130
),
113-
CTRL_ATTR_VERSION => {
114-
Self::Version(parse_u32(payload).context("invalid CTRL_ATTR_VERSION value")?)
115-
}
116-
CTRL_ATTR_HDRSIZE => {
117-
Self::HdrSize(parse_u32(payload).context("invalid CTRL_ATTR_HDRSIZE value")?)
118-
}
119-
CTRL_ATTR_MAXATTR => {
120-
Self::MaxAttr(parse_u32(payload).context("invalid CTRL_ATTR_MAXATTR value")?)
121-
}
122131
CTRL_ATTR_OPS => {
123132
let ops = NlasIterator::new(payload)
124133
.map(|nlas| {
125134
nlas.and_then(|nlas| {
126135
NlasIterator::new(nlas.value())
127-
.map(|nla| nla.and_then(|nla| OpAttrs::parse(&nla)))
136+
.map(|nla| {
137+
nla.and_then(|nla| OpAttrs::parse(&nla))
138+
})
128139
.collect::<Result<Vec<_>, _>>()
129140
})
130141
})
@@ -138,7 +149,11 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for GenlCtrlAttrs
138149
.map(|nlas| {
139150
nlas.and_then(|nlas| {
140151
NlasIterator::new(nlas.value())
141-
.map(|nla| nla.and_then(|nla| McastGrpAttrs::parse(&nla)))
152+
.map(|nla| {
153+
nla.and_then(|nla| {
154+
McastGrpAttrs::parse(&nla)
155+
})
156+
})
142157
.collect::<Result<Vec<_>, _>>()
143158
})
144159
})
@@ -156,7 +171,11 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for GenlCtrlAttrs
156171
.context("failed to parse CTRL_ATTR_OP_POLICY")?,
157172
),
158173
CTRL_ATTR_OP => Self::Op(parse_u32(payload)?),
159-
kind => return Err(DecodeError::from(format!("Unknown NLA type: {}", kind))),
174+
kind => {
175+
return Err(DecodeError::from(format!(
176+
"Unknown NLA type: {kind}"
177+
)))
178+
}
160179
})
161180
}
162181
}

src/ctrl/nlas/oppolicy.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,25 @@ impl Nla for OppolicyIndexAttr {
8282
}
8383
}
8484

85-
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for OppolicyIndexAttr {
85+
impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>>
86+
for OppolicyIndexAttr
87+
{
8688
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
8789
let payload = buf.value();
8890
Ok(match buf.kind() {
89-
CTRL_ATTR_POLICY_DO => {
90-
Self::Do(parse_u32(payload).context("invalid CTRL_ATTR_POLICY_DO value")?)
91+
CTRL_ATTR_POLICY_DO => Self::Do(
92+
parse_u32(payload)
93+
.context("invalid CTRL_ATTR_POLICY_DO value")?,
94+
),
95+
CTRL_ATTR_POLICY_DUMP => Self::Dump(
96+
parse_u32(payload)
97+
.context("invalid CTRL_ATTR_POLICY_DUMP value")?,
98+
),
99+
kind => {
100+
return Err(DecodeError::from(format!(
101+
"Unknown NLA type: {kind}"
102+
)))
91103
}
92-
CTRL_ATTR_POLICY_DUMP => {
93-
Self::Dump(parse_u32(payload).context("invalid CTRL_ATTR_POLICY_DUMP value")?)
94-
}
95-
kind => return Err(DecodeError::from(format!("Unknown NLA type: {}", kind))),
96104
})
97105
}
98106
}

src/ctrl/nlas/ops.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for OpAttrs {
4747
fn parse(buf: &NlaBuffer<&'a T>) -> Result<Self, DecodeError> {
4848
let payload = buf.value();
4949
Ok(match buf.kind() {
50-
CTRL_ATTR_OP_ID => {
51-
Self::Id(parse_u32(payload).context("invalid CTRL_ATTR_OP_ID value")?)
50+
CTRL_ATTR_OP_ID => Self::Id(
51+
parse_u32(payload).context("invalid CTRL_ATTR_OP_ID value")?,
52+
),
53+
CTRL_ATTR_OP_FLAGS => Self::Flags(
54+
parse_u32(payload)
55+
.context("invalid CTRL_ATTR_OP_FLAGS value")?,
56+
),
57+
kind => {
58+
return Err(DecodeError::from(format!(
59+
"Unknown NLA type: {kind}"
60+
)))
5261
}
53-
CTRL_ATTR_OP_FLAGS => {
54-
Self::Flags(parse_u32(payload).context("invalid CTRL_ATTR_OP_FLAGS value")?)
55-
}
56-
kind => return Err(DecodeError::from(format!("Unknown NLA type: {}", kind))),
5762
})
5863
}
5964
}

0 commit comments

Comments
 (0)