Skip to content

Commit 130150d

Browse files
authored
Improve code formatting (#116)
* Improve formatting * Fix formatting
1 parent 84f91c2 commit 130150d

File tree

11 files changed

+4103
-1795
lines changed

11 files changed

+4103
-1795
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,19 @@ jobs:
3232
override: true
3333
components: rustfmt, clippy
3434

35+
- name: "Format check"
36+
uses: actions-rs/cargo@v1
37+
with:
38+
command: fmt
39+
args: -- --check
40+
3541
- name: "Run cargo test"
3642
uses: actions-rs/cargo@v1
3743
with:
3844
command: test
3945
args: --all-features --all-targets
4046

4147
# Uncomment when supported:
42-
# - name: "Format check"
43-
# uses: actions-rs/cargo@v1
44-
# with:
45-
# command: fmt
46-
# args: -- --check
4748
# - name: "Linter checks"
4849
# uses: actions-rs/cargo@v1
4950
# with:

src/cli.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clap::{Parser};
1+
use clap::Parser;
22
use once_cell::sync::Lazy;
33
// TODO: make non-annotation generate different DeserializeError that is simpler
44
// and works with From<cbor_event:Error> only
@@ -51,4 +51,4 @@ pub struct Cli {
5151
pub package_json: bool,
5252
}
5353

54-
pub static CLI_ARGS: Lazy<Cli> = Lazy::new(|| Cli::parse());
54+
pub static CLI_ARGS: Lazy<Cli> = Lazy::new(|| Cli::parse());

src/comment_ast.rs

Lines changed: 91 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,89 @@
11
extern crate nom;
22
use nom::{
3-
IResult,
4-
bytes::complete::{tag, take_while1, take_while}, branch::alt, multi::many0,
3+
branch::alt,
4+
bytes::complete::{tag, take_while, take_while1},
5+
multi::many0,
6+
IResult,
57
};
68

79
#[derive(Default, Debug, PartialEq)]
810
pub struct RuleMetadata {
9-
pub name: Option<String>,
10-
pub is_newtype: bool,
11+
pub name: Option<String>,
12+
pub is_newtype: bool,
1113
}
1214

1315
fn merge_metadata(r1: &RuleMetadata, r2: &RuleMetadata) -> RuleMetadata {
1416
RuleMetadata {
1517
name: match (r1.name.as_ref(), r2.name.as_ref()) {
16-
(Some(val1), Some(val2)) => panic!("Key \"name\" specified twice: {:?} {:?}", val1, val2),
17-
(val@Some(_), _) => val.cloned(),
18-
(_, val) => val.cloned()
18+
(Some(val1), Some(val2)) => {
19+
panic!("Key \"name\" specified twice: {:?} {:?}", val1, val2)
20+
}
21+
(val @ Some(_), _) => val.cloned(),
22+
(_, val) => val.cloned(),
1923
},
20-
is_newtype: r1.is_newtype || r2.is_newtype
24+
is_newtype: r1.is_newtype || r2.is_newtype,
2125
}
2226
}
2327

2428
enum ParseResult {
25-
NewType,
26-
Name(String)
29+
NewType,
30+
Name(String),
2731
}
2832

2933
impl RuleMetadata {
30-
fn from_parse_results(results: &[ParseResult]) -> RuleMetadata {
31-
let mut base = RuleMetadata::default();
32-
for result in results {
33-
match result {
34-
ParseResult::Name(name) => {
35-
match base.name.as_ref() {
36-
Some(old_name) => panic!("Key \"name\" specified twice: {:?} {:?}", old_name, name),
37-
None => { base.name = Some(name.to_string()); }
38-
}
39-
},
40-
ParseResult::NewType => { base.is_newtype = true; }
41-
}
34+
fn from_parse_results(results: &[ParseResult]) -> RuleMetadata {
35+
let mut base = RuleMetadata::default();
36+
for result in results {
37+
match result {
38+
ParseResult::Name(name) => match base.name.as_ref() {
39+
Some(old_name) => {
40+
panic!("Key \"name\" specified twice: {:?} {:?}", old_name, name)
41+
}
42+
None => {
43+
base.name = Some(name.to_string());
44+
}
45+
},
46+
ParseResult::NewType => {
47+
base.is_newtype = true;
48+
}
49+
}
50+
}
51+
base
4252
}
43-
base
44-
}
4553
}
4654

4755
fn tag_name(input: &str) -> IResult<&str, ParseResult> {
48-
let (input, _) = tag("@name")(input)?;
49-
let (input, _) = take_while(char::is_whitespace)(input)?;
50-
let (input, name) = take_while1(|ch| !char::is_whitespace(ch))(input)?;
56+
let (input, _) = tag("@name")(input)?;
57+
let (input, _) = take_while(char::is_whitespace)(input)?;
58+
let (input, name) = take_while1(|ch| !char::is_whitespace(ch))(input)?;
5159

52-
Ok((input, ParseResult::Name(name.to_string())))
60+
Ok((input, ParseResult::Name(name.to_string())))
5361
}
5462
fn tag_newtype(input: &str) -> IResult<&str, ParseResult> {
55-
let (input, _) = tag("@newtype")(input)?;
63+
let (input, _) = tag("@newtype")(input)?;
5664

57-
Ok((input, ParseResult::NewType))
65+
Ok((input, ParseResult::NewType))
5866
}
5967
fn whitespace_then_tag(input: &str) -> IResult<&str, ParseResult> {
60-
let (input, _) = take_while(char::is_whitespace)(input)?;
61-
let (input, result) = alt((tag_name, tag_newtype))(input)?;
68+
let (input, _) = take_while(char::is_whitespace)(input)?;
69+
let (input, result) = alt((tag_name, tag_newtype))(input)?;
6270

63-
Ok((input, result))
71+
Ok((input, result))
6472
}
6573

6674
fn rule_metadata(input: &str) -> IResult<&str, RuleMetadata> {
67-
let (input, parse_results) = many0(whitespace_then_tag)(input)?;
68-
75+
let (input, parse_results) = many0(whitespace_then_tag)(input)?;
6976

70-
Ok((input, RuleMetadata::from_parse_results(&parse_results)))
77+
Ok((input, RuleMetadata::from_parse_results(&parse_results)))
7178
}
7279

73-
74-
impl <'a> From<Option<&'a cddl::ast::Comments<'a>>> for RuleMetadata {
75-
fn from(comments: Option<&'a cddl::ast::Comments<'a>>) -> RuleMetadata {
76-
match comments {
77-
None => RuleMetadata::default(),
78-
Some(c) => metadata_from_comments(&c.0)
80+
impl<'a> From<Option<&'a cddl::ast::Comments<'a>>> for RuleMetadata {
81+
fn from(comments: Option<&'a cddl::ast::Comments<'a>>) -> RuleMetadata {
82+
match comments {
83+
None => RuleMetadata::default(),
84+
Some(c) => metadata_from_comments(&c.0),
85+
}
7986
}
80-
}
8187
}
8288

8389
pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
@@ -86,38 +92,62 @@ pub fn metadata_from_comments(comments: &[&str]) -> RuleMetadata {
8692
if let Ok(comment_metadata) = rule_metadata(comment) {
8793
result = merge_metadata(&result, &comment_metadata.1);
8894
}
89-
};
95+
}
9096
result
9197
}
9298

9399
#[test]
94100
fn parse_comment_name() {
95-
assert_eq!(rule_metadata("@name foo"), Ok(("", RuleMetadata {
96-
name: Some("foo".to_string()),
97-
is_newtype: false,
98-
})));
101+
assert_eq!(
102+
rule_metadata("@name foo"),
103+
Ok((
104+
"",
105+
RuleMetadata {
106+
name: Some("foo".to_string()),
107+
is_newtype: false,
108+
}
109+
))
110+
);
99111
}
100112

101113
#[test]
102114
fn parse_comment_newtype() {
103-
assert_eq!(rule_metadata("@newtype"), Ok(("", RuleMetadata {
104-
name: None,
105-
is_newtype: true
106-
})));
115+
assert_eq!(
116+
rule_metadata("@newtype"),
117+
Ok((
118+
"",
119+
RuleMetadata {
120+
name: None,
121+
is_newtype: true
122+
}
123+
))
124+
);
107125
}
108126

109127
#[test]
110128
fn parse_comment_newtype_and_name() {
111-
assert_eq!(rule_metadata("@newtype @name foo"), Ok(("", RuleMetadata {
112-
name: Some("foo".to_string()),
113-
is_newtype: true
114-
})));
129+
assert_eq!(
130+
rule_metadata("@newtype @name foo"),
131+
Ok((
132+
"",
133+
RuleMetadata {
134+
name: Some("foo".to_string()),
135+
is_newtype: true
136+
}
137+
))
138+
);
115139
}
116140

117141
#[test]
118142
fn parse_comment_newtype_and_name_inverse() {
119-
assert_eq!(rule_metadata("@name foo @newtype"), Ok(("", RuleMetadata {
120-
name: Some("foo".to_string()),
121-
is_newtype: true
122-
})));
123-
}
143+
assert_eq!(
144+
rule_metadata("@name foo @newtype"),
145+
Ok((
146+
"",
147+
RuleMetadata {
148+
name: Some("foo".to_string()),
149+
is_newtype: true
150+
}
151+
))
152+
);
153+
}

0 commit comments

Comments
 (0)