Module: Kumi::Core::Analyzer::StateSerde

Defined in:
lib/kumi/core/analyzer/state_serde.rb

Class Method Summary collapse

Class Method Details

.decode_json_safe(x) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kumi/core/analyzer/state_serde.rb', line 46

def decode_json_safe(x)
  case x
  when Hash
    # Check for special encoding markers first (before key transformation)
    if    x.key?("$sym") then x["$sym"].to_sym
    elsif x.key?("$set") then Set.new(x["$set"].map { |item| decode_json_safe(item) })
    elsif x.key?("$ir")  then x["$ir"] # Keep as string inspection for JSON round-trip
    else
      # Regular hash - transform keys to symbols and recursively decode values
      x.transform_keys(&:to_sym).transform_values { |value| decode_json_safe(value) }
    end
  when Array then x.map { |item| decode_json_safe(item) }
  else x
  end
end

.dump_json(state, pretty: true) ⇒ Object

Human-readable snapshot (best-effort; not guaranteed resumable)



23
24
25
26
# File 'lib/kumi/core/analyzer/state_serde.rb', line 23

def dump_json(state, pretty: true)
  h = encode_json_safe(state.to_h)
  pretty ? JSON.pretty_generate(h) : JSON.generate(h)
end

.dump_marshal(state) ⇒ Object

Exact round-trip (recommended for resume)



13
14
15
# File 'lib/kumi/core/analyzer/state_serde.rb', line 13

def dump_marshal(state)
  Marshal.dump({ v: 1, data: state.to_h })
end

.encode_json_safe(x) ⇒ Object

—- helpers —-



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kumi/core/analyzer/state_serde.rb', line 34

def encode_json_safe(x)
  case x
  when Hash  then x.transform_keys(&:to_s).transform_values { |v| encode_json_safe(v) }
  when Array then x.map { |v| encode_json_safe(v) }
  when Set   then { "$set" => x.to_a.map { |v| encode_json_safe(v) } }
  when Symbol then { "$sym" => x.to_s }
  when ::Kumi::Core::IR::Module, ::Kumi::Core::IR::Decl, ::Kumi::Core::IR::Op
    { "$ir" => x.inspect }
  else x
  end
end

.load_json(json_str) ⇒ Object



28
29
30
31
# File 'lib/kumi/core/analyzer/state_serde.rb', line 28

def load_json(json_str)
  h = JSON.parse(json_str) # Don't symbolize keys - let decode_json_safe handle it
  ::Kumi::Core::Analyzer::AnalysisState.new(decode_json_safe(h))
end

.load_marshal(bytes) ⇒ Object



17
18
19
20
# File 'lib/kumi/core/analyzer/state_serde.rb', line 17

def load_marshal(bytes)
  payload = Marshal.load(bytes)
  ::Kumi::Core::Analyzer::AnalysisState.new(payload[:data])
end