Skip to content

Commit 82c9dfb

Browse files
authored
Merge pull request #28 from DataDog/chore/trait-helpers
chore: expose trait implementation helpers
2 parents bdd6cd2 + 4efd1c9 commit 82c9dfb

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

cargo_pup_lint_config/src/struct_lint/builder.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ impl<'a> StructConstraintBuilder<'a> {
127127
self
128128
}
129129

130+
/// Add a rule requiring the struct to implement a given trait
131+
pub fn must_implement_trait(mut self, trait_path: impl Into<String>) -> Self {
132+
self.add_rule_internal(StructRule::ImplementsTrait(
133+
trait_path.into(),
134+
self.current_severity,
135+
));
136+
self
137+
}
138+
139+
/// Add a rule requiring the struct NOT to implement a given trait
140+
pub fn must_not_implement_trait(mut self, trait_path: impl Into<String>) -> Self {
141+
let inner = StructRule::ImplementsTrait(trait_path.into(), self.current_severity);
142+
self.add_rule_internal(StructRule::Not(Box::new(inner)));
143+
self
144+
}
145+
130146
/// Add a rule requiring the struct to have public visibility
131147
pub fn must_be_public(mut self) -> Self {
132148
self.add_rule_internal(StructRule::MustBePublic(self.current_severity));

test_app/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_app/tests/pup_ron_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ fn test_lint_config() {
8282
// Trait restrictions
8383
builder.struct_lint()
8484
.lint_named("trait_restrictions")
85-
.matching(|m|
86-
m.implements_trait("^test_app::trait_impl::MyTrait$"))
85+
.matching(|m| m.implements_trait("^test_app::trait_impl::MyTrait$"))
8786
.with_severity(Severity::Warn)
88-
.must_be_named(".*MyTraitImpl$".into())
87+
.must_be_named(".*MyTraitImpl$".into())
8988
.must_be_private()
89+
.must_implement_trait("test_app::trait_impl::MyTrait")
9090
.build();
9191

9292
// Result error implementation check

0 commit comments

Comments
 (0)