-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathliteral.rb
81 lines (69 loc) · 1.96 KB
/
literal.rb
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
# frozen_string_literal: true
module Literal
autoload :Array, "literal/array"
autoload :Data, "literal/data"
autoload :DataProperty, "literal/data_property"
autoload :DataStructure, "literal/data_structure"
autoload :Enum, "literal/enum"
autoload :Flags, "literal/flags"
autoload :Flags16, "literal/flags"
autoload :Flags32, "literal/flags"
autoload :Flags64, "literal/flags"
autoload :Flags8, "literal/flags"
autoload :Hash, "literal/hash"
autoload :Null, "literal/null"
autoload :Object, "literal/object"
autoload :Properties, "literal/properties"
autoload :Property, "literal/property"
autoload :Set, "literal/set"
autoload :Struct, "literal/struct"
autoload :Type, "literal/type"
autoload :Types, "literal/types"
autoload :Tuple, "literal/tuple"
# Errors
autoload :Error, "literal/errors/error"
autoload :TypeError, "literal/errors/type_error"
autoload :ArgumentError, "literal/errors/argument_error"
autoload :TRANSFORMS, "literal/transforms"
def self.Enum(type)
Class.new(Literal::Enum) do
prop :value, type, :positional
end
end
def self.Array(type)
Literal::Array::Generic.new(type)
end
def self.Set(type)
Literal::Set::Generic.new(type)
end
def self.Hash(key_type, value_type)
Literal::Hash::Generic.new(key_type, value_type)
end
def self.Tuple(*types)
Literal::Tuple::Generic.new(*types)
end
def self.check(actual:, expected:)
if expected === actual
true
else
context = Literal::TypeError::Context.new(expected:, actual:)
expected.record_literal_type_errors(context) if expected.respond_to?(:record_literal_type_errors)
yield context if block_given?
raise Literal::TypeError.new(context:)
end
end
def self.subtype?(type, of:)
type = type.block.call if Types::DeferredType === type
(of == type) || case of
when Literal::Type
of >= type
when Module
(Module === type) ? of >= type : false
when Range
of.cover?(type)
else
false
end
end
end
require_relative "literal/rails" if defined?(Rails)