-
-
Notifications
You must be signed in to change notification settings - Fork 645
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the Rust Scope code to its own file. (#21825)
As I will shortly be adding a "ScopeInfo" type struct on the Rust side, and piling it on to id.rs doesn't seem right. This is just a move of top-level symbols and their test. No logic changes.
- Loading branch information
Showing
8 changed files
with
72 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
use lazy_static::lazy_static; | ||
|
||
use regex::Regex; | ||
|
||
#[derive(Clone, Debug, Eq, Hash, PartialEq)] | ||
pub enum Scope { | ||
Global, | ||
Scope(String), | ||
} | ||
|
||
lazy_static! { | ||
// Note: must be aligned with the regex in src/python/pants/option/subsystem.py. | ||
static ref SCOPE_NAME_RE: Regex = Regex::new(r"^(?:[a-z0-9_])+(?:-(?:[a-z0-9_])+)*$").unwrap(); | ||
} | ||
|
||
pub(crate) fn is_valid_scope_name(name: &str) -> bool { | ||
// The exact string "pants" is not allowed as a scope name: if we encounter it on the | ||
// command line, it is part of the invocation: /path/to/python -m pants <actual args>. | ||
SCOPE_NAME_RE.is_match(name) && name != "pants" | ||
} | ||
|
||
impl Scope { | ||
pub fn named(name: &str) -> Scope { | ||
match name { | ||
"" | "GLOBAL" => Scope::Global, | ||
scope => Scope::Scope(scope.to_owned()), | ||
} | ||
} | ||
|
||
pub fn name(&self) -> &str { | ||
match self { | ||
Scope::Global => "GLOBAL", | ||
Scope::Scope(scope) => scope.as_str(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md). | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
use crate::scope::is_valid_scope_name; | ||
|
||
#[test] | ||
fn test_is_valid_scope_name() { | ||
assert!(is_valid_scope_name("test")); | ||
assert!(is_valid_scope_name("test1")); | ||
assert!(is_valid_scope_name("generate-lockfiles")); | ||
assert!(is_valid_scope_name("i_dont_like_underscores")); | ||
|
||
assert!(!is_valid_scope_name("pants")); | ||
assert!(!is_valid_scope_name("No-Caps")); | ||
assert!(!is_valid_scope_name("looks/like/a/target")); | ||
assert!(!is_valid_scope_name("//:target")); | ||
assert!(!is_valid_scope_name("-b")); | ||
assert!(!is_valid_scope_name("--flag=value")); | ||
} |