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
#[repr(C)]
#[derive(Debug, Eq, PartialEq)]
pub struct PropertyAttribute(u32);

impl PropertyAttribute {
  /// No property attributes.
  pub const NONE: Self = Self(0);

  /// Not writable. Corresponds to
  /// `Object.defineProperty(o, "p", { writable: false })`.
  pub const READ_ONLY: Self = Self(1 << 0);

  /// Not enumerable. Corresponds to
  /// `Object.defineProperty(o, "p", { enumerable: false })`.
  pub const DONT_ENUM: Self = Self(1 << 1);

  /// Not configurable. Corresponds to
  /// `Object.defineProperty(o, "p", { configurable: false })`.
  pub const DONT_DELETE: Self = Self(1 << 2);

  /// Test if no property attributes are set.
  #[inline(always)]
  pub fn is_none(&self) -> bool {
    *self == PropertyAttribute::NONE
  }

  /// Test if the read-only property attribute is set.
  #[inline(always)]
  pub fn is_read_only(&self) -> bool {
    self.has(Self::READ_ONLY)
  }

  /// Test if the non-enumerable property attribute is set.
  #[inline(always)]
  pub fn is_dont_enum(&self) -> bool {
    self.has(Self::DONT_ENUM)
  }

  /// Test if the non-configurable property attribute is set.
  #[inline(always)]
  pub fn is_dont_delete(&self) -> bool {
    self.has(Self::DONT_DELETE)
  }

  #[inline(always)]
  fn has(&self, that: Self) -> bool {
    let Self(lhs) = self;
    let Self(rhs) = that;
    0 != lhs & rhs
  }

  pub fn as_u32(&self) -> u32 {
    let Self(bits) = self;
    *bits
  }
}

// Identical to #[derive(Default)] but arguably clearer when made explicit.
impl Default for PropertyAttribute {
  fn default() -> Self {
    Self::NONE
  }
}

impl std::ops::BitOr for PropertyAttribute {
  type Output = Self;

  fn bitor(self, Self(rhs): Self) -> Self {
    let Self(lhs) = self;
    Self(lhs | rhs)
  }
}

#[test]
fn test_attr() {
  assert!(PropertyAttribute::NONE.is_none());
  assert!(!PropertyAttribute::NONE.is_read_only());
  assert!(!PropertyAttribute::NONE.is_dont_enum());
  assert!(!PropertyAttribute::NONE.is_dont_delete());

  assert!(!PropertyAttribute::READ_ONLY.is_none());
  assert!(PropertyAttribute::READ_ONLY.is_read_only());
  assert!(!PropertyAttribute::READ_ONLY.is_dont_enum());
  assert!(!PropertyAttribute::READ_ONLY.is_dont_delete());

  assert!(!PropertyAttribute::DONT_ENUM.is_none());
  assert!(!PropertyAttribute::DONT_ENUM.is_read_only());
  assert!(PropertyAttribute::DONT_ENUM.is_dont_enum());
  assert!(!PropertyAttribute::DONT_ENUM.is_dont_delete());

  assert!(!PropertyAttribute::DONT_DELETE.is_none());
  assert!(!PropertyAttribute::DONT_DELETE.is_read_only());
  assert!(!PropertyAttribute::DONT_DELETE.is_dont_enum());
  assert!(PropertyAttribute::DONT_DELETE.is_dont_delete());

  assert_eq!(PropertyAttribute::NONE, Default::default());
  assert_eq!(
    PropertyAttribute::READ_ONLY,
    PropertyAttribute::NONE | PropertyAttribute::READ_ONLY
  );

  let attr = PropertyAttribute::READ_ONLY | PropertyAttribute::DONT_ENUM;
  assert!(!attr.is_none());
  assert!(attr.is_read_only());
  assert!(attr.is_dont_enum());
  assert!(!attr.is_dont_delete());

  let attr = PropertyAttribute::READ_ONLY
    | PropertyAttribute::READ_ONLY
    | PropertyAttribute::DONT_ENUM;
  assert!(!attr.is_none());
  assert!(attr.is_read_only());
  assert!(attr.is_dont_enum());
  assert!(!attr.is_dont_delete());
}