Skip to main content

domain_key/
key.rs

1//! Core Key implementation for domain-key
2//!
3//! This module contains the main `Key<T>` structure and its implementation,
4//! providing high-performance, type-safe key handling with extensive optimizations.
5
6use core::borrow::Borrow;
7use core::fmt;
8use core::hash::{Hash, Hasher};
9use core::marker::PhantomData;
10use core::ops::Deref;
11use core::str::FromStr;
12
13#[cfg(not(feature = "std"))]
14use alloc::borrow::Cow;
15#[cfg(not(feature = "std"))]
16use alloc::string::{String, ToString};
17
18#[cfg(feature = "std")]
19use std::borrow::Cow;
20
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24use smartstring::alias::String as SmartString;
25
26use crate::domain::KeyDomain;
27use crate::error::KeyParseError;
28use crate::utils;
29
30// ============================================================================
31// CONSTANTS
32// ============================================================================
33
34/// Default maximum allowed length for any key
35///
36/// This is a reasonable default that balances usability with performance.
37/// Keys up to this length can benefit from stack allocation optimizations.
38/// Domains can override this with their own limits.
39pub const DEFAULT_MAX_KEY_LENGTH: usize = 64;
40
41// ============================================================================
42// SPLIT ITERATOR TYPES
43// ============================================================================
44
45/// Split cache type for consistent API
46pub type SplitCache<'a> = core::str::Split<'a, char>;
47
48/// Split iterator with consistent API
49#[derive(Debug)]
50pub struct SplitIterator<'a>(SplitCache<'a>);
51
52impl<'a> Iterator for SplitIterator<'a> {
53    type Item = &'a str;
54
55    #[inline]
56    fn next(&mut self) -> Option<Self::Item> {
57        self.0.next()
58    }
59}
60
61// ============================================================================
62// CORE KEY IMPLEMENTATION
63// ============================================================================
64
65/// High-performance generic key type with advanced optimizations
66///
67/// This is the core key type that provides type safety through the domain
68/// marker `T`. Keys are immutable after creation and use `SmartString` for
69/// optimal memory usage (stack allocation for short keys, heap for longer ones).
70///
71/// # Performance Characteristics
72///
73/// - **Memory Layout**: 32 bytes total (fits in single cache line)
74/// - **Hash Access**: O(1) via pre-computed hash (`.hash() -> u64`)
75/// - **Length Access**: O(1) via `SmartString` (inline length)
76/// - **String Access**: Direct reference to internal storage
77/// - **HashMap Lookup**: by `&str` via `Borrow<str>` — no temporary key needed
78/// - **Clone**: Efficient via `SmartString`'s copy-on-write semantics
79///
80/// # Type Parameters
81///
82/// * `T` - A domain marker type that implements `KeyDomain`
83///
84/// # Memory Layout
85///
86/// ```text
87/// Key<T> struct (32 bytes, cache-line friendly):
88/// ┌─────────────────────┬──────────┬─────────────┐
89/// │ SmartString (24B)   │ hash (8B)│ marker (0B) │
90/// └─────────────────────┴──────────┴─────────────┘
91/// ```
92///
93/// Keys use `SmartString` which stores strings up to 23 bytes inline on the stack,
94/// only allocating on the heap for longer strings. The pre-computed hash (feature-
95/// selected algorithm) is accessible via `.hash() -> u64`. The `Hash` trait
96/// delegates to `str` so that `Borrow<str>` works correctly with `HashMap`.
97///
98/// # Examples
99///
100/// ```rust
101/// use domain_key::{Key, Domain, KeyDomain};
102///
103/// #[derive(Debug)]
104/// struct UserDomain;
105///
106/// impl Domain for UserDomain {
107///     const DOMAIN_NAME: &'static str = "user";
108/// }
109///
110/// impl KeyDomain for UserDomain {
111///     const MAX_LENGTH: usize = 32;
112/// }
113///
114/// type UserKey = Key<UserDomain>;
115///
116/// let key = UserKey::new("john_doe")?;
117/// assert_eq!(key.as_str(), "john_doe");
118/// assert_eq!(key.domain(), "user");
119/// assert_eq!(key.len(), 8);
120/// # Ok::<(), domain_key::KeyParseError>(())
121/// ```
122#[derive(Debug)]
123pub struct Key<T: KeyDomain> {
124    /// Internal string storage using `SmartString` for optimal memory usage
125    inner: SmartString,
126
127    /// Pre-computed hash value accessible via [`Key::hash()`]
128    ///
129    /// This hash is computed once during key creation using the
130    /// feature-selected algorithm (gxhash / ahash / blake3 / fnv-1a).
131    /// It is **not** used by the [`Hash`] trait implementation — that
132    /// one delegates to `self.inner` so that the `Borrow<str>` contract
133    /// (`hash(key) == hash(key.borrow())`) is upheld.
134    hash: u64,
135
136    /// Zero-sized type marker for compile-time type safety
137    ///
138    /// This field provides compile-time type safety without any runtime
139    /// overhead. Different domain types cannot be mixed or compared.
140    _marker: PhantomData<T>,
141}
142
143// Manual Clone implementation to ensure optimal performance
144impl<T: KeyDomain> Clone for Key<T> {
145    /// Efficient clone implementation
146    ///
147    /// Cloning a key is efficient due to `SmartString`'s optimizations:
148    /// - For inline strings (≤23 chars): Simple memory copy
149    /// - For heap strings: Reference counting or copy-on-write
150    #[inline]
151    fn clone(&self) -> Self {
152        Self {
153            inner: self.inner.clone(),
154            hash: self.hash,
155            _marker: PhantomData,
156        }
157    }
158}
159
160// Manual PartialEq/Eq — compare only the key string, not cached fields
161impl<T: KeyDomain> PartialEq for Key<T> {
162    #[inline]
163    fn eq(&self, other: &Self) -> bool {
164        self.inner == other.inner
165    }
166}
167
168impl<T: KeyDomain> Eq for Key<T> {}
169
170// Manual PartialOrd/Ord — compare only the key string
171impl<T: KeyDomain> PartialOrd for Key<T> {
172    #[inline]
173    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
174        Some(self.cmp(other))
175    }
176}
177
178impl<T: KeyDomain> Ord for Key<T> {
179    #[inline]
180    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
181        self.inner.cmp(&other.inner)
182    }
183}
184
185// Hash implementation delegates to the inner string so that the
186// Borrow<str> contract is satisfied: hash(key) == hash(key.borrow()).
187// This allows HashMap<Key<T>, V>::get("some_str") to work correctly.
188impl<T: KeyDomain> Hash for Key<T> {
189    #[inline]
190    fn hash<H: Hasher>(&self, state: &mut H) {
191        self.inner.hash(state);
192    }
193}
194
195// Conditional Serde support for serialization/deserialization
196#[cfg(feature = "serde")]
197impl<T: KeyDomain> Serialize for Key<T> {
198    /// Serialize the key as its string representation
199    ///
200    /// Keys are serialized as their string content, not including
201    /// the cached hash or length for efficiency and compatibility.
202    #[inline]
203    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
204    where
205        S: serde::Serializer,
206    {
207        self.inner.serialize(serializer)
208    }
209}
210
211#[cfg(feature = "serde")]
212impl<'de, T: KeyDomain> Deserialize<'de> for Key<T> {
213    /// Deserialize and validate a key from its string representation
214    ///
215    /// This implementation chooses the optimal deserialization strategy
216    /// based on the format (human-readable vs binary) for best performance.
217    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
218    where
219        D: serde::Deserializer<'de>,
220    {
221        if deserializer.is_human_readable() {
222            // For human-readable formats (JSON, YAML), use zero-copy when possible
223            let s = <&str>::deserialize(deserializer)?;
224            Key::new(s).map_err(serde::de::Error::custom)
225        } else {
226            // For binary formats, deserialize as owned string
227            let s = String::deserialize(deserializer)?;
228            Key::from_string(s).map_err(serde::de::Error::custom)
229        }
230    }
231}
232
233// ============================================================================
234// KEY IMPLEMENTATION - CORE METHODS
235// ============================================================================
236
237impl<T: KeyDomain> Key<T> {
238    /// Creates a new key with comprehensive validation and optimization
239    ///
240    /// This method performs both common validation (length, characters) and
241    /// domain-specific validation according to the key's domain type. It
242    /// automatically chooses the optimal creation path based on the input
243    /// characteristics and domain configuration.
244    ///
245    /// # Arguments
246    ///
247    /// * `key` - String-like input that will be normalized and validated
248    ///
249    /// # Returns
250    ///
251    /// * `Ok(Key<T>)` if the key is valid
252    /// * `Err(KeyParseError)` with the specific validation failure
253    ///
254    /// # Errors
255    ///
256    /// Returns `KeyParseError` if the key fails common validation (empty, too
257    /// long, invalid characters) or domain-specific validation rules
258    ///
259    /// # Examples
260    ///
261    /// ```rust
262    /// use domain_key::{Key, Domain, KeyDomain};
263    ///
264    /// #[derive(Debug)]
265    /// struct TestDomain;
266    /// impl Domain for TestDomain {
267    ///     const DOMAIN_NAME: &'static str = "test";
268    /// }
269    /// impl KeyDomain for TestDomain {}
270    /// type TestKey = Key<TestDomain>;
271    ///
272    /// let key = TestKey::new("valid_key")?;
273    /// assert_eq!(key.as_str(), "valid_key");
274    ///
275    /// // Invalid keys return descriptive errors
276    /// let error = TestKey::new("").unwrap_err();
277    /// assert!(matches!(error, domain_key::KeyParseError::Empty));
278    /// # Ok::<(), domain_key::KeyParseError>(())
279    /// ```
280    #[inline]
281    pub fn new(key: impl AsRef<str>) -> Result<Self, KeyParseError> {
282        let key_str = key.as_ref();
283        Self::new_optimized(key_str)
284    }
285
286    /// Optimized implementation for key creation
287    ///
288    /// This method uses performance optimizations when available:
289    /// - Stack allocation for short keys
290    /// - Fast validation paths
291    /// - Cached operations
292    ///
293    /// # Errors
294    ///
295    /// Returns `KeyParseError` if the constructed key fails validation
296    fn new_optimized(key: &str) -> Result<Self, KeyParseError> {
297        // Step 1: Reject truly empty input before normalization; whitespace-only
298        // strings are handled by normalize() + validate_common() below.
299        if key.is_empty() {
300            return Err(KeyParseError::Empty);
301        }
302
303        // Step 2: Normalization (trimming, lowercasing, domain-specific)
304        let normalized = Self::normalize(key);
305
306        // Step 3: Common validation on the normalized result
307        Self::validate_common(&normalized)?;
308
309        // Step 4: Domain-specific validation
310        T::validate_domain_rules(&normalized).map_err(Self::fix_domain_error)?;
311
312        // Step 5: Hash computation and storage
313        let hash = Self::compute_hash(&normalized);
314
315        Ok(Self {
316            inner: SmartString::from(normalized.as_ref()),
317            hash,
318            _marker: PhantomData,
319        })
320    }
321
322    /// Creates a new key from an owned String with optimized handling
323    ///
324    /// This method is more efficient when you already have a `String` as it
325    /// can reuse the allocation when possible.
326    ///
327    /// # Arguments
328    ///
329    /// * `key` - Owned string that will be normalized and validated
330    ///
331    /// # Errors
332    ///
333    /// Returns `KeyParseError` if the key fails validation
334    ///
335    /// # Examples
336    ///
337    /// ```rust
338    /// use domain_key::{Key, Domain, KeyDomain};
339    ///
340    /// #[derive(Debug)]
341    /// struct TestDomain;
342    /// impl Domain for TestDomain {
343    ///     const DOMAIN_NAME: &'static str = "test";
344    /// }
345    /// impl KeyDomain for TestDomain {}
346    /// type TestKey = Key<TestDomain>;
347    ///
348    /// let key_string = "test_key".to_string();
349    /// let key = TestKey::from_string(key_string)?;
350    /// assert_eq!(key.as_str(), "test_key");
351    /// # Ok::<(), domain_key::KeyParseError>(())
352    /// ```
353    pub fn from_string(key: String) -> Result<Self, KeyParseError> {
354        // Reject obviously empty input before normalization
355        if key.trim().is_empty() {
356            return Err(KeyParseError::Empty);
357        }
358
359        // Normalize efficiently, reusing allocation when possible
360        let normalized = Self::normalize_owned(key);
361
362        // Validate the normalized result
363        Self::validate_common(&normalized)?;
364
365        // Domain validation
366        T::validate_domain_rules(&normalized).map_err(Self::fix_domain_error)?;
367
368        let hash = Self::compute_hash(&normalized);
369
370        Ok(Self {
371            inner: SmartString::from(normalized),
372            hash,
373            _marker: PhantomData,
374        })
375    }
376
377    /// Create a key from multiple parts separated by a delimiter
378    ///
379    /// This method efficiently constructs a key from multiple string parts,
380    /// using pre-calculated sizing to minimize allocations.
381    ///
382    /// # Arguments
383    ///
384    /// * `parts` - Array of string parts to join
385    /// * `delimiter` - String to use as separator between parts
386    ///
387    /// # Returns
388    ///
389    /// * `Ok(Key<T>)` if the constructed key is valid
390    /// * `Err(KeyParseError)` if validation fails
391    ///
392    /// # Examples
393    ///
394    /// ```rust
395    /// use domain_key::{Key, Domain, KeyDomain};
396    ///
397    /// #[derive(Debug)]
398    /// struct TestDomain;
399    /// impl Domain for TestDomain {
400    ///     const DOMAIN_NAME: &'static str = "test";
401    /// }
402    /// impl KeyDomain for TestDomain {}
403    /// type TestKey = Key<TestDomain>;
404    ///
405    /// let key = TestKey::from_parts(&["user", "123", "profile"], "_")?;
406    /// assert_eq!(key.as_str(), "user_123_profile");
407    /// # Ok::<(), domain_key::KeyParseError>(())
408    /// ```
409    /// # Errors
410    ///
411    /// Returns `KeyParseError` if the constructed key fails validation
412    pub fn from_parts(parts: &[&str], delimiter: &str) -> Result<Self, KeyParseError> {
413        if parts.is_empty() {
414            return Err(KeyParseError::Empty);
415        }
416
417        if parts.iter().any(|part| part.is_empty()) {
418            return Err(KeyParseError::InvalidStructure {
419                reason: "Parts cannot contain empty strings",
420            });
421        }
422
423        let joined = parts.join(delimiter);
424
425        if joined.is_empty() {
426            return Err(KeyParseError::Empty);
427        }
428
429        Self::from_string(joined)
430    }
431
432    /// Try to create a key from multiple parts, returning None on failure
433    ///
434    /// This is a convenience method for when you want to handle validation
435    /// failures by ignoring invalid keys rather than handling errors.
436    ///
437    /// # Examples
438    ///
439    /// ```rust
440    /// use domain_key::{Key, Domain, KeyDomain};
441    ///
442    /// #[derive(Debug)]
443    /// struct TestDomain;
444    /// impl Domain for TestDomain {
445    ///     const DOMAIN_NAME: &'static str = "test";
446    /// }
447    /// impl KeyDomain for TestDomain {}
448    /// type TestKey = Key<TestDomain>;
449    ///
450    /// let valid = TestKey::try_from_parts(&["user", "123"], "_").unwrap();
451    /// let invalid = TestKey::try_from_parts(&["", ""], "_"); // Returns None
452    /// assert!(invalid.is_none());
453    /// ```
454    #[must_use]
455    pub fn try_from_parts(parts: &[&str], delimiter: &str) -> Option<Self> {
456        Self::from_parts(parts, delimiter).ok()
457    }
458
459    /// Creates a key from a static string without runtime validation
460    ///
461    /// # Warning
462    ///
463    /// The caller must ensure that the static string follows all validation
464    /// rules for the domain (allowed characters, length limits, normalization,
465    /// domain-specific rules). Invalid keys created this way will violate
466    /// internal invariants and may cause unexpected behavior.
467    ///
468    /// Prefer [`try_from_static`](Self::try_from_static) or the [`static_key!`](macro@crate::static_key) macro
469    /// for safe creation of static keys.
470    ///
471    /// # Panics
472    ///
473    /// Panics in debug builds if the key is empty or exceeds `T::MAX_LENGTH`.
474    ///
475    /// # Arguments
476    ///
477    /// * `key` - A static string literal that represents a valid key
478    ///
479    /// # Examples
480    ///
481    /// ```rust
482    /// use domain_key::{Key, Domain, KeyDomain};
483    ///
484    /// #[derive(Debug)]
485    /// struct TestDomain;
486    /// impl Domain for TestDomain {
487    ///     const DOMAIN_NAME: &'static str = "test";
488    /// }
489    /// impl KeyDomain for TestDomain {}
490    /// type TestKey = Key<TestDomain>;
491    ///
492    /// let key = TestKey::from_static_unchecked("static_key");
493    /// assert_eq!(key.as_str(), "static_key");
494    /// ```
495    #[must_use]
496    pub fn from_static_unchecked(key: &'static str) -> Self {
497        debug_assert!(
498            !key.is_empty(),
499            "from_static_unchecked: key must not be empty"
500        );
501        debug_assert!(
502            key.len() <= T::MAX_LENGTH,
503            "from_static_unchecked: key length {} exceeds domain max {}",
504            key.len(),
505            T::MAX_LENGTH
506        );
507
508        let hash = Self::compute_hash(key);
509
510        Self {
511            inner: SmartString::from(key),
512            hash,
513            _marker: PhantomData,
514        }
515    }
516
517    /// Creates a key from a static string with validation
518    ///
519    /// This is a safer alternative to `from_static_unchecked` that validates
520    /// the key at runtime. The validation cost is paid once, and subsequent
521    /// uses of the key are as fast as the unchecked version.
522    ///
523    /// # Arguments
524    ///
525    /// * `key` - A static string literal to validate and convert
526    ///
527    /// # Errors
528    ///
529    /// Returns `KeyParseError` if the static key fails validation
530    ///
531    /// # Examples
532    ///
533    /// ```rust
534    /// use domain_key::{Key, Domain, KeyDomain};
535    ///
536    /// #[derive(Debug)]
537    /// struct TestDomain;
538    /// impl Domain for TestDomain {
539    ///     const DOMAIN_NAME: &'static str = "test";
540    /// }
541    /// impl KeyDomain for TestDomain {}
542    /// type TestKey = Key<TestDomain>;
543    ///
544    /// let key = TestKey::try_from_static("static_key")?;
545    /// assert_eq!(key.as_str(), "static_key");
546    ///
547    /// let invalid = TestKey::try_from_static("");
548    /// assert!(invalid.is_err());
549    /// # Ok::<(), domain_key::KeyParseError>(())
550    /// ```
551    /// # Errors
552    ///
553    /// Returns `KeyParseError` if the constructed key fails validation
554    pub fn try_from_static(key: &'static str) -> Result<Self, KeyParseError> {
555        // Validate and create via the normal path
556        Self::new(key)
557    }
558
559    /// Try to create a key, returning None on validation failure
560    ///
561    /// This is a convenience method for when you want to handle validation
562    /// failures by ignoring invalid keys rather than handling errors.
563    ///
564    /// # Examples
565    ///
566    /// ```rust
567    /// use domain_key::{Key, Domain, KeyDomain};
568    ///
569    /// #[derive(Debug)]
570    /// struct TestDomain;
571    /// impl Domain for TestDomain {
572    ///     const DOMAIN_NAME: &'static str = "test";
573    /// }
574    /// impl KeyDomain for TestDomain {}
575    /// type TestKey = Key<TestDomain>;
576    ///
577    /// let valid = TestKey::try_new("valid_key").unwrap();
578    /// let invalid = TestKey::try_new(""); // Returns None
579    /// assert!(invalid.is_none());
580    /// ```
581    #[inline]
582    pub fn try_new(key: impl AsRef<str>) -> Option<Self> {
583        Self::new(key).ok()
584    }
585}
586
587// ============================================================================
588// KEY IMPLEMENTATION - ACCESSOR METHODS
589// ============================================================================
590
591impl<T: KeyDomain> Key<T> {
592    /// Returns the key as a string slice
593    ///
594    /// This is the primary way to access the string content of a key.
595    /// The returned reference is valid for the lifetime of the key.
596    ///
597    /// # Examples
598    ///
599    /// ```rust
600    /// use domain_key::{Key, Domain, KeyDomain};
601    ///
602    /// #[derive(Debug)]
603    /// struct TestDomain;
604    /// impl Domain for TestDomain {
605    ///     const DOMAIN_NAME: &'static str = "test";
606    /// }
607    /// impl KeyDomain for TestDomain {}
608    /// type TestKey = Key<TestDomain>;
609    ///
610    /// let key = TestKey::new("example")?;
611    /// assert_eq!(key.as_str(), "example");
612    /// # Ok::<(), domain_key::KeyParseError>(())
613    /// ```
614    #[inline]
615    #[must_use]
616    pub fn as_str(&self) -> &str {
617        &self.inner
618    }
619
620    /// Returns the domain name for this key type
621    ///
622    /// This is a compile-time constant that identifies which domain
623    /// this key belongs to.
624    ///
625    /// # Examples
626    ///
627    /// ```rust
628    /// use domain_key::{Key, Domain, KeyDomain};
629    ///
630    /// #[derive(Debug)]
631    /// struct UserDomain;
632    /// impl Domain for UserDomain {
633    ///     const DOMAIN_NAME: &'static str = "user";
634    /// }
635    /// impl KeyDomain for UserDomain {}
636    /// type UserKey = Key<UserDomain>;
637    ///
638    /// let key = UserKey::new("john")?;
639    /// assert_eq!(key.domain(), "user");
640    /// # Ok::<(), domain_key::KeyParseError>(())
641    /// ```
642    #[inline]
643    #[must_use]
644    pub const fn domain(&self) -> &'static str {
645        T::DOMAIN_NAME
646    }
647
648    /// Returns the length of the key string in bytes
649    ///
650    /// This is an O(1) operation — `SmartString` stores the length inline.
651    ///
652    /// # Examples
653    ///
654    /// ```rust
655    /// use domain_key::{Key, Domain, KeyDomain};
656    ///
657    /// #[derive(Debug)]
658    /// struct TestDomain;
659    /// impl Domain for TestDomain {
660    ///     const DOMAIN_NAME: &'static str = "test";
661    /// }
662    /// impl KeyDomain for TestDomain {}
663    /// type TestKey = Key<TestDomain>;
664    ///
665    /// let key = TestKey::new("example")?;
666    /// assert_eq!(key.len(), 7);
667    /// # Ok::<(), domain_key::KeyParseError>(())
668    /// ```
669    #[inline]
670    #[must_use]
671    pub fn len(&self) -> usize {
672        self.inner.len()
673    }
674
675    /// Returns true if the key is empty (this should never happen for valid keys)
676    ///
677    /// Since empty keys are rejected during validation, this method should
678    /// always return `false` for properly constructed keys. It's provided
679    /// for completeness and debugging purposes.
680    ///
681    /// # Examples
682    ///
683    /// ```rust
684    /// use domain_key::{Key, Domain, KeyDomain};
685    ///
686    /// #[derive(Debug)]
687    /// struct TestDomain;
688    /// impl Domain for TestDomain {
689    ///     const DOMAIN_NAME: &'static str = "test";
690    /// }
691    /// impl KeyDomain for TestDomain {}
692    /// type TestKey = Key<TestDomain>;
693    ///
694    /// let key = TestKey::new("example")?;
695    /// assert!(!key.is_empty());
696    /// # Ok::<(), domain_key::KeyParseError>(())
697    /// ```
698    #[inline]
699    #[must_use]
700    pub fn is_empty(&self) -> bool {
701        self.inner.is_empty()
702    }
703
704    /// Returns the pre-computed hash value
705    ///
706    /// This hash is computed once during key creation using the
707    /// feature-selected algorithm (gxhash / ahash / blake3 / fnv-1a)
708    /// and cached for the lifetime of the key.
709    ///
710    /// **Important:** This is *not* the hash used by [`Hash`] trait /
711    /// `HashMap`.  The `Hash` trait delegates to `str`'s implementation
712    /// so that `Borrow<str>` works correctly.  Use this method when you
713    /// need a deterministic, feature-dependent hash for your own data
714    /// structures or protocols.
715    ///
716    /// **Note:** The hash algorithm depends on the active feature flags
717    /// (`fast`, `secure`, `crypto`, or the default hasher). Keys created
718    /// with different feature configurations will produce different hash
719    /// values. Do not persist or compare hash values across builds with
720    /// different features.
721    ///
722    /// # Examples
723    ///
724    /// ```rust
725    /// use domain_key::{Key, Domain, KeyDomain};
726    ///
727    /// #[derive(Debug)]
728    /// struct TestDomain;
729    /// impl Domain for TestDomain {
730    ///     const DOMAIN_NAME: &'static str = "test";
731    /// }
732    /// impl KeyDomain for TestDomain {}
733    /// type TestKey = Key<TestDomain>;
734    ///
735    /// let key1 = TestKey::new("example")?;
736    /// let key2 = TestKey::new("example")?;
737    /// let key3 = TestKey::new("different")?;
738    ///
739    /// // Same keys have same hash
740    /// assert_eq!(key1.hash(), key2.hash());
741    /// // Different keys have different hashes (with high probability)
742    /// assert_ne!(key1.hash(), key3.hash());
743    /// # Ok::<(), domain_key::KeyParseError>(())
744    /// ```
745    #[inline]
746    #[must_use]
747    pub const fn hash(&self) -> u64 {
748        self.hash
749    }
750
751    /// Checks if this key starts with the given prefix
752    ///
753    /// This is a simple string prefix check that can be useful for
754    /// categorizing or filtering keys.
755    ///
756    /// # Arguments
757    ///
758    /// * `prefix` - The prefix string to check for
759    ///
760    /// # Examples
761    ///
762    /// ```rust
763    /// use domain_key::{Key, Domain, KeyDomain};
764    ///
765    /// #[derive(Debug)]
766    /// struct TestDomain;
767    /// impl Domain for TestDomain {
768    ///     const DOMAIN_NAME: &'static str = "test";
769    /// }
770    /// impl KeyDomain for TestDomain {}
771    /// type TestKey = Key<TestDomain>;
772    ///
773    /// let key = TestKey::new("user_profile")?;
774    /// assert!(key.starts_with("user_"));
775    /// assert!(!key.starts_with("admin_"));
776    /// # Ok::<(), domain_key::KeyParseError>(())
777    /// ```
778    #[inline]
779    #[must_use]
780    pub fn starts_with(&self, prefix: &str) -> bool {
781        self.inner.starts_with(prefix)
782    }
783
784    /// Checks if this key ends with the given suffix
785    ///
786    /// This is a simple string suffix check that can be useful for
787    /// categorizing or filtering keys.
788    ///
789    /// # Arguments
790    ///
791    /// * `suffix` - The suffix string to check for
792    ///
793    /// # Examples
794    ///
795    /// ```rust
796    /// use domain_key::{Key, Domain, KeyDomain};
797    ///
798    /// #[derive(Debug)]
799    /// struct TestDomain;
800    /// impl Domain for TestDomain {
801    ///     const DOMAIN_NAME: &'static str = "test";
802    /// }
803    /// impl KeyDomain for TestDomain {}
804    /// type TestKey = Key<TestDomain>;
805    ///
806    /// let key = TestKey::new("user_profile")?;
807    /// assert!(key.ends_with("_profile"));
808    /// assert!(!key.ends_with("_settings"));
809    /// # Ok::<(), domain_key::KeyParseError>(())
810    /// ```
811    #[inline]
812    #[must_use]
813    pub fn ends_with(&self, suffix: &str) -> bool {
814        self.inner.ends_with(suffix)
815    }
816
817    /// Checks if this key contains the given substring
818    ///
819    /// This performs a substring search within the key.
820    ///
821    /// # Arguments
822    ///
823    /// * `pattern` - The substring to search for
824    ///
825    /// # Examples
826    ///
827    /// ```rust
828    /// use domain_key::{Key, Domain, KeyDomain};
829    ///
830    /// #[derive(Debug)]
831    /// struct TestDomain;
832    /// impl Domain for TestDomain {
833    ///     const DOMAIN_NAME: &'static str = "test";
834    /// }
835    /// impl KeyDomain for TestDomain {}
836    /// type TestKey = Key<TestDomain>;
837    ///
838    /// let key = TestKey::new("user_profile_settings")?;
839    /// assert!(key.contains("profile"));
840    /// assert!(!key.contains("admin"));
841    /// # Ok::<(), domain_key::KeyParseError>(())
842    /// ```
843    #[inline]
844    #[must_use]
845    pub fn contains(&self, pattern: &str) -> bool {
846        self.inner.contains(pattern)
847    }
848
849    /// Returns an iterator over the characters of the key
850    ///
851    /// This provides access to individual characters in the key string.
852    ///
853    /// # Examples
854    ///
855    /// ```rust
856    /// use domain_key::{Key, Domain, KeyDomain};
857    ///
858    /// #[derive(Debug)]
859    /// struct TestDomain;
860    /// impl Domain for TestDomain {
861    ///     const DOMAIN_NAME: &'static str = "test";
862    /// }
863    /// impl KeyDomain for TestDomain {}
864    /// type TestKey = Key<TestDomain>;
865    ///
866    /// let key = TestKey::new("abc")?;
867    /// let chars: Vec<char> = key.chars().collect();
868    /// assert_eq!(chars, vec!['a', 'b', 'c']);
869    /// # Ok::<(), domain_key::KeyParseError>(())
870    /// ```
871    #[inline]
872    pub fn chars(&self) -> core::str::Chars<'_> {
873        self.inner.chars()
874    }
875
876    /// Splits the key by a delimiter and returns an iterator
877    ///
878    /// This method provides consistent split functionality.
879    ///
880    /// # Arguments
881    ///
882    /// * `delimiter` - Character to split on
883    ///
884    /// # Examples
885    ///
886    /// ```rust
887    /// use domain_key::{Key, Domain, KeyDomain};
888    ///
889    /// #[derive(Debug)]
890    /// struct TestDomain;
891    /// impl Domain for TestDomain {
892    ///     const DOMAIN_NAME: &'static str = "test";
893    /// }
894    /// impl KeyDomain for TestDomain {}
895    /// type TestKey = Key<TestDomain>;
896    ///
897    /// let key = TestKey::new("user_profile_settings")?;
898    /// let parts: Vec<&str> = key.split('_').collect();
899    /// assert_eq!(parts, vec!["user", "profile", "settings"]);
900    /// # Ok::<(), domain_key::KeyParseError>(())
901    /// ```
902    #[must_use]
903    pub fn split(&self, delimiter: char) -> SplitIterator<'_> {
904        SplitIterator(utils::new_split_cache(&self.inner, delimiter))
905    }
906
907    /// Split operation for consistent API
908    ///
909    /// This method provides the same functionality as `split()` but with explicit naming
910    /// for cases where caching behavior needs to be clear.
911    #[must_use]
912    pub fn split_cached(&self, delimiter: char) -> SplitCache<'_> {
913        utils::new_split_cache(&self.inner, delimiter)
914    }
915
916    /// Splits the key by a string delimiter and returns an iterator
917    ///
918    /// This method splits the key using a string pattern rather than a single character.
919    ///
920    /// # Examples
921    ///
922    /// ```rust
923    /// use domain_key::{Key, Domain, KeyDomain};
924    ///
925    /// #[derive(Debug)]
926    /// struct TestDomain;
927    /// impl Domain for TestDomain {
928    ///     const DOMAIN_NAME: &'static str = "test";
929    /// }
930    /// impl KeyDomain for TestDomain {}
931    /// type TestKey = Key<TestDomain>;
932    ///
933    /// let key = TestKey::new("user-and-profile-and-settings")?;
934    /// let parts: Vec<&str> = key.split_str("-and-").collect();
935    /// assert_eq!(parts, vec!["user", "profile", "settings"]);
936    /// # Ok::<(), domain_key::KeyParseError>(())
937    /// ```
938    #[must_use]
939    pub fn split_str<'a>(&'a self, delimiter: &'a str) -> core::str::Split<'a, &'a str> {
940        self.inner.split(delimiter)
941    }
942
943    /// Returns the key with a prefix if it doesn't already have it
944    ///
945    /// This method efficiently adds a prefix to a key if it doesn't already
946    /// start with that prefix.
947    ///
948    /// # Arguments
949    ///
950    /// * `prefix` - The prefix to ensure is present
951    ///
952    /// # Examples
953    ///
954    /// ```rust
955    /// use domain_key::{Key, Domain, KeyDomain};
956    ///
957    /// #[derive(Debug)]
958    /// struct TestDomain;
959    /// impl Domain for TestDomain {
960    ///     const DOMAIN_NAME: &'static str = "test";
961    /// }
962    /// impl KeyDomain for TestDomain {}
963    /// type TestKey = Key<TestDomain>;
964    ///
965    /// let key = TestKey::new("profile")?;
966    /// let prefixed = key.ensure_prefix("user_")?;
967    /// assert_eq!(prefixed.as_str(), "user_profile");
968    ///
969    /// // If prefix already exists, returns the same key
970    /// let already_prefixed = prefixed.ensure_prefix("user_")?;
971    /// assert_eq!(already_prefixed.as_str(), "user_profile");
972    /// # Ok::<(), domain_key::KeyParseError>(())
973    /// ```
974    /// # Errors
975    ///
976    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
977    pub fn ensure_prefix(&self, prefix: &str) -> Result<Self, KeyParseError> {
978        if self.starts_with(prefix) {
979            return Ok(self.clone());
980        }
981
982        let new_len = prefix.len() + self.len();
983        if new_len > T::MAX_LENGTH {
984            return Err(KeyParseError::TooLong {
985                max_length: T::MAX_LENGTH,
986                actual_length: new_len,
987            });
988        }
989
990        let result = utils::add_prefix_optimized(&self.inner, prefix);
991
992        // Full structural validation (start char, end char, consecutive chars at junction)
993        Self::validate_common(&result)?;
994
995        T::validate_domain_rules(&result).map_err(Self::fix_domain_error)?;
996
997        let hash = Self::compute_hash(&result);
998
999        Ok(Self {
1000            inner: result,
1001            hash,
1002            _marker: PhantomData,
1003        })
1004    }
1005
1006    /// Returns the key with a suffix if it doesn't already have it
1007    ///
1008    /// This method efficiently adds a suffix to a key if it doesn't already
1009    /// end with that suffix.
1010    ///
1011    /// # Arguments
1012    ///
1013    /// * `suffix` - The suffix to ensure is present
1014    ///
1015    /// # Examples
1016    ///
1017    /// ```rust
1018    /// use domain_key::{Key, Domain, KeyDomain};
1019    ///
1020    /// #[derive(Debug)]
1021    /// struct TestDomain;
1022    /// impl Domain for TestDomain {
1023    ///     const DOMAIN_NAME: &'static str = "test";
1024    /// }
1025    /// impl KeyDomain for TestDomain {}
1026    /// type TestKey = Key<TestDomain>;
1027    ///
1028    /// let key = TestKey::new("user")?;
1029    /// let suffixed = key.ensure_suffix("_profile")?;
1030    /// assert_eq!(suffixed.as_str(), "user_profile");
1031    ///
1032    /// // If suffix already exists, returns the same key
1033    /// let already_suffixed = suffixed.ensure_suffix("_profile")?;
1034    /// assert_eq!(already_suffixed.as_str(), "user_profile");
1035    /// # Ok::<(), domain_key::KeyParseError>(())
1036    /// ```
1037    /// # Errors
1038    ///
1039    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1040    pub fn ensure_suffix(&self, suffix: &str) -> Result<Self, KeyParseError> {
1041        if self.ends_with(suffix) {
1042            return Ok(self.clone());
1043        }
1044
1045        let new_len = self.len() + suffix.len();
1046        if new_len > T::MAX_LENGTH {
1047            return Err(KeyParseError::TooLong {
1048                max_length: T::MAX_LENGTH,
1049                actual_length: new_len,
1050            });
1051        }
1052
1053        let result = utils::add_suffix_optimized(&self.inner, suffix);
1054
1055        // Full structural validation (start char, end char, consecutive chars at junction)
1056        Self::validate_common(&result)?;
1057
1058        T::validate_domain_rules(&result).map_err(Self::fix_domain_error)?;
1059
1060        let hash = Self::compute_hash(&result);
1061
1062        Ok(Self {
1063            inner: result,
1064            hash,
1065            _marker: PhantomData,
1066        })
1067    }
1068
1069    /// Get validation rules that this key satisfies
1070    ///
1071    /// Returns detailed information about the validation characteristics
1072    /// of this key and its domain, useful for debugging and introspection.
1073    ///
1074    /// # Examples
1075    ///
1076    /// ```rust
1077    /// use domain_key::{Key, Domain, KeyDomain};
1078    ///
1079    /// #[derive(Debug)]
1080    /// struct TestDomain;
1081    /// impl Domain for TestDomain {
1082    ///     const DOMAIN_NAME: &'static str = "test";
1083    /// }
1084    /// impl KeyDomain for TestDomain {
1085    ///     const MAX_LENGTH: usize = 32;
1086    ///     const HAS_CUSTOM_VALIDATION: bool = true;
1087    /// }
1088    /// type TestKey = Key<TestDomain>;
1089    ///
1090    /// let key = TestKey::new("example")?;
1091    /// let info = key.validation_info();
1092    ///
1093    /// assert_eq!(info.domain_info.name, "test");
1094    /// assert_eq!(info.domain_info.max_length, 32);
1095    /// assert_eq!(info.length, 7);
1096    /// assert!(info.domain_info.has_custom_validation);
1097    /// # Ok::<(), domain_key::KeyParseError>(())
1098    /// ```
1099    #[must_use]
1100    pub fn validation_info(&self) -> KeyValidationInfo {
1101        KeyValidationInfo {
1102            domain_info: crate::domain::domain_info::<T>(),
1103            length: self.len(),
1104        }
1105    }
1106}
1107
1108// ============================================================================
1109// KEY IMPLEMENTATION - HELPER METHODS
1110// ============================================================================
1111
1112impl<T: KeyDomain> Key<T> {
1113    /// Fix domain name in domain validation errors
1114    ///
1115    /// This helper ensures that domain validation errors have the correct
1116    /// domain name, even when they're created generically.
1117    #[inline]
1118    fn fix_domain_error(e: KeyParseError) -> KeyParseError {
1119        match e {
1120            KeyParseError::DomainValidation { message, .. } => KeyParseError::DomainValidation {
1121                domain: T::DOMAIN_NAME,
1122                message,
1123            },
1124            other => other,
1125        }
1126    }
1127
1128    /// Common validation pipeline
1129    ///
1130    /// Performs validation that's common to all domains: length checking,
1131    /// character validation, and structural validation.
1132    ///
1133    /// # Errors
1134    ///
1135    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1136    pub(crate) fn validate_common(key: &str) -> Result<(), KeyParseError> {
1137        // The caller (new_optimized / from_string) has already normalized
1138        // the key (which includes trimming), so we validate `key` directly
1139        // without an extra .trim() pass.
1140
1141        if key.is_empty() {
1142            return Err(KeyParseError::Empty);
1143        }
1144
1145        if key.len() > T::MAX_LENGTH {
1146            return Err(KeyParseError::TooLong {
1147                max_length: T::MAX_LENGTH,
1148                actual_length: key.len(),
1149            });
1150        }
1151
1152        if key.len() < T::min_length() {
1153            return Err(KeyParseError::TooShort {
1154                min_length: T::min_length(),
1155                actual_length: key.len(),
1156            });
1157        }
1158
1159        // Use fast validation
1160        Self::validate_fast(key)
1161    }
1162
1163    /// Fast validation path using optimized algorithms
1164    /// # Errors
1165    ///
1166    /// Returns `KeyParseError` if the prefixed key would be invalid or too long
1167    fn validate_fast(key: &str) -> Result<(), KeyParseError> {
1168        let mut chars = key.char_indices();
1169        let mut prev_char = None;
1170
1171        // Validate first character
1172        if let Some((pos, first)) = chars.next() {
1173            let char_allowed = crate::utils::char_validation::is_key_char_fast(first)
1174                || T::allowed_start_character(first);
1175
1176            if !char_allowed {
1177                return Err(KeyParseError::InvalidCharacter {
1178                    character: first,
1179                    position: pos,
1180                    expected: Some("allowed by domain"),
1181                });
1182            }
1183
1184            prev_char = Some(first);
1185        }
1186
1187        // Validate remaining characters
1188        for (pos, c) in chars {
1189            let char_allowed = T::allowed_characters(c);
1190
1191            if !char_allowed {
1192                return Err(KeyParseError::InvalidCharacter {
1193                    character: c,
1194                    position: pos,
1195                    expected: Some("allowed by domain"),
1196                });
1197            }
1198
1199            if let Some(prev) = prev_char {
1200                if !T::allowed_consecutive_characters(prev, c) {
1201                    return Err(KeyParseError::InvalidStructure {
1202                        reason: "consecutive characters not allowed",
1203                    });
1204                }
1205            }
1206            prev_char = Some(c);
1207        }
1208
1209        // Check last character
1210        if let Some(last) = prev_char {
1211            if !T::allowed_end_character(last) {
1212                return Err(KeyParseError::InvalidStructure {
1213                    reason: "invalid end character",
1214                });
1215            }
1216        }
1217
1218        Ok(())
1219    }
1220
1221    /// Normalize a borrowed string
1222    pub(crate) fn normalize(key: &str) -> Cow<'_, str> {
1223        let trimmed = key.trim();
1224
1225        let needs_lowercase =
1226            T::CASE_INSENSITIVE && trimmed.chars().any(|c| c.is_ascii_uppercase());
1227
1228        let base = if needs_lowercase {
1229            Cow::Owned(trimmed.to_ascii_lowercase())
1230        } else {
1231            // Borrow the trimmed slice — no allocation needed
1232            Cow::Borrowed(trimmed)
1233        };
1234
1235        // Apply domain-specific normalization
1236        T::normalize_domain(base)
1237    }
1238
1239    /// Normalize an owned string efficiently
1240    fn normalize_owned(mut key: String) -> String {
1241        // In-place trim: remove leading whitespace by draining, then truncate trailing
1242        let start = key.len() - key.trim_start().len();
1243        if start > 0 {
1244            key.drain(..start);
1245        }
1246        let trimmed_len = key.trim_end().len();
1247        key.truncate(trimmed_len);
1248
1249        if T::CASE_INSENSITIVE {
1250            key.make_ascii_lowercase();
1251        }
1252
1253        // Apply domain normalization
1254        match T::normalize_domain(Cow::Owned(key)) {
1255            Cow::Owned(s) => s,
1256            Cow::Borrowed(s) => s.to_owned(),
1257        }
1258    }
1259
1260    /// Compute hash using the configured algorithm
1261    ///
1262    /// The hash algorithm is selected at compile time based on feature flags,
1263    /// allowing for different performance/security trade-offs.
1264    pub(crate) fn compute_hash(key: &str) -> u64 {
1265        if key.is_empty() {
1266            return 0;
1267        }
1268
1269        Self::compute_hash_inner(key.as_bytes())
1270    }
1271
1272    /// Inner hash computation dispatched by feature flags
1273    ///
1274    /// Separated to keep each cfg branch as the sole return path,
1275    /// avoiding mixed `return` statements and dead-code warnings.
1276    fn compute_hash_inner(bytes: &[u8]) -> u64 {
1277        // Priority: fast > secure > crypto > default
1278
1279        #[cfg(feature = "fast")]
1280        {
1281            #[cfg(any(
1282                all(target_arch = "x86_64", target_feature = "aes"),
1283                all(
1284                    target_arch = "aarch64",
1285                    target_feature = "aes",
1286                    target_feature = "neon"
1287                )
1288            ))]
1289            {
1290                gxhash::gxhash64(bytes, 0)
1291            }
1292
1293            #[cfg(not(any(
1294                all(target_arch = "x86_64", target_feature = "aes"),
1295                all(
1296                    target_arch = "aarch64",
1297                    target_feature = "aes",
1298                    target_feature = "neon"
1299                )
1300            )))]
1301            {
1302                use core::hash::Hasher;
1303                let mut hasher = ahash::AHasher::default();
1304                hasher.write(bytes);
1305                hasher.finish()
1306            }
1307        }
1308
1309        #[cfg(all(feature = "secure", not(feature = "fast")))]
1310        {
1311            use core::hash::Hasher;
1312            let mut hasher = ahash::AHasher::default();
1313            hasher.write(bytes);
1314            hasher.finish()
1315        }
1316
1317        #[cfg(all(feature = "crypto", not(any(feature = "fast", feature = "secure"))))]
1318        {
1319            let hash = blake3::hash(bytes);
1320            let h = hash.as_bytes();
1321            u64::from_le_bytes([h[0], h[1], h[2], h[3], h[4], h[5], h[6], h[7]])
1322        }
1323
1324        #[cfg(not(any(feature = "fast", feature = "secure", feature = "crypto")))]
1325        {
1326            #[cfg(feature = "std")]
1327            {
1328                use core::hash::Hasher;
1329                use std::collections::hash_map::DefaultHasher;
1330                let mut hasher = DefaultHasher::new();
1331                hasher.write(bytes);
1332                hasher.finish()
1333            }
1334
1335            #[cfg(not(feature = "std"))]
1336            {
1337                Self::fnv1a_hash(bytes)
1338            }
1339        }
1340    }
1341
1342    /// FNV-1a hash implementation for `no_std` environments
1343    #[allow(dead_code)]
1344    fn fnv1a_hash(bytes: &[u8]) -> u64 {
1345        const FNV_OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325;
1346        const FNV_PRIME: u64 = 0x0100_0000_01b3;
1347
1348        let mut hash = FNV_OFFSET_BASIS;
1349        for &byte in bytes {
1350            hash ^= u64::from(byte);
1351            hash = hash.wrapping_mul(FNV_PRIME);
1352        }
1353        hash
1354    }
1355}
1356
1357// ============================================================================
1358// SUPPORTING TYPES
1359// ============================================================================
1360
1361/// Information about a key's validation characteristics
1362///
1363/// This structure provides detailed information about how a key was validated
1364/// and what domain-specific features are enabled. Domain-level configuration
1365/// is available through the embedded [`DomainInfo`](crate::DomainInfo).
1366#[derive(Debug, Clone, PartialEq, Eq)]
1367pub struct KeyValidationInfo {
1368    /// Full domain configuration
1369    pub domain_info: crate::domain::DomainInfo,
1370    /// Actual length of the key
1371    pub length: usize,
1372}
1373
1374// ============================================================================
1375// STANDARD TRAIT IMPLEMENTATIONS
1376// ============================================================================
1377
1378/// Display implementation shows the key value
1379///
1380/// Outputs just the key string, consistent with `AsRef<str>`, `From<Key<T>> for String`,
1381/// and serde serialization. Use [`Key::domain`] separately when domain context is needed.
1382impl<T: KeyDomain> fmt::Display for Key<T> {
1383    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1384        f.write_str(&self.inner)
1385    }
1386}
1387
1388/// `Deref` implementation for automatic coercion to `&str`
1389///
1390/// This allows `&key` to automatically coerce to `&str` in contexts
1391/// that expect a string slice, eliminating the need for explicit
1392/// `.as_ref()` or `.as_str()` calls in most situations.
1393///
1394/// # Examples
1395///
1396/// ```rust
1397/// use domain_key::{Key, Domain, KeyDomain};
1398///
1399/// #[derive(Debug)]
1400/// struct TestDomain;
1401/// impl Domain for TestDomain {
1402///     const DOMAIN_NAME: &'static str = "test";
1403/// }
1404/// impl KeyDomain for TestDomain {}
1405/// type TestKey = Key<TestDomain>;
1406///
1407/// let key = TestKey::new("example")?;
1408///
1409/// // Automatic coercion — no .as_ref() needed
1410/// let s: &str = &key;
1411/// assert_eq!(s, "example");
1412///
1413/// // Works with functions expecting &str
1414/// fn takes_str(_s: &str) {}
1415/// takes_str(&key);
1416/// # Ok::<(), domain_key::KeyParseError>(())
1417/// ```
1418impl<T: KeyDomain> Deref for Key<T> {
1419    type Target = str;
1420
1421    #[inline]
1422    fn deref(&self) -> &str {
1423        &self.inner
1424    }
1425}
1426
1427/// `AsRef` implementation for string conversion
1428impl<T: KeyDomain> AsRef<str> for Key<T> {
1429    #[inline]
1430    fn as_ref(&self) -> &str {
1431        self
1432    }
1433}
1434
1435/// `Borrow<str>` implementation enabling `HashMap<Key<T>, V>::get("str")`
1436///
1437/// This is sound because the [`Hash`] trait implementation for `Key<T>`
1438/// delegates to `str`'s hash, satisfying the contract
1439/// `hash(key) == hash(key.borrow())`.
1440impl<T: KeyDomain> Borrow<str> for Key<T> {
1441    #[inline]
1442    fn borrow(&self) -> &str {
1443        self
1444    }
1445}
1446
1447/// From implementation for converting to String
1448impl<T: KeyDomain> From<Key<T>> for String {
1449    fn from(key: Key<T>) -> Self {
1450        key.inner.into()
1451    }
1452}
1453
1454/// Creates a `Key` from a pre-validated [`SmartString`] **without re-validation**.
1455///
1456/// This is intended for internal or advanced usage where the caller has
1457/// already ensured that the string satisfies all domain rules (length,
1458/// allowed characters, normalization, etc.).  Hash is computed
1459/// automatically, but **no validation or normalization is performed**.
1460///
1461/// # Safety (logical)
1462///
1463/// If the string does not satisfy the domain's invariants the resulting
1464/// key will silently violate those invariants.  Prefer [`Key::new`] or
1465/// [`Key::from_string`] unless you are certain the input is valid.
1466impl<T: KeyDomain> From<SmartString> for Key<T> {
1467    #[inline]
1468    fn from(inner: SmartString) -> Self {
1469        let hash = Self::compute_hash(&inner);
1470
1471        Self {
1472            inner,
1473            hash,
1474            _marker: PhantomData,
1475        }
1476    }
1477}
1478
1479/// `TryFrom<String>` implementation for owned string conversion
1480///
1481/// This avoids re-borrowing through `&str` when you already have a `String`.
1482impl<T: KeyDomain> TryFrom<String> for Key<T> {
1483    type Error = KeyParseError;
1484
1485    fn try_from(s: String) -> Result<Self, Self::Error> {
1486        Key::from_string(s)
1487    }
1488}
1489
1490/// `TryFrom<&str>` implementation for borrowed string conversion
1491impl<T: KeyDomain> TryFrom<&str> for Key<T> {
1492    type Error = KeyParseError;
1493
1494    fn try_from(s: &str) -> Result<Self, Self::Error> {
1495        Key::new(s)
1496    }
1497}
1498
1499/// `FromStr` implementation for parsing from strings
1500impl<T: KeyDomain> FromStr for Key<T> {
1501    type Err = KeyParseError;
1502
1503    fn from_str(s: &str) -> Result<Self, Self::Err> {
1504        Key::new(s)
1505    }
1506}
1507
1508// ============================================================================
1509// TESTS
1510// ============================================================================
1511
1512#[cfg(test)]
1513mod tests {
1514    use super::*;
1515    use crate::domain::{DefaultDomain, Domain};
1516    #[cfg(not(feature = "std"))]
1517    use alloc::format;
1518    #[cfg(not(feature = "std"))]
1519    use alloc::string::ToString;
1520    #[cfg(not(feature = "std"))]
1521    use alloc::vec;
1522    #[cfg(not(feature = "std"))]
1523    use alloc::vec::Vec;
1524
1525    // Test domain
1526    #[derive(Debug)]
1527    struct TestDomain;
1528
1529    impl Domain for TestDomain {
1530        const DOMAIN_NAME: &'static str = "test";
1531    }
1532
1533    impl KeyDomain for TestDomain {
1534        const MAX_LENGTH: usize = 32;
1535        const HAS_CUSTOM_VALIDATION: bool = true;
1536        const HAS_CUSTOM_NORMALIZATION: bool = true;
1537        const CASE_INSENSITIVE: bool = true;
1538
1539        fn validate_domain_rules(key: &str) -> Result<(), KeyParseError> {
1540            if key.starts_with("invalid_") {
1541                return Err(KeyParseError::domain_error(
1542                    Self::DOMAIN_NAME,
1543                    "Keys cannot start with 'invalid_'",
1544                ));
1545            }
1546            Ok(())
1547        }
1548
1549        fn normalize_domain(key: Cow<'_, str>) -> Cow<'_, str> {
1550            if key.contains('-') {
1551                Cow::Owned(key.replace('-', "_"))
1552            } else {
1553                key
1554            }
1555        }
1556
1557        fn allowed_characters(c: char) -> bool {
1558            c.is_ascii_alphanumeric() || c == '_' || c == '-'
1559        }
1560
1561        fn validation_help() -> Option<&'static str> {
1562            Some("Use alphanumeric characters, underscores, and hyphens. Cannot start with 'invalid_'.")
1563        }
1564    }
1565
1566    type TestKey = Key<TestDomain>;
1567
1568    #[test]
1569    fn new_key_stores_value_and_domain() {
1570        let key = TestKey::new("valid_key").unwrap();
1571        assert_eq!(key.as_str(), "valid_key");
1572        assert_eq!(key.domain(), "test");
1573        assert_eq!(key.len(), 9);
1574    }
1575
1576    #[test]
1577    fn case_insensitive_domain_lowercases_and_normalizes() {
1578        let key = TestKey::new("Test-Key").unwrap();
1579        assert_eq!(key.as_str(), "test_key");
1580    }
1581
1582    #[test]
1583    fn domain_rules_reject_invalid_prefix() {
1584        let result = TestKey::new("invalid_key");
1585        assert!(result.is_err());
1586
1587        if let Err(KeyParseError::DomainValidation { domain, message }) = result {
1588            assert_eq!(domain, "test");
1589            assert!(message.contains("invalid_"));
1590        } else {
1591            panic!("Expected domain validation error");
1592        }
1593    }
1594
1595    #[test]
1596    fn rejects_empty_too_long_and_invalid_characters() {
1597        // Empty key
1598        assert!(matches!(TestKey::new(""), Err(KeyParseError::Empty)));
1599
1600        // Too long key
1601        let long_key = "a".repeat(50);
1602        assert!(matches!(
1603            TestKey::new(&long_key),
1604            Err(KeyParseError::TooLong {
1605                max_length: 32,
1606                actual_length: 50
1607            })
1608        ));
1609
1610        // Invalid character
1611        let result = TestKey::new("key with spaces");
1612        assert!(matches!(
1613            result,
1614            Err(KeyParseError::InvalidCharacter {
1615                character: ' ',
1616                position: 3,
1617                ..
1618            })
1619        ));
1620    }
1621
1622    #[test]
1623    fn equal_keys_produce_same_hash() {
1624        use core::hash::{Hash, Hasher};
1625
1626        let key1 = TestKey::new("test_key").unwrap();
1627        let key2 = TestKey::new("test_key").unwrap();
1628
1629        // Pre-computed hashes should match
1630        assert_eq!(key1.hash(), key2.hash());
1631
1632        let key3 = TestKey::new("different_key").unwrap();
1633        assert_ne!(key1.hash(), key3.hash());
1634
1635        // Hash trait should produce same result as hashing the raw &str,
1636        // so Borrow<str> contract is upheld.
1637        let key_trait_hash = {
1638            let mut h = std::collections::hash_map::DefaultHasher::new();
1639            Hash::hash(&key1, &mut h);
1640            h.finish()
1641        };
1642        let str_trait_hash = {
1643            let mut h = std::collections::hash_map::DefaultHasher::new();
1644            Hash::hash(key1.as_str(), &mut h);
1645            h.finish()
1646        };
1647        assert_eq!(key_trait_hash, str_trait_hash);
1648    }
1649
1650    #[test]
1651    fn string_query_methods_work_correctly() {
1652        let key = TestKey::new("test_key_example").unwrap();
1653        assert!(key.starts_with("test_"));
1654        assert!(key.ends_with("_example"));
1655        assert!(key.contains("_key_"));
1656        assert_eq!(key.len(), 16);
1657        assert!(!key.is_empty());
1658    }
1659
1660    #[test]
1661    fn from_string_validates_owned_input() {
1662        let key = TestKey::from_string("test_key".to_string()).unwrap();
1663        assert_eq!(key.as_str(), "test_key");
1664    }
1665
1666    #[test]
1667    fn try_from_static_rejects_empty_string() {
1668        let key = TestKey::try_from_static("static_key").unwrap();
1669        assert_eq!(key.as_str(), "static_key");
1670
1671        let invalid = TestKey::try_from_static("");
1672        assert!(invalid.is_err());
1673    }
1674
1675    #[test]
1676    fn validation_info_reflects_domain_config() {
1677        let key = TestKey::new("test_key").unwrap();
1678        let info = key.validation_info();
1679
1680        assert_eq!(info.domain_info.name, "test");
1681        assert_eq!(info.domain_info.max_length, 32);
1682        assert_eq!(info.length, 8);
1683        assert!(info.domain_info.has_custom_validation);
1684        assert!(info.domain_info.has_custom_normalization);
1685    }
1686
1687    #[cfg(feature = "serde")]
1688    #[test]
1689    fn serde_roundtrip_preserves_key() {
1690        let key = TestKey::new("test_key").unwrap();
1691
1692        // Test JSON serialization
1693        let json = serde_json::to_string(&key).unwrap();
1694        assert_eq!(json, r#""test_key""#);
1695
1696        // Test JSON deserialization
1697        let deserialized: TestKey = serde_json::from_str(&json).unwrap();
1698        assert_eq!(deserialized, key);
1699    }
1700
1701    #[test]
1702    fn from_parts_joins_and_splits_roundtrip() {
1703        let key = TestKey::from_parts(&["user", "123", "profile"], "_").unwrap();
1704        assert_eq!(key.as_str(), "user_123_profile");
1705
1706        let parts: Vec<&str> = key.split('_').collect();
1707        assert_eq!(parts, vec!["user", "123", "profile"]);
1708    }
1709
1710    #[test]
1711    fn ensure_prefix_suffix_is_idempotent() {
1712        let key = TestKey::new("profile").unwrap();
1713
1714        let prefixed = key.ensure_prefix("user_").unwrap();
1715        assert_eq!(prefixed.as_str(), "user_profile");
1716
1717        // Already has prefix
1718        let same = prefixed.ensure_prefix("user_").unwrap();
1719        assert_eq!(same.as_str(), "user_profile");
1720
1721        let suffixed = key.ensure_suffix("_v1").unwrap();
1722        assert_eq!(suffixed.as_str(), "profile_v1");
1723
1724        // Already has suffix
1725        let same = suffixed.ensure_suffix("_v1").unwrap();
1726        assert_eq!(same.as_str(), "profile_v1");
1727    }
1728
1729    #[test]
1730    fn display_shows_raw_key_value() {
1731        let key = TestKey::new("example").unwrap();
1732        assert_eq!(format!("{key}"), "example");
1733    }
1734
1735    #[test]
1736    fn into_string_extracts_value() {
1737        let key = TestKey::new("example").unwrap();
1738        let string: String = key.into();
1739        assert_eq!(string, "example");
1740    }
1741
1742    #[test]
1743    fn parse_str_creates_validated_key() {
1744        let key: TestKey = "example".parse().unwrap();
1745        assert_eq!(key.as_str(), "example");
1746    }
1747
1748    #[test]
1749    fn default_domain_accepts_simple_keys() {
1750        type DefaultKey = Key<DefaultDomain>;
1751        let key = DefaultKey::new("test_key").unwrap();
1752        assert_eq!(key.domain(), "default");
1753        assert_eq!(key.as_str(), "test_key");
1754    }
1755
1756    #[test]
1757    fn len_returns_consistent_cached_value() {
1758        let key = TestKey::new("test_key").unwrap();
1759        assert_eq!(key.len(), 8);
1760        assert_eq!(key.len(), 8); // Second call — same result
1761    }
1762
1763    #[test]
1764    fn split_methods_produce_same_parts() {
1765        let key = TestKey::new("user_profile_settings").unwrap();
1766
1767        let parts: Vec<&str> = key.split('_').collect();
1768        assert_eq!(parts, vec!["user", "profile", "settings"]);
1769
1770        let cached_parts: Vec<&str> = key.split_cached('_').collect();
1771        assert_eq!(cached_parts, vec!["user", "profile", "settings"]);
1772
1773        let str_parts: Vec<&str> = key.split_str("_").collect();
1774        assert_eq!(str_parts, vec!["user", "profile", "settings"]);
1775    }
1776
1777    #[test]
1778    fn deref_coerces_to_str() {
1779        let key = TestKey::new("hello").unwrap();
1780        // Deref allows &Key<T> → &str coercion
1781        let s: &str = &key;
1782        assert_eq!(s, "hello");
1783
1784        // Works with functions that accept &str
1785        fn takes_str(s: &str) -> &str {
1786            s
1787        }
1788        assert_eq!(takes_str(&key), "hello");
1789    }
1790
1791    #[test]
1792    fn from_smartstring_creates_key_without_revalidation() {
1793        use smartstring::alias::String as SmartString;
1794
1795        let smart = SmartString::from("pre_validated");
1796        let key: TestKey = TestKey::from(smart);
1797        assert_eq!(key.as_str(), "pre_validated");
1798        assert_eq!(key.len(), 13);
1799        // Hash should be computed correctly
1800        assert_ne!(key.hash(), 0);
1801    }
1802
1803    #[test]
1804    fn borrow_str_enables_hashmap_get_by_str() {
1805        use std::collections::HashMap;
1806
1807        let mut map: HashMap<TestKey, u32> = HashMap::new();
1808        let key = TestKey::new("lookup_test").unwrap();
1809        map.insert(key, 42);
1810
1811        // Lookup by &str — works thanks to Borrow<str>
1812        assert_eq!(map.get("lookup_test"), Some(&42));
1813        assert_eq!(map.get("nonexistent"), None);
1814    }
1815
1816    #[test]
1817    fn struct_is_32_bytes() {
1818        // SmartString(24) + u64 hash(8) + PhantomData(0) = 32 bytes
1819        assert_eq!(core::mem::size_of::<TestKey>(), 32);
1820    }
1821}