Skip to content

Commit 21719e2

Browse files
committed
Move WrapComments into own module, include aliases in all errors
1 parent 3454812 commit 21719e2

File tree

4 files changed

+93
-91
lines changed

4 files changed

+93
-91
lines changed

src/config/config_type.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::config::file_lines::FileLines;
22
use crate::config::macro_names::MacroSelectors;
33
use crate::config::options::{IgnoreList, WidthHeuristics};
4+
use crate::config::WrapComments;
45

56
/// Trait for types that can be used in `Config`.
67
pub(crate) trait ConfigType: Sized {
@@ -65,6 +66,12 @@ impl ConfigType for IgnoreList {
6566
}
6667
}
6768

69+
impl ConfigType for WrapComments {
70+
fn doc_hint() -> String {
71+
"[ Off (false) | All (true) | Doc | Normal ]".to_owned()
72+
}
73+
}
74+
6875
macro_rules! create_config {
6976
// Options passed into the macro.
7077
//

src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub use crate::config::lists::*;
1515
pub use crate::config::macro_names::{MacroSelector, MacroSelectors};
1616
#[allow(unreachable_pub)]
1717
pub use crate::config::options::*;
18+
pub(crate) use crate::config::wrap_comments::WrapComments;
1819

1920
#[macro_use]
2021
pub(crate) mod config_type;
@@ -27,6 +28,7 @@ pub(crate) mod file_lines;
2728
pub(crate) mod lists;
2829
pub(crate) mod macro_names;
2930
pub(crate) mod style_edition;
31+
mod wrap_comments;
3032

3133
// This macro defines configuration options used in rustfmt. Each option
3234
// is defined as follows:

src/config/options.rs

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -216,97 +216,6 @@ pub enum Verbosity {
216216
Quiet,
217217
}
218218

219-
/// Which comments to wrap
220-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize)]
221-
pub enum WrapComments {
222-
/// Don't wrap comments
223-
Off,
224-
/// Wrap all kinds of comments
225-
All,
226-
/// Only wrap doc comments
227-
Doc,
228-
/// Only wrap normal comments
229-
Normal,
230-
}
231-
232-
impl WrapComments {
233-
pub(crate) fn is_normal(self) -> bool {
234-
matches!(self, WrapComments::All | WrapComments::Normal)
235-
}
236-
237-
pub(crate) fn is_doc(self) -> bool {
238-
matches!(self, WrapComments::All | WrapComments::Doc)
239-
}
240-
}
241-
242-
impl fmt::Display for WrapComments {
243-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
244-
match self {
245-
WrapComments::Off => f.write_str("Off"),
246-
WrapComments::All => f.write_str("All"),
247-
WrapComments::Doc => f.write_str("Doc"),
248-
WrapComments::Normal => f.write_str("Normal"),
249-
}
250-
}
251-
}
252-
253-
impl super::ConfigType for WrapComments {
254-
fn doc_hint() -> String {
255-
"[Off|All|Doc|Normal]".to_owned()
256-
}
257-
258-
fn stable_variant(&self) -> bool {
259-
true
260-
}
261-
}
262-
263-
impl std::str::FromStr for WrapComments {
264-
type Err = &'static str;
265-
266-
fn from_str(s: &str) -> Result<Self, Self::Err> {
267-
match s.to_lowercase().as_str() {
268-
"off" | "false" => Ok(WrapComments::Off),
269-
"all" | "true" => Ok(WrapComments::All),
270-
"doc" => Ok(WrapComments::Doc),
271-
"normal" => Ok(WrapComments::Normal),
272-
_ => Err("Bad variant, expected one of: `Off` `All` `Doc` `Normal`"),
273-
}
274-
}
275-
}
276-
277-
impl<'de> serde::de::Deserialize<'de> for WrapComments {
278-
fn deserialize<D>(d: D) -> Result<Self, D::Error>
279-
where
280-
D: serde::Deserializer<'de>,
281-
{
282-
use serde::de::Error;
283-
284-
struct StringOrBoolVisitor;
285-
286-
impl<'de> Visitor<'de> for StringOrBoolVisitor {
287-
type Value = String;
288-
289-
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
290-
formatter.write_str("string")
291-
}
292-
293-
fn visit_str<E>(self, value: &str) -> Result<String, E> {
294-
Ok(String::from(value))
295-
}
296-
297-
fn visit_bool<E>(self, value: bool) -> Result<String, E> {
298-
Ok(value.to_string())
299-
}
300-
}
301-
302-
let s = d.deserialize_string(StringOrBoolVisitor)?;
303-
s.parse().map_err(|_| {
304-
static ALLOWED: &'static [&str] = &["Off", "All", "Doc", "Normal"];
305-
D::Error::unknown_variant(&s, ALLOWED)
306-
})
307-
}
308-
}
309-
310219
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
311220
pub struct WidthHeuristics {
312221
// Maximum width of the args of a function call before falling back

src/config/wrap_comments.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
use std::fmt;
2+
3+
/// Which comments to wrap
4+
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize)]
5+
pub enum WrapComments {
6+
/// Don't wrap comments
7+
Off,
8+
/// Wrap all kinds of comments
9+
All,
10+
/// Only wrap doc comments
11+
Doc,
12+
/// Only wrap normal comments
13+
Normal,
14+
}
15+
16+
impl WrapComments {
17+
pub(crate) fn is_normal(self) -> bool {
18+
matches!(self, WrapComments::All | WrapComments::Normal)
19+
}
20+
21+
pub(crate) fn is_doc(self) -> bool {
22+
matches!(self, WrapComments::All | WrapComments::Doc)
23+
}
24+
}
25+
26+
impl fmt::Display for WrapComments {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
match self {
29+
WrapComments::Off => f.write_str("Off"),
30+
WrapComments::All => f.write_str("All"),
31+
WrapComments::Doc => f.write_str("Doc"),
32+
WrapComments::Normal => f.write_str("Normal"),
33+
}
34+
}
35+
}
36+
37+
impl std::str::FromStr for WrapComments {
38+
type Err = &'static str;
39+
40+
fn from_str(s: &str) -> Result<Self, Self::Err> {
41+
match s.to_lowercase().as_str() {
42+
"off" | "false" => Ok(WrapComments::Off),
43+
"all" | "true" => Ok(WrapComments::All),
44+
"doc" => Ok(WrapComments::Doc),
45+
"normal" => Ok(WrapComments::Normal),
46+
_ => {
47+
Err("Bad variant, expected one of: `Off`, `false`, `All`, `true`, `Doc`, `Normal`")
48+
}
49+
}
50+
}
51+
}
52+
53+
impl<'de> serde::de::Deserialize<'de> for WrapComments {
54+
fn deserialize<D>(d: D) -> Result<Self, D::Error>
55+
where
56+
D: serde::Deserializer<'de>,
57+
{
58+
use serde::de::Error;
59+
60+
struct StringOrBoolVisitor;
61+
62+
impl<'de> serde::de::Visitor<'de> for StringOrBoolVisitor {
63+
type Value = String;
64+
65+
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
formatter.write_str("string")
67+
}
68+
69+
fn visit_str<E>(self, value: &str) -> Result<String, E> {
70+
Ok(String::from(value))
71+
}
72+
73+
fn visit_bool<E>(self, value: bool) -> Result<String, E> {
74+
Ok(value.to_string())
75+
}
76+
}
77+
78+
let s = d.deserialize_string(StringOrBoolVisitor)?;
79+
s.parse().map_err(|_| {
80+
static ALLOWED: &'static [&str] = &["Off", "false", "All", "true", "Doc", "Normal"];
81+
D::Error::unknown_variant(&s, ALLOWED)
82+
})
83+
}
84+
}

0 commit comments

Comments
 (0)