Skip to content

Commit 508192c

Browse files
committed
chore: do not wrap one newtype in another newtype
1 parent e0d3c57 commit 508192c

File tree

3 files changed

+37
-55
lines changed

3 files changed

+37
-55
lines changed

Cargo.lock

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ dialoguer = "0.11.0"
5454
clap_complete_command = { version="0.6.1", features = ["nushell", "fig", "carapace"] }
5555
clap-verbosity-flag = "3.0.3"
5656
itertools = "0.14.0"
57-
nutype_test_util = "0.1.1"
5857

5958
[dev-dependencies]
6059
pretty_assertions = "1.4"

src/config.rs

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use anyhow::{anyhow, bail};
44
use itertools::Itertools;
55
use nutype::nutype;
6-
use std::{convert::Infallible, fmt::Display, num::NonZeroU32, path::PathBuf, str::FromStr};
6+
use std::{convert::Infallible, fmt::Display, path::PathBuf, str::FromStr};
77
use tap::Pipe as _;
88

99
use indexmap::IndexSet;
@@ -188,42 +188,41 @@ impl FromStr for Ref {
188188
}
189189

190190
/// Number of a pull request
191-
#[nutype_test_util::derive(From)]
192-
#[nutype(const_fn, derive(Eq, PartialEq, Display, Debug, FromStr, Copy, Clone))]
193-
pub struct PrNumber(NonZeroU32);
191+
#[nutype(
192+
validate(greater = 0),
193+
derive(Eq, PartialEq, Display, Debug, FromStr, Copy, Clone, TryFrom)
194+
)]
195+
pub struct PrNumber(u32);
194196

195197
/// Represents owner of a repository
196198
///
197199
/// E.g. in `helix-editor/helix/master`, this is `helix-editor`
198-
#[nutype_test_util::derive(From)]
199200
#[nutype(
200201
validate(not_empty),
201202
derive(
202-
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize
203+
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize, TryFrom
203204
)
204205
)]
205206
pub struct RepoOwner(String);
206207

207208
/// Represents name of a repository
208209
///
209210
/// E.g. in `helix-editor/helix/master`, this is `helix`
210-
#[nutype_test_util::derive(From)]
211211
#[nutype(
212212
validate(not_empty),
213213
derive(
214-
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize
214+
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize, TryFrom
215215
)
216216
)]
217217
pub struct RepoName(String);
218218

219219
/// Name of a branch in git
220220
///
221221
/// E.g. in `helix-editor/helix/master`, this is `master`
222-
#[nutype_test_util::derive(From)]
223222
#[nutype(
224223
validate(not_empty),
225224
derive(
226-
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize
225+
Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, Display, Serialize, TryFrom
227226
)
228227
)]
229228
pub struct BranchName(String);
@@ -239,8 +238,10 @@ impl FromStr for BranchName {
239238
}
240239

241240
/// File name of a patch
242-
#[nutype_test_util::derive(From)]
243-
#[nutype(validate(predicate = |p| !p.as_os_str().is_empty()), derive(Hash, Eq, PartialEq, Debug, AsRef, Deserialize, Clone, FromStr))]
241+
#[nutype(
242+
validate(predicate = |p| !p.as_os_str().is_empty()),
243+
derive(Hash, Eq, PartialEq, Debug, AsRef, Deserialize, Clone, FromStr)
244+
)]
244245
pub struct PatchName(PathBuf);
245246

246247
impl Display for PatchName {
@@ -250,10 +251,10 @@ impl Display for PatchName {
250251
}
251252

252253
/// Represents a git commit hash
253-
#[nutype_test_util::derive(From)]
254+
// #[cfg_attr(test, nutype_test_util::derive(From))]
254255
#[nutype(
255256
validate(not_empty, predicate = is_valid_commit_hash),
256-
derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef)
257+
derive(Debug, Eq, PartialEq, Ord, PartialOrd, Clone, AsRef, TryFrom)
257258
)]
258259
pub struct Commit(String);
259260

@@ -307,45 +308,45 @@ mod tests {
307308
(
308309
"helix-editor/helix/master @ 1a2b3c",
309310
Remote {
310-
owner: "helix-editor".into(),
311-
repo: "helix".into(),
312-
branch: "master".into(),
313-
commit: Some("1a2b3c".into()),
311+
owner: "helix-editor".try_into().unwrap(),
312+
repo: "helix".try_into().unwrap(),
313+
branch: "master".try_into().unwrap(),
314+
commit: Some("1a2b3c".try_into().unwrap()),
314315
},
315316
),
316317
(
317318
"helix-editor/helix @ deadbeef",
318319
Remote {
319-
owner: "helix-editor".into(),
320-
repo: "helix".into(),
321-
branch: Remote::DEFAULT_BRANCH.into(),
322-
commit: Some("deadbeef".into()),
320+
owner: "helix-editor".try_into().unwrap(),
321+
repo: "helix".try_into().unwrap(),
322+
branch: Remote::DEFAULT_BRANCH.try_into().unwrap(),
323+
commit: Some("deadbeef".try_into().unwrap()),
323324
},
324325
),
325326
(
326327
"helix-editor/helix/feat/feature-x @ abc123",
327328
Remote {
328-
owner: "helix-editor".into(),
329-
repo: "helix".into(),
330-
branch: "feat/feature-x".into(),
331-
commit: Some("abc123".into()),
329+
owner: "helix-editor".try_into().unwrap(),
330+
repo: "helix".try_into().unwrap(),
331+
branch: "feat/feature-x".try_into().unwrap(),
332+
commit: Some("abc123".try_into().unwrap()),
332333
},
333334
),
334335
(
335336
"owner/repo/branch",
336337
Remote {
337-
owner: "owner".into(),
338-
repo: "repo".into(),
339-
branch: "branch".into(),
338+
owner: "owner".try_into().unwrap(),
339+
repo: "repo".try_into().unwrap(),
340+
branch: "branch".try_into().unwrap(),
340341
commit: None,
341342
},
342343
),
343344
(
344345
"owner/repo",
345346
Remote {
346-
owner: "owner".into(),
347-
repo: "repo".into(),
348-
branch: Remote::DEFAULT_BRANCH.into(),
347+
owner: "owner".try_into().unwrap(),
348+
repo: "repo".try_into().unwrap(),
349+
branch: Remote::DEFAULT_BRANCH.try_into().unwrap(),
349350
commit: None,
350351
},
351352
),
@@ -371,32 +372,26 @@ patches = ['remove-tab']"#;
371372

372373
let conf = toml::from_str::<Config>(config).unwrap();
373374

374-
macro_rules! pr_number {
375-
($num:literal) => {
376-
PrNumber::new(const { ::std::num::NonZeroU32::new($num).expect("nonzero") })
377-
};
378-
}
379-
380375
pretty_assertions::assert_eq!(
381376
conf,
382377
Config {
383378
local_branch: BranchName::try_new("patchy".to_string()).unwrap(),
384379
patches: indexset![PatchName::try_new("remove-tab".into()).unwrap()],
385380
pull_requests: vec![
386381
PullRequest {
387-
number: pr_number!(10000),
382+
number: 10000.try_into().unwrap(),
388383
commit: None
389384
},
390385
PullRequest {
391-
number: pr_number!(10000),
386+
number: 10000.try_into().unwrap(),
392387
commit: None
393388
},
394389
PullRequest {
395-
number: pr_number!(454),
390+
number: 454.try_into().unwrap(),
396391
commit: Some(Commit::try_new("a1b2c3").unwrap())
397392
},
398393
PullRequest {
399-
number: pr_number!(1),
394+
number: 1.try_into().unwrap(),
400395
commit: Some(Commit::try_new("a1b2c3").unwrap())
401396
},
402397
],

0 commit comments

Comments
 (0)