bind/
linter.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::errors::UserError;
6use crate::parser::bind_library;
7use crate::parser::common::CompoundIdentifier;
8use std::fmt;
9use thiserror::Error;
10
11#[derive(Debug, Error, Clone, PartialEq)]
12pub enum LinterError {
13    LibraryNameMustNotContainUnderscores(CompoundIdentifier),
14}
15
16impl fmt::Display for LinterError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        write!(f, "{}", UserError::from(self.clone()))
19    }
20}
21
22pub fn lint_library<'a>(library: &bind_library::Ast) -> Result<(), LinterError> {
23    if library.name.name.contains("_") {
24        return Err(LinterError::LibraryNameMustNotContainUnderscores(library.name.clone()));
25    }
26
27    Ok(())
28}
29
30#[cfg(test)]
31mod tests {
32    use super::*;
33    use crate::make_identifier;
34
35    #[test]
36    fn library_lint_success() {
37        let ast = bind_library::Ast {
38            name: make_identifier!["valid", "library", "name"],
39            using: vec![],
40            declarations: vec![],
41        };
42
43        assert_eq!(lint_library(&ast), Ok(()));
44    }
45
46    #[test]
47    fn library_name_has_underscores() {
48        let ast = bind_library::Ast {
49            name: make_identifier!["invalid", "library_name"],
50            using: vec![],
51            declarations: vec![],
52        };
53
54        assert_eq!(
55            lint_library(&ast),
56            Err(LinterError::LibraryNameMustNotContainUnderscores(make_identifier![
57                "invalid",
58                "library_name"
59            ]))
60        );
61    }
62}