|
1 |
| -//! Simple script to verify the coding style of this library |
| 1 | +//! Provides the [StyleChecker] visitor to verify the coding style of |
| 2 | +//! this library. |
2 | 3 | //!
|
3 |
| -//! ## How to run |
4 |
| -//! |
5 |
| -//! The first argument to this script is the directory to run on, so running |
6 |
| -//! this script should be as simple as: |
7 |
| -//! |
8 |
| -//! ```notrust |
9 |
| -//! cargo test --test style |
10 |
| -//! ``` |
11 |
| -//! |
12 |
| -//! ## Guidelines |
13 |
| -//! |
14 |
| -//! The current style is: |
15 |
| -//! |
16 |
| -//! * Specific module layout: |
17 |
| -//! 1. use directives |
18 |
| -//! 2. typedefs |
19 |
| -//! 3. structs |
20 |
| -//! 4. constants |
21 |
| -//! 5. f! { ... } functions |
22 |
| -//! 6. extern functions |
23 |
| -//! 7. modules + pub use |
| 4 | +//! This is split out so that the implementation itself can be tested |
| 5 | +//! separately, see test/check_style.rs for how it's used. |
24 | 6 |
|
25 | 7 | use std::fmt::Display;
|
| 8 | +use std::fs; |
26 | 9 | use std::ops::Deref;
|
27 | 10 | use std::path::{Path, PathBuf};
|
28 |
| -use std::{env, fs}; |
29 | 11 |
|
30 | 12 | use syn::parse::{Parse, ParseStream};
|
31 | 13 | use syn::spanned::Spanned;
|
32 | 14 | use syn::visit::{self, Visit};
|
33 | 15 | use syn::Token;
|
34 | 16 |
|
35 |
| -type Error = Box<dyn std::error::Error>; |
36 |
| -type Result<T> = std::result::Result<T, Error>; |
37 |
| - |
38 |
| -#[test] |
39 |
| -fn check_style() { |
40 |
| - let root_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("../src"); |
41 |
| - walk(&root_dir).unwrap(); |
42 |
| - eprintln!("good style!"); |
43 |
| -} |
44 |
| - |
45 |
| -fn walk(root_dir: &Path) -> Result<()> { |
46 |
| - let mut style_checker = StyleChecker::new(); |
47 |
| - |
48 |
| - for entry in glob::glob(&format!( |
49 |
| - "{}/**/*.rs", |
50 |
| - root_dir.to_str().expect("dir should be valid UTF-8") |
51 |
| - ))? { |
52 |
| - let entry = entry?; |
53 |
| - |
54 |
| - let name = entry |
55 |
| - .file_name() |
56 |
| - .expect("file name should not end in ..") |
57 |
| - .to_str() |
58 |
| - .expect("file name should be valid UTF-8"); |
59 |
| - if let "lib.rs" | "macros.rs" = &name[..] { |
60 |
| - continue; |
61 |
| - } |
62 |
| - |
63 |
| - let path = entry.as_path(); |
64 |
| - style_checker.check_file(path)?; |
65 |
| - style_checker.reset_state(); |
66 |
| - } |
67 |
| - |
68 |
| - style_checker.finalize() |
69 |
| -} |
| 17 | +pub type Error = Box<dyn std::error::Error>; |
| 18 | +pub type Result<T> = std::result::Result<T, Error>; |
70 | 19 |
|
71 | 20 | #[derive(Default)]
|
72 |
| -struct StyleChecker { |
| 21 | +pub struct StyleChecker { |
73 | 22 | state: State,
|
74 | 23 | // FIXME: see StyleChecker::set_state
|
75 | 24 | _s_macros: usize,
|
@@ -106,32 +55,35 @@ enum ExprCfgElse {
|
106 | 55 | }
|
107 | 56 |
|
108 | 57 | impl StyleChecker {
|
109 |
| - fn new() -> Self { |
| 58 | + pub fn new() -> Self { |
110 | 59 | Self::default()
|
111 | 60 | }
|
112 | 61 |
|
113 | 62 | /// Reads and parses the file at the given path and checks
|
114 | 63 | /// for any style violations.
|
115 |
| - fn check_file(&mut self, path: &Path) -> Result<()> { |
| 64 | + pub fn check_file(&mut self, path: &Path) -> Result<()> { |
116 | 65 | let contents = fs::read_to_string(path)?;
|
117 |
| - let file = syn::parse_file(&contents)?; |
118 | 66 |
|
119 | 67 | self.path = PathBuf::from(path);
|
120 |
| - self.visit_file(&file); |
| 68 | + self.check_string(contents) |
| 69 | + } |
121 | 70 |
|
| 71 | + pub fn check_string(&mut self, contents: String) -> Result<()> { |
| 72 | + let file = syn::parse_file(&contents)?; |
| 73 | + self.visit_file(&file); |
122 | 74 | Ok(())
|
123 | 75 | }
|
124 | 76 |
|
125 | 77 | /// Resets the state of the [StyleChecker].
|
126 |
| - fn reset_state(&mut self) { |
| 78 | + pub fn reset_state(&mut self) { |
127 | 79 | *self = Self {
|
128 | 80 | errors: std::mem::take(&mut self.errors),
|
129 | 81 | ..Self::default()
|
130 | 82 | };
|
131 | 83 | }
|
132 | 84 |
|
133 | 85 | /// Collect all errors into a single error, reporting them if any.
|
134 |
| - fn finalize(self) -> Result<()> { |
| 86 | + pub fn finalize(self) -> Result<()> { |
135 | 87 | if self.errors.is_empty() {
|
136 | 88 | return Ok(());
|
137 | 89 | }
|
|
0 commit comments