SusSourceSusOutputStructured

class Structured

Represents a structured JSON output handler for machine-readable output.

Definitions

def self.buffered(...)

Create a buffered structured output handler.

Signature

parameter io IO

The IO object to write to.

parameter identity Identity, nil

Optional identity.

returns Buffered

A new Buffered instance wrapping a Structured handler.

Implementation

def self.buffered(...)
	Buffered.new(self.new(...))
end

def initialize(io, identity = nil)

Initialize a new Structured output handler.

Signature

parameter io IO

The IO object to write to.

parameter identity Identity, nil

Optional identity.

Implementation

def initialize(io, identity = nil)
	@io = io
	@identity = identity
end

def skip(reason, identity)

Output a skip message as JSON.

Signature

parameter reason String

The reason for skipping.

parameter identity Identity, nil

The identity where the skip occurred.

Implementation

def skip(reason, identity)
	inform(reason.to_s, identity)
end

def inform(message, identity)

Output an informational message as JSON.

Signature

parameter message String, Object

The message to output.

parameter identity Identity, nil

The identity where the message was generated.

Implementation

def inform(message, identity)
	unless message.is_a?(String)
		message = message.inspect
	end
	
	@io.puts(JSON.generate({
		inform: @identity,
		message: {
			text: message,
			location: identity&.to_location,
		}
	}))
	
	@io.flush
end