Class: Bare

Inherits:
Object
  • Object
show all
Defined in:
lib/bare-rb.rb

Class Method Summary collapse

Class Method Details

.Array(*opts) ⇒ Object



153
154
155
# File 'lib/bare-rb.rb', line 153

def self.Array(*opts)
  return BareTypes::Array.new(*opts)
end

.ArrayFixedLen(*opts) ⇒ Object



157
158
159
# File 'lib/bare-rb.rb', line 157

def self.ArrayFixedLen(*opts)
  return BareTypes::ArrayFixedLen.new(*opts)
end

.BoolObject



145
146
147
# File 'lib/bare-rb.rb', line 145

def self.Bool
  return BareTypes::Bool.new
end

.DataObject



137
138
139
# File 'lib/bare-rb.rb', line 137

def self.Data
  return BareTypes::Data.new
end

.DataFixedLen(*opts) ⇒ Object



133
134
135
# File 'lib/bare-rb.rb', line 133

def self.DataFixedLen(*opts)
  return BareTypes::DataFixedLen.new(*opts)
end

.decode(msg, schema, type = nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/bare-rb.rb', line 24

def self.decode(msg, schema, type = nil)
  if schema.is_a?(BareTypes::Schema)
    raise NoTypeProvided.new("To decode with a schema as opposed to a raw type you must specify which type in the same you want to encode as a symbol.\nBare.encode(msg, schema, :Type)") if type.nil?
    value, _ = schema[type].decode(msg)
    value
  else
    value, _ = schema.decode(msg)
    value
  end
end

.encode(msg, schema, type = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bare-rb.rb', line 10

def self.encode(msg, schema, type = nil)
  buffer = "".b
  if schema.is_a?(BareTypes::Schema)
    raise NoTypeProvided.new("To encode with a schema as opposed to a raw type you must specify which type in the schema you want to encode as a symbol.\nBare.encode(msg, schema, :Type)") if type.nil?
    unless schema.types.include?(type)
      raise("#{type} is not a type found in this schema. Choose from #{schema.types.keys}")
    end
    schema[type].encode(msg, buffer)
  else
    schema.encode(msg, buffer)
  end
  buffer
end

.Enum(*opts) ⇒ Object



161
162
163
# File 'lib/bare-rb.rb', line 161

def self.Enum(*opts)
  return BareTypes::Enum.new(*opts)
end

.F32Object



77
78
79
# File 'lib/bare-rb.rb', line 77

def self.F32
  return BareTypes::F32.new
end

.F64Object



81
82
83
# File 'lib/bare-rb.rb', line 81

def self.F64
  return BareTypes::F64.new
end

.generative_test(schema_path = nil, binary_path = nil) ⇒ Object

Returns a schema and a binary input optionally write these to files



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bare-rb.rb', line 37

def self.generative_test(schema_path=nil, binary_path=nil)
  schema = BareTypes::Schema.make
  input = schema.create_input
  key = schema.types.keys[0]
  binary = Bare.encode(input[key], schema, key)
  unless binary_path.nil?
    file = File.open(binary_path, 'wb+')
    file.write(binary)
    file.close
  end
  unless schema_path.nil?
    file = File.open(schema_path, 'w+')
    file.write(schema.to_s)
    file.close
  end
  return schema, binary, key
end

.I16Object



109
110
111
# File 'lib/bare-rb.rb', line 109

def self.I16
  return BareTypes::I16.new
end

.I32Object



113
114
115
# File 'lib/bare-rb.rb', line 113

def self.I32
  return BareTypes::I32.new
end

.I64Object



117
118
119
# File 'lib/bare-rb.rb', line 117

def self.I64
  return BareTypes::I64.new
end

.I8Object



105
106
107
# File 'lib/bare-rb.rb', line 105

def self.I8
  return BareTypes::I8.new
end

.IntObject

These classes are wrapped in methods for ergonomics. Isn’t Bare.Array(Bare.U8) nicer than Bare::Array.new(Bare::U8.new)?



69
70
71
# File 'lib/bare-rb.rb', line 69

def self.Int
  return BareTypes::Int.new
end

.Map(*opts) ⇒ Object



125
126
127
# File 'lib/bare-rb.rb', line 125

def self.Map(*opts)
  return BareTypes::Map.new(*opts)
end

.Optional(*opts) ⇒ Object



121
122
123
# File 'lib/bare-rb.rb', line 121

def self.Optional(*opts)
  return BareTypes::Optional.new(*opts)
end

.parse_schema(path) ⇒ Object



55
56
57
58
59
60
# File 'lib/bare-rb.rb', line 55

def self.parse_schema(path)
  # Hash of class names to BARE types
  # Eg. types['Customer'] == Bare.i32
  parsed = parser(lexer(path))
  Bare.Schema(parsed)
end

.Schema(hash) ⇒ Object



62
63
64
# File 'lib/bare-rb.rb', line 62

def self.Schema(hash)
  BareTypes::Schema.new(hash)
end

.StringObject



85
86
87
# File 'lib/bare-rb.rb', line 85

def self.String
  return BareTypes::String.new
end

.Struct(*opts) ⇒ Object



149
150
151
# File 'lib/bare-rb.rb', line 149

def self.Struct(*opts)
  return BareTypes::Struct.new(*opts)
end

.U16Object



93
94
95
# File 'lib/bare-rb.rb', line 93

def self.U16
  return BareTypes::U16.new
end

.U32Object



97
98
99
# File 'lib/bare-rb.rb', line 97

def self.U32
  return BareTypes::U32.new
end

.U64Object



101
102
103
# File 'lib/bare-rb.rb', line 101

def self.U64
  return BareTypes::U64.new
end

.U8Object



89
90
91
# File 'lib/bare-rb.rb', line 89

def self.U8
  return BareTypes::U8.new
end

.UintObject



141
142
143
# File 'lib/bare-rb.rb', line 141

def self.Uint
  return BareTypes::Uint.new
end

.Union(*opts) ⇒ Object



129
130
131
# File 'lib/bare-rb.rb', line 129

def self.Union(*opts)
  return BareTypes::Union.new(*opts)
end

.VoidObject



73
74
75
# File 'lib/bare-rb.rb', line 73

def self.Void
  return BareTypes::Void.new
end