-
Notifications
You must be signed in to change notification settings - Fork 0
feat(membership): refuse location as entity identity or language #153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "location_membership" | ||
| description = "Location is a time-varying market membership, not entity identity or language." | ||
| version.workspace = true | ||
| edition.workspace = true | ||
| rust-version.workspace = true | ||
| license.workspace = true | ||
| authors.workspace = true | ||
| repository.workspace = true | ||
| homepage.workspace = true | ||
| readme.workspace = true | ||
| keywords.workspace = true | ||
| categories.workspace = true | ||
| publish = false | ||
|
|
||
| [lints] | ||
| workspace = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| //! Fail-closed location-membership errors. | ||
|
|
||
| use std::fmt; | ||
|
|
||
| /// A fail-closed location-membership error. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| #[non_exhaustive] | ||
| pub enum LocationMembershipError { | ||
| /// Location membership was treated as permanent entity identity. | ||
| LocationIsNotEntityIdentity, | ||
| /// Location membership was treated as a language channel. | ||
| LocationIsNotLanguageChannel, | ||
| /// A recovery slice was empty or length-mismatched. | ||
| InvalidLocationPayload, | ||
| } | ||
|
|
||
| impl fmt::Display for LocationMembershipError { | ||
| fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| let message = match self { | ||
| Self::LocationIsNotEntityIdentity => { | ||
| "location membership is not permanent entity identity" | ||
| } | ||
| Self::LocationIsNotLanguageChannel => "location membership is not a language channel", | ||
| Self::InvalidLocationPayload => "invalid location-membership payload", | ||
| }; | ||
| formatter.write_str(message) | ||
| } | ||
| } | ||
|
|
||
| impl std::error::Error for LocationMembershipError {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::LocationMembershipError; | ||
|
|
||
| #[test] | ||
| fn error_messages_are_stable() { | ||
| for (error, message) in [ | ||
| ( | ||
| LocationMembershipError::LocationIsNotEntityIdentity, | ||
| "location membership is not permanent entity identity", | ||
| ), | ||
| ( | ||
| LocationMembershipError::LocationIsNotLanguageChannel, | ||
| "location membership is not a language channel", | ||
| ), | ||
| ( | ||
| LocationMembershipError::InvalidLocationPayload, | ||
| "invalid location-membership payload", | ||
| ), | ||
| ] { | ||
| assert_eq!(error.to_string(), message); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| //! Location membership versus entity identity and language. | ||
|
|
||
| use crate::LocationMembershipError; | ||
|
|
||
| /// Closed vocabulary of location-related membership treatments. | ||
| #[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
| pub enum LocationKind { | ||
| /// Time-varying market or place membership. | ||
| Location, | ||
| /// Permanent entity identity under role assignments. | ||
| EntityIdentity, | ||
| /// Language community or locale channel. | ||
| LanguageChannel, | ||
| } | ||
|
|
||
| impl LocationKind { | ||
| /// Return the stable wire kind name. | ||
| #[must_use] | ||
| pub const fn wire_name(self) -> &'static str { | ||
| match self { | ||
| Self::Location => "location", | ||
| Self::EntityIdentity => "entity_identity", | ||
| Self::LanguageChannel => "language_channel", | ||
| } | ||
| } | ||
|
|
||
| /// Parse a stable wire kind name. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`LocationMembershipError::InvalidLocationPayload`] for | ||
| /// unrecognized names. | ||
| pub fn from_wire_name(name: &str) -> Result<Self, LocationMembershipError> { | ||
| match name { | ||
| "location" => Ok(Self::Location), | ||
| "entity_identity" => Ok(Self::EntityIdentity), | ||
| "language_channel" => Ok(Self::LanguageChannel), | ||
| _ => Err(LocationMembershipError::InvalidLocationPayload), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat location membership as permanent entity identity. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`LocationMembershipError::LocationIsNotEntityIdentity`] when | ||
| /// `kind` is [`LocationKind::Location`]. | ||
| pub fn refuse_location_as_entity_identity( | ||
| kind: LocationKind, | ||
| ) -> Result<(), LocationMembershipError> { | ||
| match kind { | ||
| LocationKind::Location => Err(LocationMembershipError::LocationIsNotEntityIdentity), | ||
| LocationKind::EntityIdentity | LocationKind::LanguageChannel => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Refuse to treat location membership as a language channel. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`LocationMembershipError::LocationIsNotLanguageChannel`] when | ||
| /// `kind` is [`LocationKind::Location`]. | ||
| pub fn refuse_location_as_language_channel( | ||
| kind: LocationKind, | ||
| ) -> Result<(), LocationMembershipError> { | ||
| match kind { | ||
| LocationKind::Location => Err(LocationMembershipError::LocationIsNotLanguageChannel), | ||
| LocationKind::EntityIdentity | LocationKind::LanguageChannel => Ok(()), | ||
| } | ||
| } | ||
|
|
||
| /// Fraction of recovered location kinds that match known truth. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns [`LocationMembershipError::InvalidLocationPayload`] when either | ||
| /// slice is empty or the lengths differ. | ||
| pub fn identity_recovery_rate( | ||
| truth: &[LocationKind], | ||
| decided: &[LocationKind], | ||
| ) -> Result<f64, LocationMembershipError> { | ||
| if truth.is_empty() || truth.len() != decided.len() { | ||
| return Err(LocationMembershipError::InvalidLocationPayload); | ||
| } | ||
| let mut matches = 0_u32; | ||
| for (truth_kind, decided_kind) in truth.iter().zip(decided) { | ||
| if truth_kind == decided_kind { | ||
| matches += 1; | ||
| } | ||
| } | ||
| Ok(f64::from(matches) / truth.len() as f64) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{ | ||
| LocationKind, identity_recovery_rate, refuse_location_as_entity_identity, | ||
| refuse_location_as_language_channel, | ||
| }; | ||
| use crate::LocationMembershipError; | ||
|
|
||
| #[test] | ||
| fn local_branches_cover_kinds_payloads_and_wire_names() { | ||
| assert_eq!( | ||
| refuse_location_as_entity_identity(LocationKind::Location), | ||
| Err(LocationMembershipError::LocationIsNotEntityIdentity) | ||
| ); | ||
| assert_eq!( | ||
| refuse_location_as_language_channel(LocationKind::Location), | ||
| Err(LocationMembershipError::LocationIsNotLanguageChannel) | ||
| ); | ||
| refuse_location_as_entity_identity(LocationKind::EntityIdentity).expect("entity"); | ||
| refuse_location_as_entity_identity(LocationKind::LanguageChannel).expect("language"); | ||
| refuse_location_as_language_channel(LocationKind::EntityIdentity).expect("entity"); | ||
| refuse_location_as_language_channel(LocationKind::LanguageChannel).expect("language"); | ||
| for kind in [ | ||
| LocationKind::Location, | ||
| LocationKind::EntityIdentity, | ||
| LocationKind::LanguageChannel, | ||
| ] { | ||
| assert_eq!( | ||
| LocationKind::from_wire_name(kind.wire_name()).expect("round-trip"), | ||
| kind | ||
| ); | ||
| } | ||
| assert_eq!( | ||
| LocationKind::from_wire_name("project"), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| let matched = identity_recovery_rate(&[LocationKind::Location], &[LocationKind::Location]) | ||
| .expect("rate"); | ||
| assert!((matched - 1.0).abs() < f64::EPSILON); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[LocationKind::Location], &[]), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| #![forbid(unsafe_code)] | ||
| #![deny(missing_docs)] | ||
| #![allow(clippy::cast_precision_loss)] | ||
| //! Location is a time-varying market membership, not entity identity. | ||
| //! | ||
| //! Geographic and market assignments stay explicit multiple-membership | ||
| //! structure. They are not permanent entity classes and are not language | ||
| //! channels (ADR 0003). | ||
|
|
||
| mod error; | ||
| mod kind; | ||
|
|
||
| /// Fail-closed location-membership errors. | ||
| pub use error::LocationMembershipError; | ||
| /// Closed vocabulary of location-related membership treatments. | ||
| pub use kind::LocationKind; | ||
| /// Fraction of recovered location kinds that match known truth. | ||
| pub use kind::identity_recovery_rate; | ||
| /// Refuse to treat location membership as permanent entity identity. | ||
| pub use kind::refuse_location_as_entity_identity; | ||
| /// Refuse to treat location membership as a language channel. | ||
| pub use kind::refuse_location_as_language_channel; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| //! Integration contract for the `location_membership` package identity. | ||
|
|
||
| #[test] | ||
| fn package_identity_is_stable() { | ||
| let observed = std::hint::black_box(env!("CARGO_PKG_NAME")); | ||
| assert_eq!(observed, "location_membership"); | ||
| } |
67 changes: 67 additions & 0 deletions
67
crates/location_membership/tests/location_membership_contract.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| //! Location is not entity identity and not a language channel. | ||
|
|
||
| use location_membership::{ | ||
| LocationKind, LocationMembershipError, identity_recovery_rate, | ||
| refuse_location_as_entity_identity, refuse_location_as_language_channel, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn location_cannot_become_entity_identity_or_language() { | ||
| assert_eq!( | ||
| refuse_location_as_entity_identity(LocationKind::Location), | ||
| Err(LocationMembershipError::LocationIsNotEntityIdentity) | ||
| ); | ||
| assert_eq!( | ||
| refuse_location_as_language_channel(LocationKind::Location), | ||
| Err(LocationMembershipError::LocationIsNotLanguageChannel) | ||
| ); | ||
| refuse_location_as_entity_identity(LocationKind::EntityIdentity).expect("entity"); | ||
| refuse_location_as_language_channel(LocationKind::LanguageChannel).expect("language"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recovered_kinds_match_known_truth_better_than_an_entity_collapse() { | ||
| let truth = [ | ||
| LocationKind::Location, | ||
| LocationKind::EntityIdentity, | ||
| LocationKind::LanguageChannel, | ||
| ]; | ||
| let recovered = truth; | ||
| let collapsed = [ | ||
| LocationKind::EntityIdentity, | ||
| LocationKind::EntityIdentity, | ||
| LocationKind::EntityIdentity, | ||
| ]; | ||
| let recovered_rate = identity_recovery_rate(&truth, &recovered).expect("recovered"); | ||
| let collapsed_rate = identity_recovery_rate(&truth, &collapsed).expect("collapsed"); | ||
| let expected = { | ||
| let mut matches = 0_u32; | ||
| for (truth_kind, decided_kind) in truth.iter().zip(recovered.iter()) { | ||
| if truth_kind == decided_kind { | ||
| matches += 1; | ||
| } | ||
| } | ||
| f64::from(matches) / f64::from(u32::try_from(truth.len()).expect("len")) | ||
| }; | ||
| assert!((recovered_rate - expected).abs() < f64::EPSILON); | ||
| assert!(recovered_rate > collapsed_rate); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_or_mismatched_kind_payloads_fail_closed() { | ||
| assert_eq!( | ||
| identity_recovery_rate(&[], &[]), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate(&[LocationKind::Location], &[]), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| assert_eq!( | ||
| identity_recovery_rate( | ||
| &[LocationKind::Location, LocationKind::EntityIdentity], | ||
| &[LocationKind::Location] | ||
| ), | ||
| Err(LocationMembershipError::InvalidLocationPayload) | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Info: Loop false-branch coverage relies on integration test
The unit tests in kind.rs only call
identity_recovery_ratewith fully-matching inputs, so the false branch ofif truth_kind == decided_kind(kind.rs) is never exercised there. Under the workspace's 100% branch-coverage contract this branch is only covered because the integration testrecovered_kinds_match_known_truth_better_than_an_entity_collapsein location_membership_contract.rs feeds mismatched (collapsed) input. Sincecargo llvm-cov --workspaceaggregates integration tests, coverage holds — but the guarantee is cross-file, so removing/altering that integration test would silently break the branch-coverage gate.Was this helpful? React with 👍 or 👎 to provide feedback.