use std::fmt;
macro_rules! rule_ids {
($($variant:ident => $id:literal,)+) => {
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum RuleId {
$(
#[doc = $id]
$variant,
)+
}
impl RuleId {
pub const ALL: &'static [Self] = &[$(Self::$variant),+];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
$(Self::$variant => $id),+
}
}
#[must_use]
pub fn parse(id: &str) -> Option<Self> {
match id {
$($id => Some(Self::$variant),)+
_ => None,
}
}
}
impl serde::Serialize for RuleId {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}
impl<'de> serde::Deserialize<'de> for RuleId {
fn deserialize<D: serde::Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
let id = <std::borrow::Cow<'_, str> as serde::Deserialize>::deserialize(
deserializer,
)?;
Self::parse(&id).ok_or_else(|| {
serde::de::Error::custom(format!("no spec defines the rule {id}"))
})
}
}
};
}
rule_ids! {
RecordedDimensionOnlyShrinks => "budget-debt:a-recorded-dimension-only-shrinks",
DebtIsCreatedByAnExplicitAct => "budget-debt:debt-is-created-by-an-explicit-act",
CellCarriesOneReference => "comparison-docs:a-cell-carries-one-reference",
ComparisonCarriesALegend => "comparison-docs:a-comparison-carries-a-legend",
VerdictCarriesItsWord => "comparison-docs:a-verdict-carries-its-word",
EveryTableIsDated => "comparison-docs:every-table-is-dated",
TablePipesAreEscaped => "comparison-docs:table-pipes-are-escaped",
CitationResolvesToARule => "decision-records:a-citation-resolves-to-a-rule",
BodyStaysWithinWordCap => "decision-records:body-stays-within-350-words",
FilenameCarriesNoDigit => "decision-records:filename-carries-no-digit",
MergedRecordIsPermanent => "decision-records:merged-record-is-permanent",
RecordIsNotRevised => "decision-records:record-is-not-revised",
DeclaredLocationIsNamedByItsVariable => "distribution:a-declared-location-is-named-by-its-variable",
LandingClassifiesItsTargetFirst => "distribution:a-landing-classifies-its-target-first",
SeededRuleRunsNoCanonCommand => "distribution:a-seeded-rule-runs-no-canon-command",
SkillChecksItsHostBeforeItPlans => "distribution:a-skill-checks-its-host-before-it-plans",
SkillHasOneOwner => "distribution:a-skill-has-one-owner",
SkillInstallRestoresOnFailure => "distribution:a-skill-install-restores-on-failure",
SkillObeysThePortableFormat => "distribution:a-skill-obeys-the-portable-format",
SkillPlansBeforeItActs => "distribution:a-skill-plans-before-it-acts",
InstallSweepsWhatThePayloadDropped => "distribution:an-install-sweeps-what-the-payload-dropped",
InitializationPreservesProjectContent => "distribution:initialization-preserves-project-content",
InstancesOperateOffline => "distribution:instances-operate-offline",
ManifestIdentifiesEveryOwnedFile => "distribution:manifest-identifies-every-owned-file",
DeclarationIsSeededOnceAndThenOwned => "distribution:the-declaration-is-seeded-once-and-then-owned",
SkillPackageIsSelfContained => "distribution:a-skill-package-is-self-contained",
SkillInstallPreviewsBeforeWriting => "distribution:skill-install-previews-before-writing",
SkillUninstallRemovesOnlyWhatItWrote => "distribution:skill-uninstall-removes-only-what-it-wrote",
SkillsArePartOfThePayload => "distribution:skills-are-part-of-the-payload",
TheDoctorAnswersForTheInstalledSkills => "distribution:the-doctor-answers-for-the-installed-skills",
ThePayloadNamesNoOtherProject => "distribution:the-payload-names-no-other-project",
ThePayloadNamesNoPlanningTool => "distribution:the-payload-names-no-planning-tool",
ThePayloadRootsAreDeclaredOnce => "distribution:the-payload-roots-are-declared-once",
UpgradeConflictsAreAtomic => "distribution:upgrade-conflicts-are-atomic",
UserScopeFilesStayUnrecorded => "distribution:user-scope-files-stay-unrecorded",
UserScopeReceiptIsRequiredState => "distribution:a-user-scope-receipt-is-required-state",
PlanIsStoredAndAppliedByItsId => "reconcile:a-plan-is-stored-and-applied-by-its-id",
ApplyRefusesAPlanWhoseInputsMoved => "reconcile:an-apply-refuses-a-plan-whose-inputs-moved",
OneWriterHoldsATarget => "reconcile:one-writer-holds-a-target",
PlanWritesNothing => "reconcile:a-plan-writes-nothing",
DecisionPrecedesWhatDependsOnIt => "reconcile:a-decision-precedes-what-depends-on-it",
FindingIsSomethingTheProgramProved => "reconcile:a-finding-is-something-the-program-proved",
WriteIntoAdoptedStateIsAnOperatorAct => "reconcile:a-write-into-adopted-state-is-an-operator-act",
IncrementalScopeLeavesNoStructuralFinding => "reconcile:an-incremental-scope-leaves-no-structural-finding",
OnePlanIsTheInputToEveryWrite => "reconcile:one-plan-is-the-input-to-every-write",
ReadinessIsTheWorstPrecondition => "reconcile:readiness-is-the-worst-precondition",
FingerprintCoversWhatTheApplyWouldDo => "reconcile:the-fingerprint-covers-what-the-apply-would-do",
PlannerIsDeterministic => "reconcile:the-planner-is-deterministic",
TargetDecidesItsClassification => "reconcile:the-target-decides-its-classification",
ReleaseIsReadThroughOneSeam => "bundle:a-release-is-read-through-one-seam",
ReleaseDeclaresWhatItLands => "bundle:a-release-declares-what-it-lands",
ProtocolVersionIsARangeTheEngineDeclares => "bundle:the-protocol-version-is-a-range-the-engine-declares",
PreSchemaReleaseIsCatalogedOrUnavailable => "bundle:a-pre-schema-release-is-cataloged-or-unavailable",
FetchedArchiveIsVerifiedBeforeItIsRead => "bundle:a-fetched-archive-is-verified-before-it-is-read",
ResolutionHappensOnceAndWritesOnlyTheCache => "bundle:resolution-happens-once-and-writes-only-the-cache",
RunThatDidNotFinishIsRolledBack => "reconcile:a-run-that-did-not-finish-is-rolled-back",
ApplyProvesEveryPostconditionItReports => "reconcile:an-apply-proves-every-postcondition-it-reports",
DestinationIsContainedBeforeItIsWritten => "reconcile:a-destination-is-contained-before-it-is-written",
PlanIdIsAFingerprintAndNeverAPath => "reconcile:a-plan-id-is-a-fingerprint-and-never-a-path",
ApplyResolvesNothing => "reconcile:an-apply-resolves-nothing",
TargetRecordsTheReleaseItHolds => "reconcile:a-target-records-the-release-it-holds",
ReleaseDeclaresWhatItAsksOfItsOperator => "reconcile:a-release-declares-what-it-asks-of-its-operator",
OneCatalogDescribesEveryServedDocument => "docs-discovery:one-catalog-describes-every-served-document",
InstanceIsRoutedToTheIndex => "docs-discovery:an-instance-is-routed-to-the-index",
AuthorInstructionsStayWithinBudget => "docs-format:author-instructions-stay-within-budget",
ChapterStaysWithinLineCap => "docs-format:chapter-stays-within-200-lines",
DocumentStatesThePresent => "docs-format:document-states-the-present",
DocumentUsesStructuralMarkdownOnly => "docs-format:document-uses-structural-markdown-only",
EveryBudgetCarriesAGate => "docs-format:every-budget-carries-a-gate",
FenceDeclaresALanguage => "docs-format:fence-declares-a-language",
ProseStaysUnwrapped => "docs-format:prose-stays-unwrapped",
DocumentCarriesNoPersonalPath => "docs-foundations:a-document-carries-no-personal-path",
DocumentDirectoryExplainsItself => "docs-foundations:a-document-directory-explains-itself",
DocumentOwnsWhatItGoverns => "docs-foundations:a-document-owns-what-it-governs",
KindPrefixCarriesASlug => "docs-foundations:a-kind-prefix-carries-a-slug",
ArtifactFilenamesCarryAKindPrefix => "docs-foundations:artifact-filenames-carry-a-kind-prefix",
CompanionArtifactsShareTheSpecName => "docs-foundations:companion-artifacts-share-the-spec-name",
SpecStatesThePresent => "docs-foundations:spec-states-the-present",
SpecWinsOverRecord => "docs-foundations:spec-wins-over-record",
SpecsAreCentralized => "docs-foundations:specs-are-centralized",
ProhibitionsAreCapped => "docs-specs:prohibitions-are-capped",
RequirementCarriesAVerification => "docs-specs:requirement-carries-a-verification",
RequirementCarriesFiveParts => "docs-specs:requirement-carries-five-parts",
RuleIdIsUniqueAndSlugged => "docs-specs:rule-id-is-unique-and-slugged",
RuleIdOutlivesItsSentence => "docs-specs:rule-id-outlives-its-sentence",
SpecStaysWithinLineCap => "docs-specs:spec-stays-within-300-lines",
StatementUsesAnEarsPattern => "docs-specs:statement-uses-an-ears-pattern",
UnenforcedRulesAreDeclared => "docs-specs:unenforced-rules-are-declared",
VerificationNamesALiveHook => "docs-specs:verification-names-a-live-hook",
DivergentResultNamesItsDestination => "guides:a-divergent-result-names-its-destination",
ManualStepEnumeratesItsInteraction => "guides:a-manual-step-enumerates-its-interaction",
StepFollowsItsProducers => "guides:a-step-follows-its-producers",
StepIsOneImperativeAction => "guides:a-step-is-one-imperative-action",
ExternalFactIsVerifiedUpstream => "guides:an-external-fact-is-verified-upstream",
CitationsLiveInTheReferenceZone => "guides:citations-live-in-the-reference-zone",
EveryStepCarriesItsCheck => "guides:every-step-carries-its-check",
PreconditionsOpenAndVerificationCloses => "guides:preconditions-open-and-verification-closes",
TheManifestStaysReadable => "instance:the-manifest-stays-readable",
AgentsBlockStaysManaged => "instance:the-agents-block-stays-managed",
TrackingRegistryStaysValid => "instance:the-tracking-registry-stays-valid",
ProjectDeclaresWhatItsGatesJudge => "instance:the-project-declares-what-its-gates-judge",
ManagedBlockAgreesWithTheDeclaration => "instance:the-managed-block-agrees-with-the-declaration",
BugzillaReportBodyFitsReportWidth => "known-issues:a-bugzilla-report-body-fits-in-79-columns",
FiledRecordCarriesItsReport => "known-issues:a-filed-record-carries-its-report",
RecordCarriesItsRetirementCondition => "known-issues:a-record-carries-its-retirement-condition",
RecordCarriesOneFilingState => "known-issues:a-record-carries-one-filing-state",
RecordCarriesOneState => "known-issues:a-record-carries-one-state",
RecordRecordsItsLastCheck => "known-issues:a-record-records-its-last-check",
RecordWalksTheMechanism => "known-issues:a-record-walks-the-mechanism",
CaseIdIsASlug => "known-issues:case-id-is-a-slug",
CanonGateIsNotDelivered => "release:a-canon-gate-is-not-delivered",
DeliveredGateReadsWhatTheConventionOwns => "release:a-delivered-gate-reads-what-the-convention-owns",
ReleasedVersionIsNotReAuthored => "release:a-released-version-is-not-re-authored",
TagDerivesFromTheVersionFile => "release:a-tag-derives-from-the-version-file",
EveryReleaseDeclaresWhatItAsks => "release:every-release-declares-what-it-asks",
LicenseDeclaresBothHalves => "release:license-declares-both-halves",
CanonRecordDescribesItsTree => "release:the-canon-record-describes-its-tree",
RkPinHasTwoFactsAndOneMover => "release:the-rk-pin-has-two-facts-and-one-mover",
DeliveredGateSetIsDeclaredOnce => "release:the-delivered-gate-set-is-declared-once",
ThirdPartyNoticesTravelWithThePayload => "release:third-party-notices-travel-with-the-payload",
VersionsAreSemanticAndAligned => "release:versions-are-semantic-and-aligned",
CommentCitesTheRule => "spec-to-code:a-comment-cites-the-rule",
CommentNamesNoRecord => "spec-to-code:a-comment-names-no-record",
GateMessageCitesTheRule => "spec-to-code:a-gate-message-cites-the-rule",
SpecChangeIsTyped => "spec-to-code:a-spec-change-is-typed",
SpecMayLeadItsCode => "spec-to-code:a-spec-may-lead-its-code",
SuppressionNamesItsCase => "spec-to-code:a-suppression-names-its-case",
EntryDocumentCitesRuleIds => "spec-to-code:an-entry-document-cites-rule-ids",
UnenactedRulesAreTheBacklog => "spec-to-code:unenacted-rules-are-the-backlog",
DeclaredDependentExists => "tracking:a-declared-dependent-exists",
PerishableSourceIsRegistered => "tracking:a-perishable-source-is-registered",
EntryDeclaresHowToRevalidate => "tracking:an-entry-declares-how-to-revalidate",
OverdueEntryBlocks => "tracking:an-overdue-entry-blocks",
UpstreamCheckDoesNotEditTheTree => "tracking:an-upstream-check-does-not-edit-the-tree",
UpstreamDerivationPinsARevision => "tracking:an-upstream-derivation-pins-a-revision",
RegistryHasOneReadableShape => "tracking:the-registry-has-one-readable-shape",
ExistingDocumentConvertsWhenEdited => "writing-style:an-existing-document-converts-when-edited",
NoDeliveredGateJudgesProse => "writing-style:no-delivered-gate-judges-prose",
SourcesNameTheRevisionRead => "writing-style:sources-name-the-revision-read",
DocumentationBlockRoutesToTheStyle => "writing-style:the-documentation-block-routes-to-the-style",
StyleLivesInOneDocument => "writing-style:the-style-lives-in-one-document",
NoneImposesNoObligation => "writing-policy:none-imposes-no-obligation",
ProjectSelectsOneSource => "writing-policy:the-project-selects-one-source",
}
impl fmt::Display for RuleId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn slugs_are_well_formed_and_unique() {
let mut seen = std::collections::BTreeSet::new();
for rule in RuleId::ALL {
let id = rule.as_str();
let (domain, name) = id.split_once(':').unwrap();
let assert_slug = |part: &str| {
assert!(!part.is_empty(), "{id} has an empty half");
assert!(
part.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'),
"{id} is not a slug pair"
);
};
assert_slug(domain);
assert_slug(name);
assert!(seen.insert(id), "{id} is duplicated");
}
}
#[test]
fn display_renders_the_slug_pair() {
assert_eq!(
RuleId::ChapterStaysWithinLineCap.to_string(),
"docs-format:chapter-stays-within-200-lines"
);
}
}