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
use crate::PropertyFilter;
use crate::ONLY_ENUMERABLE;
use crate::SKIP_SYMBOLS;

#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum KeyConversionMode {
  /// kConvertToString will convert integer indices to strings.
  ConvertToString,
  /// kKeepNumbers will return numbers for integer indices.
  KeepNumbers,
  NoNumbers,
}

/// Keys/Properties filter enums:
///
/// KeyCollectionMode limits the range of collected properties. kOwnOnly limits
/// the collected properties to the given Object only. kIncludesPrototypes will
/// include all keys of the objects's prototype chain as well.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum KeyCollectionMode {
  /// OwnOnly limits the collected properties to the given Object only.
  OwnOnly,
  /// kIncludesPrototypes will include all keys of the objects's prototype chain
  /// as well.
  IncludePrototypes,
}

#[derive(Debug, Clone, Copy)]
#[repr(C)]
pub enum IndexFilter {
  /// kIncludesIndices allows for integer indices to be collected.
  IncludeIndices,
  /// kSkipIndices will exclude integer indices from being collected.
  SkipIndices,
}

pub struct GetPropertyNamesArgs {
  pub mode: KeyCollectionMode,
  pub property_filter: PropertyFilter,
  pub index_filter: IndexFilter,
  pub key_conversion: KeyConversionMode,
}

impl Default for GetPropertyNamesArgs {
  fn default() -> Self {
    GetPropertyNamesArgs {
      mode: KeyCollectionMode::IncludePrototypes,
      property_filter: ONLY_ENUMERABLE | SKIP_SYMBOLS,
      index_filter: IndexFilter::IncludeIndices,
      key_conversion: KeyConversionMode::KeepNumbers,
    }
  }
}

pub struct GetPropertyNamesArgsBuilder {
  mode: KeyCollectionMode,
  property_filter: PropertyFilter,
  index_filter: IndexFilter,
  key_conversion: KeyConversionMode,
}

impl Default for GetPropertyNamesArgsBuilder {
  fn default() -> Self {
    Self::new()
  }
}

impl GetPropertyNamesArgsBuilder {
  #[inline(always)]
  pub fn new() -> Self {
    Self {
      mode: KeyCollectionMode::IncludePrototypes,
      property_filter: ONLY_ENUMERABLE | SKIP_SYMBOLS,
      index_filter: IndexFilter::IncludeIndices,
      key_conversion: KeyConversionMode::KeepNumbers,
    }
  }

  #[inline(always)]
  pub fn build(&self) -> GetPropertyNamesArgs {
    GetPropertyNamesArgs {
      mode: self.mode,
      property_filter: self.property_filter,
      index_filter: self.index_filter,
      key_conversion: self.key_conversion,
    }
  }

  #[inline(always)]
  pub fn mode(
    &mut self,
    mode: KeyCollectionMode,
  ) -> &mut GetPropertyNamesArgsBuilder {
    self.mode = mode;
    self
  }

  #[inline(always)]
  pub fn property_filter(
    &mut self,
    property_filter: PropertyFilter,
  ) -> &mut GetPropertyNamesArgsBuilder {
    self.property_filter = property_filter;
    self
  }

  #[inline(always)]
  pub fn index_filter(
    &mut self,
    index_filter: IndexFilter,
  ) -> &mut GetPropertyNamesArgsBuilder {
    self.index_filter = index_filter;
    self
  }

  #[inline(always)]
  pub fn key_conversion(
    &mut self,
    key_conversion: KeyConversionMode,
  ) -> &mut Self {
    self.key_conversion = key_conversion;
    self
  }
}