Skip to content

Commit 3c2d0ca

Browse files
committed
Make is_valid_*() methods into regular functions
1 parent 8067c47 commit 3c2d0ca

File tree

4 files changed

+149
-146
lines changed

4 files changed

+149
-146
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ v0.6.0 (in development)
1414
- `PartialOrd<str>`
1515
- `TryFrom<String>`
1616
- Added `GHRepo::as_str()` method
17+
- **Breaking**: The `GHRepo::is_valid_owner()` and `GHRepo::is_valid_name()`
18+
methods are now regular functions
1719

1820
v0.5.0 (2023-04-27)
1921
-------------------

src/lib.rs

Lines changed: 52 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ impl GHRepo {
129129
/// If `owner` is not a valid GitHub owner name, or if `name` is not a
130130
/// valid GitHub repository name, returns [`ParseError`].
131131
pub fn new(owner: &str, name: &str) -> Result<Self, ParseError> {
132-
if !GHRepo::is_valid_owner(owner) {
132+
if !is_valid_owner(owner) {
133133
Err(ParseError::InvalidOwner(owner.to_string()))
134-
} else if !GHRepo::is_valid_name(name) {
134+
} else if !is_valid_name(name) {
135135
Err(ParseError::InvalidName(name.to_string()))
136136
} else {
137137
Ok(GHRepo {
@@ -141,57 +141,6 @@ impl GHRepo {
141141
}
142142
}
143143

144-
/// Test whether a string is a valid GitHub user login or organization
145-
/// name.
146-
///
147-
/// As of 2017-07-23, trying to sign up to GitHub with an invalid username
148-
/// or create an organization with an invalid name gives the message
149-
/// "Username may only contain alphanumeric characters or single hyphens,
150-
/// and cannot begin or end with a hyphen". Additionally, trying to create
151-
/// a user named "none" (case insensitive) gives the message "Username name
152-
/// 'none' is a reserved word." Unfortunately, there are a number of users
153-
/// who made accounts before the current name restrictions were put in
154-
/// place, and so this method also needs to accept names that contain
155-
/// underscores, contain multiple consecutive hyphens, begin with a hyphen,
156-
/// and/or end with a hyphen.
157-
///
158-
/// As this function endeavors to accept all usernames that were valid at
159-
/// any point, just because a name is accepted doesn't necessarily mean you
160-
/// can create a user by that name on GitHub today.
161-
///
162-
/// # Example
163-
///
164-
/// ```
165-
/// # use ghrepo::GHRepo;
166-
/// assert!(GHRepo::is_valid_owner("octocat"));
167-
/// assert!(GHRepo::is_valid_owner("octo-cat"));
168-
/// assert!(!GHRepo::is_valid_owner("octo.cat"));
169-
/// assert!(!GHRepo::is_valid_owner("octocat/repository"));
170-
/// assert!(!GHRepo::is_valid_owner("none"));
171-
/// ```
172-
pub fn is_valid_owner(s: &str) -> bool {
173-
matches!(split_owner(s), Some((_, "")))
174-
}
175-
176-
/// Test whether a string is a valid repository name.
177-
///
178-
/// Testing as of 2017-05-21 indicates that repository names can be
179-
/// composed of alphanumeric ASCII characters, hyphens, periods, and/or
180-
/// underscores, with the names `.` and `..` being reserved and names
181-
/// ending with `.git` (case insensitive) forbidden.
182-
///
183-
/// # Example
184-
///
185-
/// ```
186-
/// # use ghrepo::GHRepo;
187-
/// assert!(GHRepo::is_valid_name("my-repo"));
188-
/// assert!(!GHRepo::is_valid_name("my-repo.git"));
189-
/// assert!(!GHRepo::is_valid_owner("octocat/my-repo"));
190-
/// ```
191-
pub fn is_valid_name(s: &str) -> bool {
192-
matches!(split_name(s), Some((_, "")))
193-
}
194-
195144
/// Like [`GHRepo::from_str()`], except that if `s` is just a repository
196145
/// name without an owner, the owner will be set to `owner`
197146
///
@@ -217,7 +166,7 @@ impl GHRepo {
217166
/// # }
218167
/// ```
219168
pub fn from_str_with_owner(s: &str, owner: &str) -> Result<Self, ParseError> {
220-
if GHRepo::is_valid_name(s) {
169+
if is_valid_name(s) {
221170
GHRepo::new(owner, s)
222171
} else {
223172
GHRepo::from_str(s)
@@ -680,3 +629,52 @@ impl From<ParseError> for LocalRepoError {
680629
LocalRepoError::InvalidRemoteURL(e)
681630
}
682631
}
632+
633+
/// Test whether a string is a valid GitHub user login or organization name.
634+
///
635+
/// As of 2017-07-23, trying to sign up to GitHub with an invalid username or
636+
/// create an organization with an invalid name gives the message "Username may
637+
/// only contain alphanumeric characters or single hyphens, and cannot begin or
638+
/// end with a hyphen". Additionally, trying to create a user named "none"
639+
/// (case insensitive) gives the message "Username name 'none' is a reserved
640+
/// word." Unfortunately, there are a number of users who made accounts before
641+
/// the current name restrictions were put in place, and so this method also
642+
/// needs to accept names that contain underscores, contain multiple
643+
/// consecutive hyphens, begin with a hyphen, and/or end with a hyphen.
644+
///
645+
/// As this function endeavors to accept all usernames that were valid at any
646+
/// point, just because a name is accepted doesn't necessarily mean you can
647+
/// create a user by that name on GitHub today.
648+
///
649+
/// # Example
650+
///
651+
/// ```
652+
/// # use ghrepo::is_valid_owner;
653+
/// assert!(is_valid_owner("octocat"));
654+
/// assert!(is_valid_owner("octo-cat"));
655+
/// assert!(!is_valid_owner("octo.cat"));
656+
/// assert!(!is_valid_owner("octocat/repository"));
657+
/// assert!(!is_valid_owner("none"));
658+
/// ```
659+
pub fn is_valid_owner(s: &str) -> bool {
660+
matches!(split_owner(s), Some((_, "")))
661+
}
662+
663+
/// Test whether a string is a valid repository name.
664+
///
665+
/// Testing as of 2017-05-21 indicates that repository names can be composed of
666+
/// alphanumeric ASCII characters, hyphens, periods, and/or underscores, with
667+
/// the names `.` and `..` being reserved and names ending with `.git` (case
668+
/// insensitive) forbidden.
669+
///
670+
/// # Example
671+
///
672+
/// ```
673+
/// # use ghrepo::is_valid_name;
674+
/// assert!(is_valid_name("my-repo"));
675+
/// assert!(!is_valid_name("my-repo.git"));
676+
/// assert!(!is_valid_name("octocat/my-repo"));
677+
/// ```
678+
pub fn is_valid_name(s: &str) -> bool {
679+
matches!(split_name(s), Some((_, "")))
680+
}

tests/ghrepo.rs

Lines changed: 0 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -74,98 +74,6 @@ fn test_ssh_url() {
7474
assert_eq!(r.ssh_url().parse::<GHRepo>(), Ok(r));
7575
}
7676

77-
#[rstest]
78-
#[case("steven-universe")]
79-
#[case("steven")]
80-
#[case("s")]
81-
#[case("s-u")]
82-
#[case("7152")]
83-
#[case("s-t-e-v-e-n")]
84-
#[case("s-t-eeeeee-v-e-n")]
85-
#[case("peridot-2F5L-5XG")]
86-
#[case("nonely")]
87-
#[case("none-one")]
88-
#[case("none-none")]
89-
#[case("nonenone")]
90-
#[case("none0")]
91-
#[case("0none")]
92-
// The following are actual usernames on GitHub that violate the current
93-
// username restrictions:
94-
#[case("-")]
95-
#[case("-Jerry-")]
96-
#[case("-SFT-Clan")]
97-
#[case("123456----")]
98-
#[case("FirE-Fly-")]
99-
#[case("None-")]
100-
#[case("alex--evil")]
101-
#[case("johan--")]
102-
#[case("pj_nitin")]
103-
#[case("up_the_irons")]
104-
fn test_good_owner(#[case] owner: &str) {
105-
assert!(GHRepo::is_valid_owner(owner));
106-
}
107-
108-
#[rstest]
109-
#[case("steven.universe")]
110-
#[case("steven-universe@beachcity.dv")]
111-
#[case("steven-univerß")]
112-
#[case("")]
113-
#[case("none")]
114-
#[case("NONE")]
115-
fn test_bad_owner(#[case] owner: &str) {
116-
assert!(!GHRepo::is_valid_owner(owner));
117-
}
118-
119-
#[rstest]
120-
#[case("steven-universe")]
121-
#[case("steven")]
122-
#[case("s")]
123-
#[case("s-u")]
124-
#[case("7152")]
125-
#[case("s-t-e-v-e-n")]
126-
#[case("s-t-eeeeee-v-e-n")]
127-
#[case("peridot-2F5L-5XG")]
128-
#[case("...")]
129-
#[case("-steven")]
130-
#[case("steven-")]
131-
#[case("-steven-")]
132-
#[case("steven.universe")]
133-
#[case("steven_universe")]
134-
#[case("steven--universe")]
135-
#[case("s--u")]
136-
#[case("git.steven")]
137-
#[case("steven.git.txt")]
138-
#[case("steven.gitt")]
139-
#[case(".gitt")]
140-
#[case("..gitt")]
141-
#[case("...gitt")]
142-
#[case("git")]
143-
#[case("-")]
144-
#[case("_")]
145-
#[case("---")]
146-
#[case(".---")]
147-
#[case(".steven")]
148-
#[case("..steven")]
149-
#[case("...steven")]
150-
fn test_good_name(#[case] name: &str) {
151-
assert!(GHRepo::is_valid_name(name));
152-
}
153-
154-
#[rstest]
155-
#[case("steven-univerß")]
156-
#[case(".")]
157-
#[case("..")]
158-
#[case("...git")]
159-
#[case("..git")]
160-
#[case(".git")]
161-
#[case("")]
162-
#[case("steven.git")]
163-
#[case("steven.GIT")]
164-
#[case("steven.Git")]
165-
fn test_bad_name(#[case] name: &str) {
166-
assert!(!GHRepo::is_valid_name(name));
167-
}
168-
16977
#[template]
17078
#[rstest]
17179
#[case("git://github.com/jwodder/headerparser", "jwodder", "headerparser")]

tests/valid.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#![allow(clippy::items_after_test_module)]
2+
use ghrepo::{is_valid_name, is_valid_owner};
3+
use rstest::rstest;
4+
5+
#[rstest]
6+
#[case("steven-universe")]
7+
#[case("steven")]
8+
#[case("s")]
9+
#[case("s-u")]
10+
#[case("7152")]
11+
#[case("s-t-e-v-e-n")]
12+
#[case("s-t-eeeeee-v-e-n")]
13+
#[case("peridot-2F5L-5XG")]
14+
#[case("nonely")]
15+
#[case("none-one")]
16+
#[case("none-none")]
17+
#[case("nonenone")]
18+
#[case("none0")]
19+
#[case("0none")]
20+
// The following are actual usernames on GitHub that violate the current
21+
// username restrictions:
22+
#[case("-")]
23+
#[case("-Jerry-")]
24+
#[case("-SFT-Clan")]
25+
#[case("123456----")]
26+
#[case("FirE-Fly-")]
27+
#[case("None-")]
28+
#[case("alex--evil")]
29+
#[case("johan--")]
30+
#[case("pj_nitin")]
31+
#[case("up_the_irons")]
32+
fn test_good_owner(#[case] owner: &str) {
33+
assert!(is_valid_owner(owner));
34+
}
35+
36+
#[rstest]
37+
#[case("steven.universe")]
38+
#[case("steven-universe@beachcity.dv")]
39+
#[case("steven-univerß")]
40+
#[case("")]
41+
#[case("none")]
42+
#[case("NONE")]
43+
fn test_bad_owner(#[case] owner: &str) {
44+
assert!(!is_valid_owner(owner));
45+
}
46+
47+
#[rstest]
48+
#[case("steven-universe")]
49+
#[case("steven")]
50+
#[case("s")]
51+
#[case("s-u")]
52+
#[case("7152")]
53+
#[case("s-t-e-v-e-n")]
54+
#[case("s-t-eeeeee-v-e-n")]
55+
#[case("peridot-2F5L-5XG")]
56+
#[case("...")]
57+
#[case("-steven")]
58+
#[case("steven-")]
59+
#[case("-steven-")]
60+
#[case("steven.universe")]
61+
#[case("steven_universe")]
62+
#[case("steven--universe")]
63+
#[case("s--u")]
64+
#[case("git.steven")]
65+
#[case("steven.git.txt")]
66+
#[case("steven.gitt")]
67+
#[case(".gitt")]
68+
#[case("..gitt")]
69+
#[case("...gitt")]
70+
#[case("git")]
71+
#[case("-")]
72+
#[case("_")]
73+
#[case("---")]
74+
#[case(".---")]
75+
#[case(".steven")]
76+
#[case("..steven")]
77+
#[case("...steven")]
78+
fn test_good_name(#[case] name: &str) {
79+
assert!(is_valid_name(name));
80+
}
81+
82+
#[rstest]
83+
#[case("steven-univerß")]
84+
#[case(".")]
85+
#[case("..")]
86+
#[case("...git")]
87+
#[case("..git")]
88+
#[case(".git")]
89+
#[case("")]
90+
#[case("steven.git")]
91+
#[case("steven.GIT")]
92+
#[case("steven.Git")]
93+
fn test_bad_name(#[case] name: &str) {
94+
assert!(!is_valid_name(name));
95+
}

0 commit comments

Comments
 (0)