Skip to content

Commit b9587ac

Browse files
authored
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.
1 parent 56dc35e commit b9587ac

File tree

8 files changed

+72
-56
lines changed

8 files changed

+72
-56
lines changed

src/rust/engine/options/src/args.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
use std::env;
55

6-
use super::id::{is_valid_scope_name, NameTransform, OptionId, Scope};
6+
use super::id::{NameTransform, OptionId};
7+
use super::scope::{is_valid_scope_name, Scope};
78
use super::{DictEdit, OptionsSource};
89
use crate::cli_alias::{expand_aliases, AliasMap};
910
use crate::fromfile::FromfileExpander;

src/rust/engine/options/src/cli_alias.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2024 Pants project contributors (see CONTRIBUTORS.md).
22
// Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4-
use crate::Scope;
4+
use crate::scope::Scope;
55
use lazy_static::lazy_static;
66
use regex::Regex;
77
use std::collections::{HashMap, HashSet};

src/rust/engine/options/src/env.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ use std::collections::HashMap;
66
use std::env;
77
use std::ffi::OsString;
88

9-
use super::id::{NameTransform, OptionId, Scope};
9+
use super::id::{NameTransform, OptionId};
1010
use super::{DictEdit, OptionsSource};
1111
use crate::fromfile::FromfileExpander;
1212
use crate::parse::Parseable;
13+
use crate::scope::Scope;
1314
use crate::ListEdit;
1415

1516
#[derive(Debug)]

src/rust/engine/options/src/id.rs

+1-36
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,10 @@
11
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
22
// Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4-
use lazy_static::lazy_static;
4+
use crate::scope::Scope;
55
use std::fmt;
66
use std::fmt::{Display, Formatter};
77

8-
use regex::Regex;
9-
10-
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
11-
pub enum Scope {
12-
Global,
13-
Scope(String),
14-
}
15-
16-
lazy_static! {
17-
// Note: must be aligned with the regex in src/python/pants/option/subsystem.py.
18-
static ref SCOPE_NAME_RE: Regex = Regex::new(r"^(?:[a-z0-9_])+(?:-(?:[a-z0-9_])+)*$").unwrap();
19-
}
20-
21-
pub(crate) fn is_valid_scope_name(name: &str) -> bool {
22-
// The exact string "pants" is not allowed as a scope name: if we encounter it on the
23-
// command line, it is part of the invocation: /path/to/python -m pants <actual args>.
24-
SCOPE_NAME_RE.is_match(name) && name != "pants"
25-
}
26-
27-
impl Scope {
28-
pub fn named(name: &str) -> Scope {
29-
match name {
30-
"" | "GLOBAL" => Scope::Global,
31-
scope => Scope::Scope(scope.to_owned()),
32-
}
33-
}
34-
35-
pub fn name(&self) -> &str {
36-
match self {
37-
Scope::Global => "GLOBAL",
38-
Scope::Scope(scope) => scope.as_str(),
39-
}
40-
}
41-
}
42-
438
#[derive(Clone, Debug, Eq, PartialEq)]
449
pub struct OptionId {
4510
pub(crate) scope: Scope,

src/rust/engine/options/src/id_tests.rs

+2-16
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
22
// Licensed under the Apache License, Version 2.0 (see LICENSE).
33

4-
use crate::id::{is_valid_scope_name, OptionId, Scope};
4+
use crate::id::OptionId;
55
use crate::option_id;
6-
7-
#[test]
8-
fn test_is_valid_scope_name() {
9-
assert!(is_valid_scope_name("test"));
10-
assert!(is_valid_scope_name("test1"));
11-
assert!(is_valid_scope_name("generate-lockfiles"));
12-
assert!(is_valid_scope_name("i_dont_like_underscores"));
13-
14-
assert!(!is_valid_scope_name("pants"));
15-
assert!(!is_valid_scope_name("No-Caps"));
16-
assert!(!is_valid_scope_name("looks/like/a/target"));
17-
assert!(!is_valid_scope_name("//:target"));
18-
assert!(!is_valid_scope_name("-b"));
19-
assert!(!is_valid_scope_name("--flag=value"));
20-
}
6+
use crate::scope::Scope;
217

228
#[test]
239
fn test_option_id_global_switch() {

src/rust/engine/options/src/lib.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ mod parse;
3737
#[cfg(test)]
3838
mod parse_tests;
3939

40+
mod scope;
41+
#[cfg(test)]
42+
mod scope_tests;
43+
4044
#[cfg(test)]
4145
mod tests;
4246

@@ -61,7 +65,8 @@ use self::env::EnvReader;
6165
use crate::fromfile::FromfileExpander;
6266
use crate::parse::Parseable;
6367
pub use build_root::BuildRoot;
64-
pub use id::{OptionId, Scope};
68+
pub use id::OptionId;
69+
pub use scope::Scope;
6570
pub use types::OptionType;
6671

6772
// NB: The legacy Python options parser supported dicts with member_type "Any", which means

src/rust/engine/options/src/scope.rs

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
use lazy_static::lazy_static;
5+
6+
use regex::Regex;
7+
8+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
9+
pub enum Scope {
10+
Global,
11+
Scope(String),
12+
}
13+
14+
lazy_static! {
15+
// Note: must be aligned with the regex in src/python/pants/option/subsystem.py.
16+
static ref SCOPE_NAME_RE: Regex = Regex::new(r"^(?:[a-z0-9_])+(?:-(?:[a-z0-9_])+)*$").unwrap();
17+
}
18+
19+
pub(crate) fn is_valid_scope_name(name: &str) -> bool {
20+
// The exact string "pants" is not allowed as a scope name: if we encounter it on the
21+
// command line, it is part of the invocation: /path/to/python -m pants <actual args>.
22+
SCOPE_NAME_RE.is_match(name) && name != "pants"
23+
}
24+
25+
impl Scope {
26+
pub fn named(name: &str) -> Scope {
27+
match name {
28+
"" | "GLOBAL" => Scope::Global,
29+
scope => Scope::Scope(scope.to_owned()),
30+
}
31+
}
32+
33+
pub fn name(&self) -> &str {
34+
match self {
35+
Scope::Global => "GLOBAL",
36+
Scope::Scope(scope) => scope.as_str(),
37+
}
38+
}
39+
}
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2021 Pants project contributors (see CONTRIBUTORS.md).
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
use crate::scope::is_valid_scope_name;
5+
6+
#[test]
7+
fn test_is_valid_scope_name() {
8+
assert!(is_valid_scope_name("test"));
9+
assert!(is_valid_scope_name("test1"));
10+
assert!(is_valid_scope_name("generate-lockfiles"));
11+
assert!(is_valid_scope_name("i_dont_like_underscores"));
12+
13+
assert!(!is_valid_scope_name("pants"));
14+
assert!(!is_valid_scope_name("No-Caps"));
15+
assert!(!is_valid_scope_name("looks/like/a/target"));
16+
assert!(!is_valid_scope_name("//:target"));
17+
assert!(!is_valid_scope_name("-b"));
18+
assert!(!is_valid_scope_name("--flag=value"));
19+
}

0 commit comments

Comments
 (0)