use serde::{Deserialize, Serialize};
#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, gen_platform::IsVariant,
)]
pub enum Semantic {
Keyword,
Symbol,
KeywordArg,
String,
Number,
Literal,
Comment,
Accent,
Muted,
Error,
Warning,
Info,
Hint,
Added,
Removed,
Unchanged,
}
impl Semantic {
pub const ALL: &'static [Self] = &[
Self::Keyword,
Self::Symbol,
Self::KeywordArg,
Self::String,
Self::Number,
Self::Literal,
Self::Comment,
Self::Accent,
Self::Muted,
Self::Error,
Self::Warning,
Self::Info,
Self::Hint,
Self::Added,
Self::Removed,
Self::Unchanged,
];
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Keyword => "keyword",
Self::Symbol => "symbol",
Self::KeywordArg => "keyword-arg",
Self::String => "string",
Self::Number => "number",
Self::Literal => "literal",
Self::Comment => "comment",
Self::Accent => "accent",
Self::Muted => "muted",
Self::Error => "error",
Self::Warning => "warning",
Self::Info => "info",
Self::Hint => "hint",
Self::Added => "added",
Self::Removed => "removed",
Self::Unchanged => "unchanged",
}
}
#[must_use]
pub fn from_wire(s: &str) -> Option<Self> {
match s {
"keyword" => Some(Self::Keyword),
"symbol" => Some(Self::Symbol),
"keyword-arg" => Some(Self::KeywordArg),
"string" => Some(Self::String),
"number" => Some(Self::Number),
"literal" => Some(Self::Literal),
"comment" => Some(Self::Comment),
"accent" => Some(Self::Accent),
"muted" => Some(Self::Muted),
"error" => Some(Self::Error),
"warning" => Some(Self::Warning),
"info" => Some(Self::Info),
"hint" => Some(Self::Hint),
"added" => Some(Self::Added),
"removed" => Some(Self::Removed),
"unchanged" => Some(Self::Unchanged),
_ => None,
}
}
}
impl std::fmt::Display for Semantic {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl AsRef<str> for Semantic {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl TryFrom<&str> for Semantic {
type Error = ();
fn try_from(s: &str) -> Result<Self, <Self as TryFrom<&str>>::Error> {
Self::from_wire(s).ok_or(())
}
}
impl From<Semantic> for &'static str {
fn from(sem: Semantic) -> &'static str {
sem.as_str()
}
}
impl From<&Semantic> for &'static str {
fn from(sem: &Semantic) -> &'static str {
sem.as_str()
}
}
impl From<Semantic> for String {
fn from(sem: Semantic) -> String {
sem.as_str().to_owned()
}
}
impl From<&Semantic> for String {
fn from(sem: &Semantic) -> String {
sem.as_str().to_owned()
}
}
impl From<Semantic> for std::borrow::Cow<'static, str> {
fn from(sem: Semantic) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(sem.as_str())
}
}
impl From<&Semantic> for std::borrow::Cow<'static, str> {
fn from(sem: &Semantic) -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(sem.as_str())
}
}
impl From<Semantic> for Box<str> {
fn from(sem: Semantic) -> Box<str> {
Box::<str>::from(sem.as_str())
}
}
impl From<&Semantic> for Box<str> {
fn from(sem: &Semantic) -> Box<str> {
Box::<str>::from(sem.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn semantic_all_enumerates_every_variant_in_declaration_order() {
assert_eq!(
Semantic::ALL,
&[
Semantic::Keyword,
Semantic::Symbol,
Semantic::KeywordArg,
Semantic::String,
Semantic::Number,
Semantic::Literal,
Semantic::Comment,
Semantic::Accent,
Semantic::Muted,
Semantic::Error,
Semantic::Warning,
Semantic::Info,
Semantic::Hint,
Semantic::Added,
Semantic::Removed,
Semantic::Unchanged,
],
);
for variant in Semantic::ALL {
let row = [
variant.is_keyword(),
variant.is_symbol(),
variant.is_keyword_arg(),
variant.is_string(),
variant.is_number(),
variant.is_literal(),
variant.is_comment(),
variant.is_accent(),
variant.is_muted(),
variant.is_error(),
variant.is_warning(),
variant.is_info(),
variant.is_hint(),
variant.is_added(),
variant.is_removed(),
variant.is_unchanged(),
];
let hits = row.iter().filter(|b| **b).count();
assert_eq!(
hits, 1,
"Semantic::{variant:?} must satisfy exactly one of the \
15 is_* arm-discriminator predicates; got {row:?}",
);
}
}
#[test]
fn semantic_is_variant_predicates_partition_the_arm_set() {
for (idx, variant) in Semantic::ALL.iter().enumerate() {
let observed: [bool; 16] = [
variant.is_keyword(),
variant.is_symbol(),
variant.is_keyword_arg(),
variant.is_string(),
variant.is_number(),
variant.is_literal(),
variant.is_comment(),
variant.is_accent(),
variant.is_muted(),
variant.is_error(),
variant.is_warning(),
variant.is_info(),
variant.is_hint(),
variant.is_added(),
variant.is_removed(),
variant.is_unchanged(),
];
let mut expected = [false; 16];
expected[idx] = true;
assert_eq!(
observed, expected,
"Semantic::{variant:?} at ALL[{idx}] is_* predicates \
must fire only on their own arm lane (identity \
diagonal); got {observed:?}",
);
}
}
#[test]
fn semantic_as_str_returns_canonical_kebab_case_per_arm() {
assert_eq!(Semantic::Keyword.as_str(), "keyword");
assert_eq!(Semantic::Symbol.as_str(), "symbol");
assert_eq!(Semantic::KeywordArg.as_str(), "keyword-arg");
assert_eq!(Semantic::String.as_str(), "string");
assert_eq!(Semantic::Number.as_str(), "number");
assert_eq!(Semantic::Literal.as_str(), "literal");
assert_eq!(Semantic::Comment.as_str(), "comment");
assert_eq!(Semantic::Accent.as_str(), "accent");
assert_eq!(Semantic::Muted.as_str(), "muted");
assert_eq!(Semantic::Error.as_str(), "error");
assert_eq!(Semantic::Warning.as_str(), "warning");
assert_eq!(Semantic::Info.as_str(), "info");
assert_eq!(Semantic::Hint.as_str(), "hint");
assert_eq!(Semantic::Added.as_str(), "added");
assert_eq!(Semantic::Removed.as_str(), "removed");
assert_eq!(Semantic::Unchanged.as_str(), "unchanged");
}
#[test]
fn semantic_as_str_projections_are_all_distinct_across_arms() {
let mut projections: Vec<&'static str> = Semantic::ALL.iter().map(|s| s.as_str()).collect();
let before = projections.len();
projections.sort_unstable();
projections.dedup();
assert_eq!(
projections.len(),
before,
"Semantic::as_str must be injective across ALL — collisions: \
{projections:?}",
);
}
#[test]
fn semantic_display_and_as_ref_str_route_through_as_str_accessor() {
for &sem in Semantic::ALL {
let via_as_str: &str = sem.as_str();
let via_display: String = format!("{sem}");
let via_as_ref: &str = <Semantic as AsRef<str>>::as_ref(&sem);
assert_eq!(
via_display, via_as_str,
"Semantic::{sem:?} — Display routes off `as_str`; got \
Display={via_display:?} vs as_str={via_as_str:?}",
);
assert_eq!(
via_as_ref, via_as_str,
"Semantic::{sem:?} — AsRef<str> routes off `as_str`; got \
AsRef={via_as_ref:?} vs as_str={via_as_str:?}",
);
}
}
#[test]
fn semantic_as_str_is_usable_in_const_context() {
const KEYWORD: &str = Semantic::Keyword.as_str();
const ERROR: &str = Semantic::Error.as_str();
const UNCHANGED: &str = Semantic::Unchanged.as_str();
const { assert!(KEYWORD.as_bytes()[0] == b'k') };
const { assert!(ERROR.as_bytes()[0] == b'e') };
const { assert!(UNCHANGED.as_bytes()[0] == b'u') };
}
#[test]
fn semantic_from_wire_accepts_every_as_str_output() {
for &variant in Semantic::ALL {
let wire = variant.as_str();
let parsed = Semantic::from_wire(wire).unwrap_or_else(|| {
panic!(
"Semantic::from_wire({wire:?}) must accept every \
Semantic::as_str output — got None for the wire \
byte-string of {variant:?}"
)
});
assert_eq!(
parsed, variant,
"Semantic::from_wire(Semantic::{variant:?}.as_str()) \
must return Semantic::{variant:?} — the (as_str, \
from_wire) pair must form a total round-trip on the \
closed 16-arm Semantic arm-set",
);
}
}
#[test]
fn semantic_from_wire_rejects_unknown_byte_strings() {
for bad in [
"",
" ",
"Keyword",
"KEYWORD",
"Symbol",
"SYMBOL",
"KeywordArg",
"keyword_arg",
"keywordarg",
"String",
"STRING",
"Number",
"Literal",
"Comment",
"Accent",
"Muted",
"Error",
"ERROR",
"Warning",
"WARNING",
"Info",
"INFO",
"Hint",
"HINT",
"Added",
"ADDED",
"Removed",
"REMOVED",
"Unchanged",
"UNCHANGED",
"kewyord",
"sym",
"kw",
"str",
"num",
"lit",
"cmt",
"safe",
"unsafe",
"safety",
"compliance",
"proven",
"rejected",
"namespace",
"deleted",
"highlight",
"identifier",
"keyword ",
" keyword",
"keyword\n",
"keyword\t",
"keyword-arg ",
" keyword-arg",
"added ",
" added",
"unchanged ",
" unchanged",
] {
assert!(
Semantic::from_wire(bad).is_none(),
"Semantic::from_wire({bad:?}) must return None — the \
parser's accept-set is exactly the 16 Semantic::as_str \
outputs; a widening would silently split the parser's \
accept-set from the emitter's arm-set",
);
}
}
#[test]
fn semantic_is_variant_predicates_are_const_fn() {
const { assert!(Semantic::Keyword.is_keyword()) };
const { assert!(Semantic::Error.is_error()) };
const { assert!(Semantic::Added.is_added()) };
const { assert!(Semantic::Unchanged.is_unchanged()) };
}
#[test]
fn semantic_try_from_str_routes_through_from_wire_accessor() {
for &variant in Semantic::ALL {
let wire = variant.as_str();
assert_eq!(
<Semantic as TryFrom<&str>>::try_from(wire),
Ok(variant),
"TryFrom<&str> impl on Semantic must round-trip \
Semantic::{variant:?}.as_str() = {wire:?} back to \
Ok(Semantic::{variant:?}) — divergence from \
Semantic::from_wire signals a silent detour off the \
substrate-primitive accessor",
);
assert_eq!(
<Semantic as TryFrom<&str>>::try_from(wire).ok(),
Semantic::from_wire(wire),
"TryFrom<&str> ok()-projection on {wire:?} must \
byte-equal Semantic::from_wire on the same input",
);
}
}
#[test]
fn semantic_try_from_str_rejects_unknown_byte_strings() {
for bad in [
"",
" ",
"Keyword",
"KEYWORD",
"Symbol",
"KeywordArg",
"keywordarg",
"keyword_arg",
"keyword.arg",
"String",
"Number",
"Literal",
"Comment",
"Accent",
"Muted",
"Error",
"ERROR",
"Warning",
"Info",
"Hint",
"Added",
"Removed",
"Unchanged",
"kwd",
"sym",
"kw",
"str",
"num",
"lit",
"cmt",
"safe",
"unsafe",
"safety",
"compliance",
"proven",
"rejected",
"namespace",
"deleted",
"highlight",
"identifier",
"keyword ",
" keyword",
"keyword\n",
"keyword\t",
"keyword-arg ",
" keyword-arg",
"added ",
" added",
"unchanged ",
" unchanged",
] {
assert_eq!(
<Semantic as TryFrom<&str>>::try_from(bad),
Err(()),
"TryFrom<&str> for Semantic({bad:?}) must return \
Err(()) — the trait impl's accept-set is exactly the \
16 Semantic::as_str outputs; a widening would \
silently split the trait impl's accept-set from the \
emitter's arm-set",
);
}
}
#[test]
fn semantic_try_from_str_and_from_wire_partition_the_accept_set() {
for &variant in Semantic::ALL {
let wire = variant.as_str();
assert_eq!(
<Semantic as TryFrom<&str>>::try_from(wire).ok(),
Semantic::from_wire(wire),
"TryFrom<&str>::ok() and from_wire must agree on \
Semantic::{variant:?}.as_str() = {wire:?}",
);
}
for bad in [
"",
"Keyword",
"unknown",
"safety",
"safe",
"proven",
"namespace",
"keywordarg",
] {
assert_eq!(
<Semantic as TryFrom<&str>>::try_from(bad).ok(),
Semantic::from_wire(bad),
"TryFrom<&str>::ok() and from_wire must agree on the \
rejection outcome for {bad:?}",
);
}
}
#[test]
fn semantic_from_into_static_str_routes_through_as_str_accessor() {
const KEYWORD: &str = Semantic::Keyword.as_str();
const KEYWORD_ARG: &str = Semantic::KeywordArg.as_str();
const UNCHANGED: &str = Semantic::Unchanged.as_str();
for &variant in Semantic::ALL {
let via_trait: &'static str = <&'static str as From<Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait, via_method,
"From<Semantic> for &'static str impl must round-trip \
Semantic::{variant:?} to the same canonical-lowercase \
kebab byte-string Semantic::as_str returns — divergence \
signals a silent detour off the substrate-primitive \
accessor"
);
let via_into: &'static str = variant.into();
assert_eq!(
via_into, via_method,
"Into<&'static str>::into on Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — the \
blanket-derived Into shape must resolve to the same \
as_str dispatch as the explicit From impl"
);
}
assert_eq!(
[KEYWORD, KEYWORD_ARG, UNCHANGED],
["keyword", "keyword-arg", "unchanged"],
"const-context Semantic::as_str must resolve to the \
canonical-lowercase kebab byte-strings — a future \
accidental downgrade of any arm to a non-const or non-\
static byte-string breaks the `&'static str`-lifetime \
promise the paired From<Semantic> for &'static str impl \
carries by construction"
);
}
#[test]
fn semantic_from_into_static_str_and_as_str_partition_the_emit_set() {
for &variant in Semantic::ALL {
let via_trait: &'static str = <&'static str as From<Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait, via_method,
"From<Semantic> for &'static str and Semantic::as_str \
must resolve identically on Semantic::{variant:?} — \
divergence signals the two forward-projection paths \
have drifted onto different emit-sets"
);
}
for &variant in Semantic::ALL {
let emitted: &'static str = <&'static str as From<Semantic>>::from(variant);
let reparsed = <Semantic as TryFrom<&str>>::try_from(emitted).unwrap_or_else(|()| {
panic!(
"TryFrom<&str> for Semantic must accept every \
From<Semantic> for &'static str output — got \
Err(()) for Semantic::{variant:?}'s emit \
byte-string {emitted:?}"
)
});
assert_eq!(
reparsed, variant,
"trait-idiomatic Semantic ↔ &'static str round-trip \
must be the identity on Semantic::{variant:?} — the \
From<Self> for &'static str + TryFrom<&str> for Self \
pair must compose to the identity on the closed 16-arm \
accept-set"
);
}
}
#[test]
fn semantic_from_borrowed_into_static_str_routes_through_as_str_accessor() {
const KEYWORD: &str = Semantic::Keyword.as_str();
const KEYWORD_ARG: &str = Semantic::KeywordArg.as_str();
const UNCHANGED: &str = Semantic::Unchanged.as_str();
for variant in Semantic::ALL {
let via_trait: &'static str = <&'static str as From<&Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait, via_method,
"From<&Semantic> for &'static str impl must round-trip \
&Semantic::{variant:?} to the same canonical-lowercase \
kebab byte-string Semantic::as_str returns — divergence \
signals a silent detour off the substrate-primitive \
accessor"
);
let via_into: &'static str = variant.into();
assert_eq!(
via_into, via_method,
"Into<&'static str>::into on &Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — the \
blanket-derived Into shape on the borrowed-input axis \
must resolve to the same as_str dispatch as the \
explicit From impl"
);
}
assert_eq!(
[KEYWORD, KEYWORD_ARG, UNCHANGED],
["keyword", "keyword-arg", "unchanged"],
"const-context Semantic::as_str must resolve to the \
canonical-lowercase kebab byte-strings — a future \
accidental downgrade of any arm to a non-const or non-\
static byte-string breaks the `&'static str`-lifetime \
promise the paired From<&Semantic> for &'static str impl \
carries by construction"
);
}
#[test]
fn semantic_from_owned_and_borrowed_into_static_str_agree_on_every_arm() {
for &variant in Semantic::ALL {
let via_owned: &'static str = <&'static str as From<Semantic>>::from(variant);
let via_borrowed: &'static str = <&'static str as From<&Semantic>>::from(&variant);
assert_eq!(
via_owned, via_borrowed,
"From<Semantic> for &'static str and From<&Semantic> \
for &'static str must agree on Semantic::{variant:?} \
— divergence signals the owned-input and borrowed-\
input forward-projection paths have drifted onto \
different emit-sets"
);
}
let via_pipe: Vec<&'static str> = Semantic::ALL.iter().map(Into::into).collect();
let via_method: Vec<&'static str> = Semantic::ALL.iter().map(|s| s.as_str()).collect();
assert_eq!(
via_pipe, via_method,
"Semantic::ALL.iter().map(Into::into) must resolve to the \
same per-arm canonical-lowercase kebab byte-string \
sequence Semantic::as_str returns across every arm — the \
iterator yields &Semantic, so this pipe fires through the \
borrowed-input From<&Semantic> for &'static str axis and \
witnesses the newly lifted impl's arm-set matches the \
substrate-primitive accessor without a spurious Copy deref"
);
}
#[test]
fn semantic_from_into_owned_string_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: String = <String as From<Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_str(),
via_method,
"From<Semantic> for String impl must round-trip \
Semantic::{variant:?} to the same canonical-lowercase \
kebab byte-string Semantic::as_str returns — \
divergence signals a silent detour off the substrate-\
primitive accessor"
);
let via_into: String = variant.into();
assert_eq!(
via_into.as_str(),
via_method,
"Into<String>::into on Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — the \
blanket-derived Into shape must resolve to the same \
as_str dispatch as the explicit From impl"
);
}
}
#[test]
fn semantic_from_into_owned_string_and_static_str_agree_on_every_arm() {
for &variant in Semantic::ALL {
let owned_string: String = <String as From<Semantic>>::from(variant);
let owned_static: &'static str = <&'static str as From<Semantic>>::from(variant);
assert_eq!(
owned_string.as_str(),
owned_static,
"From<Semantic> for String and From<Semantic> for \
&'static str must resolve identically on \
Semantic::{variant:?} — divergence signals the two \
output-shape forward-projection paths have drifted \
onto different emit-sets"
);
let via_display: String = variant.to_string();
assert_eq!(
owned_string, via_display,
"From<Semantic> for String and ToString::to_string \
via Display must resolve identically on \
Semantic::{variant:?} — divergence signals the \
trait-idiomatic owned-`String` axis and the Display-\
routed ToString axis have drifted onto different \
vocabularies"
);
}
let via_iter: Vec<String> = Semantic::ALL.iter().copied().map(String::from).collect();
let via_method: Vec<String> = Semantic::ALL
.iter()
.map(|s| s.as_str().to_owned())
.collect();
assert_eq!(
via_iter, via_method,
"`.iter().copied().map(String::from)` over Semantic::ALL \
must byte-equal `.iter().map(|s| s.as_str().to_owned())` \
on every arm — the owned-`String` `From<Semantic> for \
String` axis is what makes the `.map(String::from)` \
shape route through the substrate-primitive \
Semantic::as_str accessor rather than through a per-\
call-site `.to_owned()` / `String::from(sem.as_str())` \
detour"
);
for &variant in Semantic::ALL {
let emitted: String = variant.into();
let re_parsed: Result<Semantic, ()> =
<Semantic as TryFrom<&str>>::try_from(emitted.as_str());
assert_eq!(
re_parsed,
Ok(variant),
"trait-idiomatic owned-`String` axis pair must round-\
trip Semantic::{variant:?} through \
`.into::<String>()` and back through `TryFrom<&str>` \
on the owned-`String`'s `String::as_str` borrow — a \
break signals the forward-emit owned-`String` axis \
and the reverse-parse `TryFrom<&str>` axis have \
drifted onto different vocabularies"
);
}
}
#[test]
fn semantic_from_borrowed_into_owned_string_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: String = <String as From<&Semantic>>::from(&variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_str(),
via_method,
"From<&Semantic> for String impl must round-trip \
&Semantic::{variant:?} to the same canonical-\
lowercase kebab byte-string Semantic::as_str \
returns — divergence signals a silent detour off \
the substrate-primitive accessor"
);
let via_into: String = (&variant).into();
assert_eq!(
via_into.as_str(),
via_method,
"Into<String>::into on &Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — \
the blanket-derived Into shape must resolve to the \
same as_str dispatch as the explicit From impl"
);
}
}
#[test]
fn semantic_from_borrowed_into_owned_string_agrees_with_paired_axes_on_every_arm() {
for &variant in Semantic::ALL {
let borrowed_string: String = <String as From<&Semantic>>::from(&variant);
let owned_string: String = <String as From<Semantic>>::from(variant);
let borrowed_static: &'static str = <&'static str as From<&Semantic>>::from(&variant);
let owned_static: &'static str = <&'static str as From<Semantic>>::from(variant);
assert_eq!(
borrowed_string, owned_string,
"From<&Semantic> for String and From<Semantic> for \
String must resolve identically on \
Semantic::{variant:?} — divergence signals the \
owned-`String` axis pair's borrowed-input and \
owned-input arms have drifted onto different emit-\
sets"
);
assert_eq!(
borrowed_string.as_str(),
borrowed_static,
"From<&Semantic> for String and From<&Semantic> for \
&'static str must resolve identically on \
Semantic::{variant:?} — divergence signals the \
borrowed-input axis pair's owned-`String`-returning \
and `&'static str`-returning arms have drifted \
onto different emit-sets"
);
assert_eq!(
borrowed_string.as_str(),
owned_static,
"From<&Semantic> for String and From<Semantic> for \
&'static str must resolve identically on \
Semantic::{variant:?} — the cross-diagonal corner \
of the 2×2 must agree, or the four projections \
have split into two vocabularies"
);
let via_display: String = variant.to_string();
assert_eq!(
borrowed_string, via_display,
"From<&Semantic> for String and ToString::to_string \
via Display must resolve identically on \
Semantic::{variant:?} — divergence signals the \
trait-idiomatic borrowed-input owned-`String` axis \
and the Display-routed ToString axis have drifted \
onto different vocabularies"
);
}
let via_iter: Vec<String> = Semantic::ALL.iter().map(String::from).collect();
let via_method: Vec<String> = Semantic::ALL
.iter()
.map(|sem| sem.as_str().to_owned())
.collect();
assert_eq!(
via_iter, via_method,
"`.iter().map(String::from)` over Semantic::ALL must \
byte-equal `.iter().map(|sem| sem.as_str().to_owned())` \
on every arm — the borrowed-input owned-`String` \
`From<&Semantic> for String` axis is what makes the \
`.map(String::from)` shape route through the substrate-\
primitive Semantic::as_str accessor without a spurious \
`.copied()` / `Copy` deref"
);
for &variant in Semantic::ALL {
let emitted: String = (&variant).into();
let re_parsed: Result<Semantic, ()> =
<Semantic as TryFrom<&str>>::try_from(emitted.as_str());
assert_eq!(
re_parsed,
Ok(variant),
"trait-idiomatic borrowed-input owned-`String` axis \
pair must round-trip Semantic::{variant:?} through \
`(&variant).into::<String>()` and back through \
`TryFrom<&str>` on the owned-`String`'s \
`String::as_str` borrow — a break signals the \
borrowed-input forward-emit owned-`String` axis \
and the reverse-parse `TryFrom<&str>` axis have \
drifted onto different vocabularies"
);
}
}
#[test]
fn semantic_from_into_static_cow_str_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_ref(),
via_method,
"From<Semantic> for Cow<'static, str> impl must round-\
trip Semantic::{variant:?} to the same canonical-\
lowercase kebab byte-string Semantic::as_str returns \
— divergence signals a silent detour off the \
substrate-primitive accessor"
);
assert!(
matches!(via_trait, std::borrow::Cow::Borrowed(_)),
"From<Semantic> for Cow<'static, str> impl must land \
on the zero-alloc Cow::Borrowed arm on \
Semantic::{variant:?} — a Cow::Owned outcome \
signals the projection has silently allocated where \
the substrate-primitive Semantic::as_str \
`&'static str` return makes the borrowed arm the \
type-correct projection"
);
let via_into: std::borrow::Cow<'static, str> = variant.into();
assert_eq!(
via_into.as_ref(),
via_method,
"Into<Cow<'static, str>>::into on \
Semantic::{variant:?} must byte-equal \
Semantic::as_str on the same input — the blanket-\
derived Into shape must resolve to the same as_str \
dispatch as the explicit From impl"
);
assert!(
matches!(via_into, std::borrow::Cow::Borrowed(_)),
"Into<Cow<'static, str>>::into on \
Semantic::{variant:?} must land on the zero-alloc \
Cow::Borrowed arm — the blanket-derived Into shape \
must resolve to the same Cow::Borrowed dispatch as \
the explicit From impl"
);
}
}
#[test]
fn semantic_from_into_static_cow_str_agrees_with_paired_axes_on_every_arm() {
for &variant in Semantic::ALL {
let via_cow: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<Semantic>>::from(variant);
let via_static: &'static str = <&'static str as From<Semantic>>::from(variant);
let via_string: String = <String as From<Semantic>>::from(variant);
assert_eq!(
via_cow.as_ref(),
via_static,
"From<Semantic> for Cow<'static, str> and \
From<Semantic> for &'static str must resolve \
identically on Semantic::{variant:?} — divergence \
signals the Cow<'static, str> and &'static str \
return-shape paths have drifted onto different \
emit-sets"
);
assert_eq!(
via_cow.as_ref(),
via_string.as_str(),
"From<Semantic> for Cow<'static, str> and \
From<Semantic> for String must resolve identically \
on Semantic::{variant:?} — divergence signals the \
Cow<'static, str> and String return-shape paths \
have drifted onto different emit-sets"
);
let via_to_string: String = variant.to_string();
assert_eq!(
via_cow.as_ref(),
via_to_string.as_str(),
"From<Semantic> for Cow<'static, str> must byte-\
equal Semantic::to_string on Semantic::{variant:?} \
— divergence signals the trait-idiomatic \
Cow<'static, str> forward-projection axis and the \
ToString-through-Display axis have drifted onto \
different emit-sets"
);
}
let via_iter: Vec<std::borrow::Cow<'static, str>> = Semantic::ALL
.iter()
.copied()
.map(std::borrow::Cow::from)
.collect();
let via_method: Vec<std::borrow::Cow<'static, str>> = Semantic::ALL
.iter()
.map(|sem| std::borrow::Cow::Borrowed(sem.as_str()))
.collect();
assert_eq!(
via_iter, via_method,
"`.iter().copied().map(Cow::from)` over Semantic::ALL \
must byte-equal `.iter().map(|sem| \
Cow::Borrowed(sem.as_str()))` on every arm — the trait-\
idiomatic `From<Semantic> for Cow<'static, str>` axis \
is what makes the `Cow::from` composition route through \
the substrate-primitive Semantic::as_str accessor \
rather than a per-call-site open-code"
);
for cow in &via_iter {
assert!(
matches!(cow, std::borrow::Cow::Borrowed(_)),
"`.iter().copied().map(Cow::from)` over \
Semantic::ALL must land on the zero-alloc \
Cow::Borrowed arm on every element — a Cow::Owned \
outcome signals the pipe has silently allocated \
where the substrate-primitive Semantic::as_str \
`&'static str` return makes the borrowed arm the \
type-correct projection"
);
}
for &variant in Semantic::ALL {
let via_cow: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<Semantic>>::from(variant);
let re_parsed: Result<Semantic, ()> =
<Semantic as TryFrom<&str>>::try_from(via_cow.as_ref());
assert_eq!(
re_parsed,
Ok(variant),
"trait-idiomatic Cow<'static, str> forward-projection \
+ reverse-projection axis pair must round-trip \
Semantic::{variant:?} through \
`.into::<Cow<'static, str>>()` on the owned-input \
surface and back through `TryFrom<&str>` on the \
projection's Cow::as_ref borrow — a break signals \
the Cow<'static, str> forward-emit and reverse-\
parse axes have drifted onto different vocabularies \
(like the sibling FixSafety and Severity pairs, \
Semantic's forward emit and reverse parse share \
the same sixteen inline canonical-lowercase kebab \
byte-strings by construction, so the round-trip \
composes directly)"
);
}
}
#[test]
fn semantic_from_borrowed_into_static_cow_str_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<&Semantic>>::from(&variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_ref(),
via_method,
"From<&Semantic> for Cow<'static, str> impl must \
round-trip &Semantic::{variant:?} to the same \
canonical-lowercase kebab byte-string \
Semantic::as_str returns — divergence signals a \
silent detour off the substrate-primitive accessor"
);
assert!(
matches!(via_trait, std::borrow::Cow::Borrowed(_)),
"From<&Semantic> for Cow<'static, str> impl must \
land on the zero-alloc Cow::Borrowed arm on \
&Semantic::{variant:?} — a Cow::Owned outcome \
signals the projection has silently allocated where \
the substrate-primitive Semantic::as_str \
`&'static str` return makes the borrowed arm the \
type-correct projection"
);
let via_into: std::borrow::Cow<'static, str> = (&variant).into();
assert_eq!(
via_into.as_ref(),
via_method,
"Into<Cow<'static, str>>::into on \
&Semantic::{variant:?} must byte-equal \
Semantic::as_str on the same input — the blanket-\
derived Into shape on the borrowed-input surface \
must resolve to the same as_str dispatch as the \
explicit From impl"
);
assert!(
matches!(via_into, std::borrow::Cow::Borrowed(_)),
"Into<Cow<'static, str>>::into on \
&Semantic::{variant:?} must land on the zero-alloc \
Cow::Borrowed arm — the blanket-derived Into shape \
on the borrowed-input surface must resolve to the \
same Cow::Borrowed dispatch as the explicit From \
impl"
);
}
}
#[test]
#[allow(
clippy::too_many_lines,
reason = "cross-axis partition pin folds four return-shape paths \
(borrowed-input Cow<'static, str>, owned-input Cow<'static, str>, \
borrowed-input &'static str, borrowed-input String) plus the \
ToString-through-Display witness plus a `.iter().map(Cow::from)` \
pipe witness with zero-alloc discriminator plus a direct \
round-trip witness through TryFrom<&str> over sixteen typed \
variants; the linear per-axis repetition is exactly what the \
fold is pinning — a helper would hide the shape it locks"
)]
fn semantic_from_borrowed_into_static_cow_str_agrees_with_paired_axes_on_every_arm() {
for &variant in Semantic::ALL {
let via_borrowed_cow: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<&Semantic>>::from(&variant);
let via_owned_cow: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<Semantic>>::from(variant);
let via_borrowed_static: &'static str =
<&'static str as From<&Semantic>>::from(&variant);
let via_borrowed_string: String = <String as From<&Semantic>>::from(&variant);
assert_eq!(
via_borrowed_cow.as_ref(),
via_owned_cow.as_ref(),
"From<&Semantic> for Cow<'static, str> and \
From<Semantic> for Cow<'static, str> must resolve \
identically on Semantic::{variant:?} — divergence \
signals the borrowed-input and owned-input \
Cow<'static, str> forward-projection input-shape \
paths have drifted onto different emit-sets"
);
assert_eq!(
via_borrowed_cow.as_ref(),
via_borrowed_static,
"From<&Semantic> for Cow<'static, str> and \
From<&Semantic> for &'static str must resolve \
identically on Semantic::{variant:?} — divergence \
signals the borrowed-input Cow<'static, str> and \
borrowed-input `&'static str` return-shape paths \
have drifted onto different emit-sets"
);
assert_eq!(
via_borrowed_cow.as_ref(),
via_borrowed_string.as_str(),
"From<&Semantic> for Cow<'static, str> and \
From<&Semantic> for String must resolve identically \
on Semantic::{variant:?} — divergence signals the \
borrowed-input Cow<'static, str> and borrowed-input \
owned-`String` return-shape paths have drifted onto \
different emit-sets"
);
let via_to_string: String = variant.to_string();
assert_eq!(
via_borrowed_cow.as_ref(),
via_to_string.as_str(),
"From<&Semantic> for Cow<'static, str> must byte-\
equal Semantic::to_string on Semantic::{variant:?} \
— divergence signals the trait-idiomatic borrowed-\
input Cow<'static, str> forward-projection axis and \
the ToString-through-Display axis have drifted onto \
different emit-sets"
);
assert!(
matches!(via_borrowed_cow, std::borrow::Cow::Borrowed(_)),
"From<&Semantic> for Cow<'static, str> must land on \
the zero-alloc Cow::Borrowed arm on \
&Semantic::{variant:?} — a Cow::Owned outcome \
signals the borrowed-input surface has silently \
allocated where the substrate-primitive \
Semantic::as_str `&'static str` return makes the \
borrowed arm the type-correct projection"
);
}
let via_iter: Vec<std::borrow::Cow<'static, str>> =
Semantic::ALL.iter().map(std::borrow::Cow::from).collect();
let via_method: Vec<std::borrow::Cow<'static, str>> = Semantic::ALL
.iter()
.map(|sem| std::borrow::Cow::Borrowed(sem.as_str()))
.collect();
assert_eq!(
via_iter, via_method,
"`.iter().map(Cow::from)` over Semantic::ALL — a call \
site whose iteration axis holds `&Semantic` by \
construction — must byte-equal `.iter().map(|sem| \
Cow::Borrowed(sem.as_str()))` on every arm — the \
borrowed-input `From<&Semantic> for Cow<'static, str>` \
axis is what makes the `Cow::from` composition route \
through the substrate-primitive `Semantic::as_str` \
accessor without a spurious `Copy` deref (which would \
only be reachable through the owned-input \
`From<Semantic> for Cow<'static, str>` axis by first \
calling `.copied()` on the iterator)"
);
for cow in &via_iter {
assert!(
matches!(cow, std::borrow::Cow::Borrowed(_)),
"`.iter().map(Cow::from)` over Semantic::ALL must \
land on the zero-alloc Cow::Borrowed arm on every \
element — a Cow::Owned outcome signals the pipe has \
silently allocated through the borrowed-input axis \
where the substrate-primitive Semantic::as_str \
`&'static str` return makes the borrowed arm the \
type-correct projection"
);
}
for &variant in Semantic::ALL {
let via_cow: std::borrow::Cow<'static, str> =
<std::borrow::Cow<'static, str> as From<&Semantic>>::from(&variant);
let re_parsed: Result<Semantic, ()> =
<Semantic as TryFrom<&str>>::try_from(via_cow.as_ref());
assert_eq!(
re_parsed,
Ok(variant),
"trait-idiomatic borrowed-input Cow<'static, str> \
forward-projection + reverse-projection axis pair \
must round-trip &Semantic::{variant:?} through \
`(&variant).into::<Cow<'static, str>>()` on the \
borrowed-input surface and back through \
`TryFrom<&str>` on the projection's Cow::as_ref \
borrow — a break signals the borrowed-input \
Cow<'static, str> forward-emit and reverse-parse \
axes have drifted onto different vocabularies \
(unlike the peer CaixaKind axis pair, Semantic's \
forward emit and reverse parse share the same \
sixteen inline canonical-lowercase kebab byte-\
strings by construction, so the round-trip composes \
directly)"
);
}
}
#[test]
fn semantic_from_into_box_str_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: Box<str> = <Box<str> as From<Semantic>>::from(variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_ref(),
via_method,
"From<Semantic> for Box<str> impl must round-trip \
Semantic::{variant:?} to the same canonical-lowercase \
kebab byte-string Semantic::as_str returns — \
divergence signals a silent detour off the substrate-\
primitive accessor"
);
let via_into: Box<str> = variant.into();
assert_eq!(
via_into.as_ref(),
via_method,
"Into<Box<str>>::into on Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — the \
blanket-derived Into shape must resolve to the same \
as_str dispatch as the explicit From impl"
);
}
}
#[test]
fn semantic_from_borrowed_into_box_str_routes_through_as_str_accessor() {
for &variant in Semantic::ALL {
let via_trait: Box<str> = <Box<str> as From<&Semantic>>::from(&variant);
let via_method: &'static str = variant.as_str();
assert_eq!(
via_trait.as_ref(),
via_method,
"From<&Semantic> for Box<str> impl must round-trip \
&Semantic::{variant:?} to the same canonical-\
lowercase kebab byte-string Semantic::as_str returns \
— divergence signals a silent detour off the \
substrate-primitive accessor"
);
let via_into: Box<str> = (&variant).into();
assert_eq!(
via_into.as_ref(),
via_method,
"Into<Box<str>>::into on &Semantic::{variant:?} must \
byte-equal Semantic::as_str on the same input — the \
blanket-derived Into shape on the borrowed-input \
surface must resolve to the same as_str dispatch as \
the explicit From impl"
);
}
let via_pipe: Vec<Box<str>> = Semantic::ALL.iter().map(Box::<str>::from).collect();
let via_accessor: Vec<&'static str> = Semantic::ALL.iter().map(|s| s.as_str()).collect();
assert_eq!(
via_pipe.len(),
via_accessor.len(),
"Semantic::ALL.iter().map(Box::<str>::from) pipe must \
preserve arity against the paired Semantic::as_str \
accessor — a length divergence signals the borrowed-input \
axis has silently rejected an arm"
);
for (pipe_arm, accessor_arm) in via_pipe.iter().zip(via_accessor.iter()) {
assert_eq!(
pipe_arm.as_ref(),
*accessor_arm,
"Semantic::ALL.iter().map(Box::<str>::from) pipe must \
byte-equal the paired Semantic::ALL.iter().map(|s| \
s.as_str()) pipe on every arm — divergence signals \
the borrowed-input `From<&Semantic> for Box<str>` \
axis has silently detoured off the substrate-\
primitive accessor"
);
}
}
}