bare-types 0.3.0

A zero-cost foundation for type-safe domain modeling in Rust. Implements the 'Parse, don't validate' philosophy to eliminate primitive obsession and ensure data integrity at the system boundary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
//! CPU architecture type for system information.
//!
//! This module provides a type-safe abstraction for CPU architectures,
//! ensuring valid architecture identifiers and providing convenient constants
//! for common architectures.
//!
//! # Supported Architectures
//!
//! This type supports all major CPU architectures:
//!
//! - **x86**: 32-bit x86 (i386, i686)
//! - **`x86_64`**: 64-bit x86 (amd64)
//! - **aarch64**: 64-bit ARM (arm64)
//! - **arm**: 32-bit ARM
//! - **riscv32**: 32-bit RISC-V
//! - **riscv64**: 64-bit RISC-V
//! - **wasm32**: 32-bit WebAssembly
//! - **wasm64**: 64-bit WebAssembly
//! - **mips**: 32-bit MIPS
//! - **mips64**: 64-bit MIPS
//! - **powerpc**: 32-bit PowerPC
//! - **powerpc64**: 64-bit PowerPC
//! - **s390x**: IBM System/390
//!
//! # Examples
//!
//! ```rust
//! use bare_types::sys::Arch;
//!
//! // Get current architecture (compile-time constant)
//! let arch = Arch::current();
//!
//! // Check architecture properties
//! assert!(arch.is_64_bit() || arch.is_32_bit());
//!
//! // Parse from string
//! let arch: Arch = "x86_64".parse()?;
//! assert_eq!(arch, Arch::X86_64);
//! # Ok::<(), bare_types::sys::ArchError>(())
//! ```

use core::fmt;
use core::str::FromStr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "arbitrary")]
use arbitrary::Arbitrary;

/// Error type for architecture parsing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum ArchError {
    /// Unknown architecture string
    ///
    /// The provided string does not match any known CPU architecture.
    /// Supported architectures include: x86, `x86_64`, aarch64, arm, riscv32,
    /// riscv64, wasm32, wasm64, mips, mips64, powerpc, powerpc64, s390x.
    UnknownArchitecture,
}

impl fmt::Display for ArchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownArchitecture => write!(f, "unknown architecture"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ArchError {}

/// CPU architecture type.
///
/// This enum provides type-safe CPU architecture identifiers.
/// All variants are validated at construction time.
///
/// # Invariants
///
/// - All variants represent valid CPU architectures
/// - Architecture information is determined at compile time
///
/// # Examples
///
/// ```rust
/// use bare_types::sys::Arch;
///
/// // Use predefined constants
/// let arch = Arch::X86_64;
/// assert!(arch.is_64_bit());
///
/// // Get current architecture
/// let current = Arch::current();
/// println!("Running on: {}", current);
///
/// // Convert to string representation
/// assert_eq!(Arch::AARCH64.as_str(), "aarch64");
/// # Ok::<(), bare_types::sys::ArchError>(())
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(Arbitrary))]
#[non_exhaustive]
pub enum Arch {
    /// 32-bit x86 architecture (i386, i686)
    X86,
    /// 64-bit x86 architecture (amd64)
    X86_64,
    /// 64-bit ARM architecture (arm64)
    AARCH64,
    /// 32-bit ARM architecture
    ARM,
    /// 32-bit RISC-V architecture
    RISCV32,
    /// 64-bit RISC-V architecture
    RISCV64,
    /// 32-bit WebAssembly
    WASM32,
    /// 64-bit WebAssembly
    WASM64,
    /// 32-bit MIPS architecture
    MIPS,
    /// 64-bit MIPS architecture
    MIPS64,
    /// 32-bit PowerPC architecture
    POWERPC,
    /// 64-bit PowerPC architecture
    POWERPC64,
    /// IBM System/390 architecture
    S390X,
}

impl Arch {
    /// Returns the current architecture (compile-time constant).
    ///
    /// This method returns the architecture the code was compiled for,
    /// not necessarily the architecture of the host system (especially
    /// relevant for cross-compilation).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// let arch = Arch::current();
    /// println!("Compiled for: {}", arch);
    /// ```
    #[must_use]
    pub const fn current() -> Self {
        #[cfg(target_arch = "x86")]
        return Self::X86;
        #[cfg(target_arch = "x86_64")]
        return Self::X86_64;
        #[cfg(target_arch = "aarch64")]
        return Self::AARCH64;
        #[cfg(target_arch = "arm")]
        return Self::ARM;
        #[cfg(target_arch = "riscv32")]
        return Self::RISCV32;
        #[cfg(target_arch = "riscv64")]
        return Self::RISCV64;
        #[cfg(target_arch = "wasm32")]
        return Self::WASM32;
        #[cfg(target_arch = "wasm64")]
        return Self::WASM64;
        #[cfg(target_arch = "mips")]
        return Self::MIPS;
        #[cfg(target_arch = "mips64")]
        return Self::MIPS64;
        #[cfg(target_arch = "powerpc")]
        return Self::POWERPC;
        #[cfg(target_arch = "powerpc64")]
        return Self::POWERPC64;
        #[cfg(target_arch = "s390x")]
        return Self::S390X;
        #[cfg(not(any(
            target_arch = "x86",
            target_arch = "x86_64",
            target_arch = "aarch64",
            target_arch = "arm",
            target_arch = "riscv32",
            target_arch = "riscv64",
            target_arch = "wasm32",
            target_arch = "wasm64",
            target_arch = "mips",
            target_arch = "mips64",
            target_arch = "powerpc",
            target_arch = "powerpc64",
            target_arch = "s390x",
        )))]
        {
            // For unknown architectures, we can't provide a constant
            // This branch should ideally not be reached
            panic!("unsupported target architecture")
        }
    }

    /// Returns the string representation of this architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert_eq!(Arch::X86_64.as_str(), "x86_64");
    /// assert_eq!(Arch::AARCH64.as_str(), "aarch64");
    /// assert_eq!(Arch::X86.as_str(), "x86");
    /// ```
    #[must_use]
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::X86 => "x86",
            Self::X86_64 => "x86_64",
            Self::AARCH64 => "aarch64",
            Self::ARM => "arm",
            Self::RISCV32 => "riscv32",
            Self::RISCV64 => "riscv64",
            Self::WASM32 => "wasm32",
            Self::WASM64 => "wasm64",
            Self::MIPS => "mips",
            Self::MIPS64 => "mips64",
            Self::POWERPC => "powerpc",
            Self::POWERPC64 => "powerpc64",
            Self::S390X => "s390x",
        }
    }

    /// Returns `true` if this is a 64-bit architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::X86_64.is_64_bit());
    /// assert!(Arch::AARCH64.is_64_bit());
    /// assert!(!Arch::X86.is_64_bit());
    /// assert!(!Arch::ARM.is_64_bit());
    /// ```
    #[must_use]
    pub const fn is_64_bit(&self) -> bool {
        matches!(
            self,
            Self::X86_64
                | Self::AARCH64
                | Self::RISCV64
                | Self::WASM64
                | Self::MIPS64
                | Self::POWERPC64
                | Self::S390X
        )
    }

    /// Returns `true` if this is a 32-bit architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::X86.is_32_bit());
    /// assert!(Arch::ARM.is_32_bit());
    /// assert!(!Arch::X86_64.is_32_bit());
    /// assert!(!Arch::AARCH64.is_32_bit());
    /// ```
    #[must_use]
    pub const fn is_32_bit(&self) -> bool {
        matches!(
            self,
            Self::X86 | Self::ARM | Self::RISCV32 | Self::WASM32 | Self::MIPS | Self::POWERPC
        )
    }

    /// Returns `true` if this is an ARM architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::AARCH64.is_arm());
    /// assert!(Arch::ARM.is_arm());
    /// assert!(!Arch::X86_64.is_arm());
    /// ```
    #[must_use]
    pub const fn is_arm(&self) -> bool {
        matches!(self, Self::ARM | Self::AARCH64)
    }

    /// Returns `true` if this is an x86 architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::X86.is_x86());
    /// assert!(Arch::X86_64.is_x86());
    /// assert!(!Arch::AARCH64.is_x86());
    /// ```
    #[must_use]
    pub const fn is_x86(&self) -> bool {
        matches!(self, Self::X86 | Self::X86_64)
    }

    /// Returns `true` if this is a RISC-V architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::RISCV32.is_riscv());
    /// assert!(Arch::RISCV64.is_riscv());
    /// assert!(!Arch::X86_64.is_riscv());
    /// ```
    #[must_use]
    pub const fn is_riscv(&self) -> bool {
        matches!(self, Self::RISCV32 | Self::RISCV64)
    }

    /// Returns `true` if this is a WebAssembly architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::WASM32.is_wasm());
    /// assert!(Arch::WASM64.is_wasm());
    /// assert!(!Arch::X86_64.is_wasm());
    /// ```
    #[must_use]
    pub const fn is_wasm(&self) -> bool {
        matches!(self, Self::WASM32 | Self::WASM64)
    }

    /// Returns `true` if this is a MIPS architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::MIPS.is_mips());
    /// assert!(Arch::MIPS64.is_mips());
    /// assert!(!Arch::X86_64.is_mips());
    /// ```
    #[must_use]
    pub const fn is_mips(&self) -> bool {
        matches!(self, Self::MIPS | Self::MIPS64)
    }

    /// Returns `true` if this is a PowerPC architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert!(Arch::POWERPC.is_powerpc());
    /// assert!(Arch::POWERPC64.is_powerpc());
    /// assert!(!Arch::X86_64.is_powerpc());
    /// ```
    #[must_use]
    pub const fn is_powerpc(&self) -> bool {
        matches!(self, Self::POWERPC | Self::POWERPC64)
    }

    /// Returns the pointer width in bits for this architecture.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::sys::Arch;
    ///
    /// assert_eq!(Arch::X86_64.pointer_width(), 64);
    /// assert_eq!(Arch::X86.pointer_width(), 32);
    /// assert_eq!(Arch::AARCH64.pointer_width(), 64);
    /// ```
    #[must_use]
    pub const fn pointer_width(&self) -> u8 {
        if self.is_64_bit() { 64 } else { 32 }
    }
}

impl TryFrom<&str> for Arch {
    type Error = ArchError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl FromStr for Arch {
    type Err = ArchError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "x86" | "i386" | "i486" | "i586" | "i686" => Ok(Self::X86),
            "x86_64" | "amd64" | "x64" => Ok(Self::X86_64),
            "aarch64" | "arm64" => Ok(Self::AARCH64),
            "arm" => Ok(Self::ARM),
            "riscv32" | "riscv-32" | "riscv_32" => Ok(Self::RISCV32),
            "riscv64" | "riscv-64" | "riscv_64" => Ok(Self::RISCV64),
            "wasm32" | "wasm-32" | "wasm_32" => Ok(Self::WASM32),
            "wasm64" | "wasm-64" | "wasm_64" => Ok(Self::WASM64),
            "mips" => Ok(Self::MIPS),
            "mips64" => Ok(Self::MIPS64),
            "powerpc" | "ppc" => Ok(Self::POWERPC),
            "powerpc64" | "ppc64" => Ok(Self::POWERPC64),
            "s390x" | "s390" => Ok(Self::S390X),
            _ => Err(ArchError::UnknownArchitecture),
        }
    }
}

impl fmt::Display for Arch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_as_str() {
        assert_eq!(Arch::X86.as_str(), "x86");
        assert_eq!(Arch::X86_64.as_str(), "x86_64");
        assert_eq!(Arch::AARCH64.as_str(), "aarch64");
        assert_eq!(Arch::ARM.as_str(), "arm");
        assert_eq!(Arch::RISCV32.as_str(), "riscv32");
        assert_eq!(Arch::RISCV64.as_str(), "riscv64");
        assert_eq!(Arch::WASM32.as_str(), "wasm32");
        assert_eq!(Arch::WASM64.as_str(), "wasm64");
        assert_eq!(Arch::MIPS.as_str(), "mips");
        assert_eq!(Arch::MIPS64.as_str(), "mips64");
        assert_eq!(Arch::POWERPC.as_str(), "powerpc");
        assert_eq!(Arch::POWERPC64.as_str(), "powerpc64");
        assert_eq!(Arch::S390X.as_str(), "s390x");
    }

    #[test]
    fn test_is_64_bit() {
        assert!(!Arch::X86.is_64_bit());
        assert!(Arch::X86_64.is_64_bit());
        assert!(Arch::AARCH64.is_64_bit());
        assert!(!Arch::ARM.is_64_bit());
        assert!(!Arch::RISCV32.is_64_bit());
        assert!(Arch::RISCV64.is_64_bit());
        assert!(!Arch::WASM32.is_64_bit());
        assert!(Arch::WASM64.is_64_bit());
        assert!(!Arch::MIPS.is_64_bit());
        assert!(Arch::MIPS64.is_64_bit());
        assert!(!Arch::POWERPC.is_64_bit());
        assert!(Arch::POWERPC64.is_64_bit());
        assert!(Arch::S390X.is_64_bit());
    }

    #[test]
    fn test_is_32_bit() {
        assert!(Arch::X86.is_32_bit());
        assert!(!Arch::X86_64.is_32_bit());
        assert!(!Arch::AARCH64.is_32_bit());
        assert!(Arch::ARM.is_32_bit());
        assert!(Arch::RISCV32.is_32_bit());
        assert!(!Arch::RISCV64.is_32_bit());
        assert!(Arch::WASM32.is_32_bit());
        assert!(!Arch::WASM64.is_32_bit());
        assert!(Arch::MIPS.is_32_bit());
        assert!(!Arch::MIPS64.is_32_bit());
        assert!(Arch::POWERPC.is_32_bit());
        assert!(!Arch::POWERPC64.is_32_bit());
        assert!(!Arch::S390X.is_32_bit());
    }

    #[test]
    fn test_is_arm() {
        assert!(!Arch::X86.is_arm());
        assert!(!Arch::X86_64.is_arm());
        assert!(Arch::AARCH64.is_arm());
        assert!(Arch::ARM.is_arm());
    }

    #[test]
    fn test_is_x86() {
        assert!(Arch::X86.is_x86());
        assert!(Arch::X86_64.is_x86());
        assert!(!Arch::AARCH64.is_x86());
        assert!(!Arch::ARM.is_x86());
    }

    #[test]
    fn test_is_riscv() {
        assert!(Arch::RISCV32.is_riscv());
        assert!(Arch::RISCV64.is_riscv());
        assert!(!Arch::X86_64.is_riscv());
    }

    #[test]
    fn test_is_wasm() {
        assert!(Arch::WASM32.is_wasm());
        assert!(Arch::WASM64.is_wasm());
        assert!(!Arch::X86_64.is_wasm());
    }

    #[test]
    fn test_is_mips() {
        assert!(Arch::MIPS.is_mips());
        assert!(Arch::MIPS64.is_mips());
        assert!(!Arch::X86_64.is_mips());
    }

    #[test]
    fn test_is_powerpc() {
        assert!(Arch::POWERPC.is_powerpc());
        assert!(Arch::POWERPC64.is_powerpc());
        assert!(!Arch::X86_64.is_powerpc());
    }

    #[test]
    fn test_pointer_width() {
        assert_eq!(Arch::X86.pointer_width(), 32);
        assert_eq!(Arch::X86_64.pointer_width(), 64);
        assert_eq!(Arch::AARCH64.pointer_width(), 64);
        assert_eq!(Arch::ARM.pointer_width(), 32);
    }

    #[test]
    fn test_from_str() {
        assert_eq!("x86".parse::<Arch>().unwrap(), Arch::X86);
        assert_eq!("x86_64".parse::<Arch>().unwrap(), Arch::X86_64);
        assert_eq!("amd64".parse::<Arch>().unwrap(), Arch::X86_64);
        assert_eq!("aarch64".parse::<Arch>().unwrap(), Arch::AARCH64);
        assert_eq!("arm64".parse::<Arch>().unwrap(), Arch::AARCH64);
        assert_eq!("arm".parse::<Arch>().unwrap(), Arch::ARM);
        assert_eq!("riscv32".parse::<Arch>().unwrap(), Arch::RISCV32);
        assert_eq!("riscv64".parse::<Arch>().unwrap(), Arch::RISCV64);
        assert_eq!("wasm32".parse::<Arch>().unwrap(), Arch::WASM32);
        assert_eq!("wasm64".parse::<Arch>().unwrap(), Arch::WASM64);
        assert_eq!("mips".parse::<Arch>().unwrap(), Arch::MIPS);
        assert_eq!("mips64".parse::<Arch>().unwrap(), Arch::MIPS64);
        assert_eq!("powerpc".parse::<Arch>().unwrap(), Arch::POWERPC);
        assert_eq!("ppc".parse::<Arch>().unwrap(), Arch::POWERPC);
        assert_eq!("powerpc64".parse::<Arch>().unwrap(), Arch::POWERPC64);
        assert_eq!("ppc64".parse::<Arch>().unwrap(), Arch::POWERPC64);
        assert_eq!("s390x".parse::<Arch>().unwrap(), Arch::S390X);

        // Test case insensitivity
        assert_eq!("X86_64".parse::<Arch>().unwrap(), Arch::X86_64);
        assert_eq!("AARCH64".parse::<Arch>().unwrap(), Arch::AARCH64);
    }

    #[test]
    fn test_from_str_error() {
        assert!("unknown".parse::<Arch>().is_err());
        assert!("".parse::<Arch>().is_err());
    }

    #[test]
    fn test_display() {
        assert_eq!(format!("{}", Arch::X86_64), "x86_64");
        assert_eq!(format!("{}", Arch::AARCH64), "aarch64");
        assert_eq!(format!("{}", Arch::X86), "x86");
    }

    #[test]
    fn test_current() {
        // Just verify it doesn't panic
        let _arch = Arch::current();
    }

    #[test]
    fn test_copy() {
        let arch = Arch::X86_64;
        let arch2 = arch;
        assert_eq!(arch, arch2);
    }

    #[test]
    fn test_clone() {
        let arch = Arch::X86_64;
        let arch2 = arch.clone();
        assert_eq!(arch, arch2);
    }

    #[test]
    fn test_equality() {
        assert_eq!(Arch::X86_64, Arch::X86_64);
        assert_ne!(Arch::X86_64, Arch::X86);
    }

    #[test]
    fn test_ordering() {
        assert!(Arch::X86 < Arch::X86_64);
        assert!(Arch::AARCH64 < Arch::ARM); // AARCH64 defined before ARM in enum
    }
}