ion

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 5 Imported by: 0

README

ion

ion is a cross-platform serialization protocol

Documentation

Index

Constants

View Source
const (
	// FloatTolerance is the tolerance for float matching.
	FloatTolerance = 1e-14
	// FloatPrecision is the number of decimal places for formatting.
	FloatPrecision = 6
)
View Source
const (
	StorageTypeWord       = 0
	StorageTypeFloat      = 1
	StorageTypeWordArray  = 2
	StorageTypeFloatArray = 3
	StorageTypeMixedArray = 4
	StorageTypeAny        = 255
)

StorageType constants define the wire format type.

View Source
const (
	NounTypeInteger          = 0
	NounTypeReal             = 1
	NounTypeCharacter        = 2
	NounTypeString           = 3
	NounTypeList             = 4
	NounTypeDictionary       = 5
	NounTypeBuiltinSymbol    = 6
	NounTypeBuiltinNilad     = 7
	NounTypeBuiltinMonad     = 8
	NounTypeBuiltinDyad      = 9
	NounTypeBuiltinTriad     = 10
	NounTypeMonadicAdverb    = 11
	NounTypeDyadicAdverb     = 12
	NounTypeTriadicAdverb    = 13
	NounTypeUserSymbol       = 14
	NounTypeUserNilad        = 15
	NounTypeUserMonad        = 16
	NounTypeUserDyad         = 17
	NounTypeUserTriad        = 18
	NounTypeError            = 19
	NounTypeExpression       = 20
	NounTypeType             = 21
	NounTypeConditional      = 22
	NounTypeQuotedSymbol     = 23
	NounTypeEffectType       = 24
	NounTypeResource         = 25
	NounTypeContingency      = 26
	NounTypeSignal           = 27
	NounTypeSequence         = 28
	NounTypeDependency       = 29
	NounTypeEffectExpression = 30
	NounTypeLens             = 31
	NounTypeEffectChain      = 32
	NounTypeNiladicEffect    = 33
	NounTypeMonadicEffect    = 34
	NounTypeDyadicEffect     = 35
	NounTypeTriadicEffect    = 36
	NounTypeConjunction      = 37
	NounTypeData             = 38
	NounTypeSymbolDefinition = 39
	NounTypeBindings         = 40
	NounTypeRange            = 41
	NounTypeAny              = 255
)

NounType constants define the semantic type of the value.

View Source
const (
	SymbolTypeI         = 0
	SymbolTypeX         = 1
	SymbolTypeY         = 2
	SymbolTypeZ         = 3
	SymbolTypeF         = 4
	SymbolTypeCausing   = 5
	SymbolTypeUndefined = 6 // Must always be last
)

SymbolType constants for built-in symbols.

Variables

View Source
var (
	ErrEmptyData        = errors.New("empty data")
	ErrInsufficientData = errors.New("insufficient data")
	ErrInvalidLength    = errors.New("invalid length")
)
View Source
var (
	ErrUnknownStorageType = errors.New("unknown storage type")
	ErrTypeMismatch       = errors.New("type mismatch")
)

Functions

func ExpandFloats

func ExpandFloats(data []byte) ([]float32, []byte, error)

ExpandFloats decodes an array of squeezed floats from a byte slice.

func ExpandInts

func ExpandInts(data []byte) ([]int32, []byte, error)

ExpandInts decodes an array of squeezed integers from a byte slice.

func FloatArrayToBytes

func FloatArrayToBytes(s Storage) ([]byte, error)

FloatArrayToBytes encodes a FloatArray to bytes (without type header). Format: [squeezed_length] [squeezed_float]...

func FloatArrayToConn

func FloatArrayToConn(w io.Writer, s Storage) error

FloatArrayToConn writes a FloatArray to a connection (with type header).

func FloatToBytes

func FloatToBytes(s Storage) ([]byte, error)

FloatToBytes encodes a Float to bytes (without type header).

func FloatToConn

func FloatToConn(w io.Writer, s Storage) error

FloatToConn writes a Float to a connection (with type header).

func MixedArrayToBytes

func MixedArrayToBytes(s Storage) ([]byte, error)

MixedArrayToBytes encodes a MixedArray to bytes (without type header). Format: [squeezed_length] ([storage_type] [object_type] [data])...

func MixedArrayToConn

func MixedArrayToConn(w io.Writer, s Storage) error

MixedArrayToConn writes a MixedArray to a connection (with type header).

func NounToBytes

func NounToBytes(s Storage) ([]byte, error)

NounToBytes encodes any Storage value to bytes (without type header). This is the default implementation used by MixedArray.

func NounToConn

func NounToConn(w io.Writer, s Storage) error

NounToConn writes any Storage value to a connection (with type header). This is the default implementation used by MixedArray.

func SqueezeBigInt

func SqueezeBigInt(value []int32) []byte

SqueezeBigInt encodes a big integer (multi-limb) value. Format: [length_byte] [data_bytes...] The input slice has sign as first element (0=positive, 1=negative), followed by the limbs in order.

func SqueezeFloat32

func SqueezeFloat32(value float32) []byte

SqueezeFloat32 encodes a float32 value.

func SqueezeFloat64

func SqueezeFloat64(value float64) []byte

SqueezeFloat64 encodes a float64 value.

func SqueezeFloating

func SqueezeFloating(value Floating) []byte

SqueezeFloating encodes a Floating value in IEEE format with length prefix. Format: [length_byte] [ieee_bytes...] - length 0: value is 0.0 - length 4: float32 (big-endian) - length 8: float64 (big-endian)

func SqueezeFloats

func SqueezeFloats(values []float32) []byte

SqueezeFloats encodes an array of floats. Format: [squeezed_length] [squeezed_float]...

func SqueezeInt

func SqueezeInt(value int32) []byte

SqueezeInt encodes an integer in the fewest number of bytes. Format: [length_byte] [data_bytes...] - Length byte: bits 0-6 = length, bit 7 = sign (1 = negative) - Data bytes: big-endian representation of absolute value Special case: 0 encodes as single byte [0x00]

func SqueezeInts

func SqueezeInts(values []int32) []byte

SqueezeInts encodes an array of integers. Format: [squeezed_length] [squeezed_int]...

func SqueezeVarint

func SqueezeVarint(v Varint) []byte

SqueezeVarint encodes a Varint value.

func WordArrayToBytes

func WordArrayToBytes(s Storage) ([]byte, error)

WordArrayToBytes encodes a WordArray to bytes (without type header). Format: [squeezed_length] [squeezed_int]...

func WordArrayToConn

func WordArrayToConn(w io.Writer, s Storage) error

WordArrayToConn writes a WordArray to a connection (with type header).

func WordToBytes

func WordToBytes(s Storage) ([]byte, error)

WordToBytes encodes a Word to bytes (without type header).

func WordToConn

func WordToConn(w io.Writer, s Storage) error

WordToConn writes a Word to a connection (with type header).

Types

type Conn

type Conn struct {
	// contains filtered or unexported fields
}

Conn wraps an io.ReadWriter for ion protocol communication. It provides thread-safe read and write operations.

func NewConn

func NewConn(rw io.ReadWriter) *Conn

NewConn creates a new ion connection from an io.ReadWriter. Typically used with a serial port connection.

func (*Conn) ReadFloat

func (c *Conn) ReadFloat() (float32, error)

ReadFloat reads a float32 value. Thread-safe.

func (*Conn) ReadFloat64

func (c *Conn) ReadFloat64() (float64, error)

ReadFloat64 reads a float64 value. Thread-safe.

func (*Conn) ReadFloatArray

func (c *Conn) ReadFloatArray() ([]float32, error)

ReadFloatArray reads a float array. Thread-safe.

func (*Conn) ReadMixedArray

func (c *Conn) ReadMixedArray() ([]Storage, error)

ReadMixedArray reads a mixed array. Thread-safe.

func (*Conn) ReadStorage

func (c *Conn) ReadStorage() (Storage, error)

ReadStorage reads a Storage value. Thread-safe.

func (*Conn) ReadWord

func (c *Conn) ReadWord() (int32, error)

ReadWord reads an integer value using the ion protocol. Thread-safe.

func (*Conn) ReadWordArray

func (c *Conn) ReadWordArray() ([]int32, error)

ReadWordArray reads an integer array. Thread-safe.

func (*Conn) ReadWordVarint

func (c *Conn) ReadWordVarint() (Varint, error)

ReadWordVarint reads an integer that may be a BigInt. Thread-safe.

func (*Conn) SendTXOff

func (c *Conn) SendTXOff() error

SendTXOff sends the TX_OFF signal (value 0) to stop transmission.

func (*Conn) SendTXOn

func (c *Conn) SendTXOn() error

SendTXOn sends the TX_ON signal (value 1) to start transmission.

func (*Conn) TransmitFrequencies

func (c *Conn) TransmitFrequencies(frequencies []int64) error

TransmitFrequencies sends a sequence of WSPR frequencies over the connection. This is the primary method for TX pipeline. Each frequency is sent as an ion Word (integer in centihertz).

func (*Conn) WriteFloat

func (c *Conn) WriteFloat(value float32) error

WriteFloat writes a float32 value. Thread-safe.

func (*Conn) WriteFloat64

func (c *Conn) WriteFloat64(value float64) error

WriteFloat64 writes a float64 value. Thread-safe.

func (*Conn) WriteFloatArray

func (c *Conn) WriteFloatArray(values []float32) error

WriteFloatArray writes a float array. Thread-safe.

func (*Conn) WriteMixedArray

func (c *Conn) WriteMixedArray(values []Storage) error

WriteMixedArray writes a mixed array. Thread-safe.

func (*Conn) WriteStorage

func (c *Conn) WriteStorage(s Storage) error

WriteStorage writes a Storage value. Thread-safe.

func (*Conn) WriteWord

func (c *Conn) WriteWord(value int32) error

WriteWord writes an integer value using the ion protocol. Thread-safe.

func (*Conn) WriteWordArray

func (c *Conn) WriteWordArray(values []int32) error

WriteWordArray writes an integer array. Thread-safe.

func (*Conn) WriteWordWithType

func (c *Conn) WriteWordWithType(value int32, objectType int) error

WriteWordWithType writes an integer with a custom NounType. Thread-safe.

func (*Conn) WriteWords

func (c *Conn) WriteWords(values []int32) error

WriteWords writes multiple integer values sequentially. Thread-safe.

type Floating

type Floating struct {
	IsFloat32 bool
	Float32   float32
	Float64   float64
}

Floating represents either a float32 or float64.

func ExpandFloating

func ExpandFloating(data []byte) (Floating, []byte, error)

ExpandFloating decodes a squeezed float from a byte slice. Returns the Floating value and remaining bytes.

func ExpandFloatingFromReader

func ExpandFloatingFromReader(r io.Reader) (Floating, error)

ExpandFloatingFromReader reads and decodes a squeezed float from an io.Reader.

func NewFloat32

func NewFloat32(v float32) Floating

NewFloat32 creates a Floating from a float32.

func NewFloat64

func NewFloat64(v float64) Floating

NewFloat64 creates a Floating from a float64.

func (Floating) AsFloat64

func (f Floating) AsFloat64() float64

AsFloat64 returns the value as float64 regardless of internal representation.

type Storage

type Storage struct {
	ObjectType  int         // NounType - semantic type
	StorageType int         // StorageType - wire format type
	Value       interface{} // The actual value
}

Storage represents an ion value with type information.

func Float

func Float(value float32, objectType ...int) Storage

Float creates a float32 Storage value.

func Float64

func Float64(value float64, objectType ...int) Storage

Float64 creates a float64 Storage value.

func FloatArray

func FloatArray(values []float32, objectType ...int) Storage

FloatArray creates a float array Storage value.

func FloatArrayFromBytes

func FloatArrayFromBytes(data []byte, objectType int) (Storage, error)

FloatArrayFromBytes decodes a FloatArray from bytes (without type header). Format: [squeezed_length] [squeezed_float]...

func FloatArrayFromConn

func FloatArrayFromConn(r io.Reader, objectType int) (Storage, error)

FloatArrayFromConn reads a FloatArray from a connection (without type header). The storageType and objectType have already been read.

func FloatFromBytes

func FloatFromBytes(data []byte, objectType int) (Storage, error)

FloatFromBytes decodes a Float from bytes (without type header).

func FloatFromConn

func FloatFromConn(r io.Reader, objectType int) (Storage, error)

FloatFromConn reads a Float from a connection (without type header). The storageType and objectType have already been read.

func FromBytes

func FromBytes(data []byte, storageType, objectType int) (Storage, error)

FromBytes decodes a Storage value from bytes (without type header).

func Identity

func Identity(s Storage) Storage

Identity returns the input unchanged (used as a default function).

func MakeFloat

func MakeFloat(value float32, objectType int) Storage

MakeFloat creates a Float Storage value from float32.

func MakeFloat64

func MakeFloat64(value float64, objectType int) Storage

MakeFloat64 creates a Float Storage value from float64.

func MakeFloatArray

func MakeFloatArray(values []float32, objectType int) Storage

MakeFloatArray creates a FloatArray Storage value.

func MakeMixedArray

func MakeMixedArray(values []Storage, objectType int) Storage

MakeMixedArray creates a MixedArray Storage value.

func MakeWord

func MakeWord(value int32, objectType int) Storage

MakeWord creates a Word Storage value. This is an alias for Word() to match the C++ API.

func MakeWordArray

func MakeWordArray(values []int32, objectType int) Storage

MakeWordArray creates a WordArray Storage value.

func MixedArray

func MixedArray(values []Storage, objectType ...int) Storage

MixedArray creates a mixed array Storage value.

func MixedArrayFromBytes

func MixedArrayFromBytes(data []byte, objectType int) (Storage, error)

MixedArrayFromBytes decodes a MixedArray from bytes (without type header). Format: [squeezed_length] ([storage_type] [object_type] [data])...

func MixedArrayFromConn

func MixedArrayFromConn(r io.Reader, objectType int) (Storage, error)

MixedArrayFromConn reads a MixedArray from a connection (without type header). The storageType and objectType have already been read.

func Nil

func Nil() Storage

Nil creates an empty list.

func NounFromConn

func NounFromConn(r io.Reader) (Storage, error)

NounFromConn reads any Storage value from a connection (with type header). This is the default implementation used by MixedArray.

func ReadFromConn

func ReadFromConn(r io.Reader) (Storage, error)

ReadFromConn reads a Storage value from an io.Reader.

func Word

func Word(value int32, objectType ...int) Storage

Word creates an integer Storage value.

func WordArray

func WordArray(values []int32, objectType ...int) Storage

WordArray creates an integer array Storage value.

func WordArrayFromBytes

func WordArrayFromBytes(data []byte, objectType int) (Storage, error)

WordArrayFromBytes decodes a WordArray from bytes (without type header). Format: [squeezed_length] [squeezed_int]...

func WordArrayFromConn

func WordArrayFromConn(r io.Reader, objectType int) (Storage, error)

WordArrayFromConn reads a WordArray from a connection (without type header). The storageType and objectType have already been read.

func WordArrayNil

func WordArrayNil() Storage

WordArrayNil creates an empty WordArray.

func WordArrayNil1

func WordArrayNil1(s Storage) Storage

WordArrayNil1 creates an empty WordArray (ignoring input).

func WordArrayNil2

func WordArrayNil2(s1, s2 Storage) Storage

WordArrayNil2 creates an empty WordArray (ignoring inputs).

func WordFromBytes

func WordFromBytes(data []byte, objectType int) (Storage, error)

WordFromBytes decodes a Word from bytes (without type header).

func WordFromConn

func WordFromConn(r io.Reader, objectType int) (Storage, error)

WordFromConn reads a Word from a connection (without type header). The storageType and objectType have already been read.

func WordFromVarint

func WordFromVarint(v Varint, objectType ...int) Storage

WordFromVarint creates a Storage from a Varint. If the Varint is a BigInt, it stores the limbs as []int32.

func (Storage) AsFloat

func (s Storage) AsFloat() (float32, error)

AsFloat returns the value as float32, or error if type mismatch.

func (Storage) AsFloat64

func (s Storage) AsFloat64() (float64, error)

AsFloat64 returns the value as float64, or error if type mismatch.

func (Storage) AsFloats

func (s Storage) AsFloats() ([]float32, error)

AsFloats returns the value as []float32, or error if type mismatch.

func (Storage) AsInt

func (s Storage) AsInt() (int32, error)

AsInt returns the value as int32, or error if type mismatch.

func (Storage) AsInts

func (s Storage) AsInts() ([]int32, error)

AsInts returns the value as []int32, or error if type mismatch.

func (Storage) AsMixed

func (s Storage) AsMixed() ([]Storage, error)

AsMixed returns the value as []Storage, or error if type mismatch.

func (Storage) Equal

func (s Storage) Equal(other Storage) bool

Equal checks if two Storage values are equal.

func (Storage) ToBytes

func (s Storage) ToBytes() ([]byte, error)

ToBytes encodes a Storage value to bytes (without type header).

func (Storage) Truth

func (s Storage) Truth() int

Truth returns the truth value of the Storage (for conditionals). Returns non-zero for truthy values.

func (Storage) WriteToConn

func (s Storage) WriteToConn(w io.Writer) error

WriteToConn writes a Storage value to an io.Writer with type header. Format: [storage_type] [object_type] [data...]

type Varint

type Varint struct {
	IsInt  bool
	Int    int32
	BigInt []int32 // [sign, limb0, limb1, ...] where sign is 0 (positive) or 1 (negative)
}

Varint represents either a regular int32 or a big integer (slice of int32 limbs). For big integers, the first element is the sign (0 = positive, 1 = negative), followed by the limbs in big-endian order.

func ExpandInt

func ExpandInt(data []byte) (Varint, []byte, error)

ExpandInt decodes a squeezed integer from a byte slice. Returns the decoded Varint and remaining bytes. If the value fits in int32, IsInt will be true. Otherwise, BigInt will contain [sign, limbs...].

func ExpandIntFromBytes

func ExpandIntFromBytes(data []byte) Varint

ExpandIntFromBytes expands an integer from raw bytes (no length prefix, no sign). This is used when the bytes are already extracted.

func ExpandIntFromReader

func ExpandIntFromReader(r io.Reader) (Varint, error)

ExpandIntFromReader reads and decodes a squeezed integer from an io.Reader.

func NewVarintBigInt

func NewVarintBigInt(limbs []int32) Varint

NewVarintBigInt creates a Varint from a big integer representation. The first element should be the sign (0 or 1), followed by limbs.

func NewVarintInt

func NewVarintInt(v int32) Varint

NewVarintInt creates a Varint from an int32.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL