Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-prism

Source of release 0.6.1

0.6.1 / tests / consumer / main.luc

from luce_prism.prism import Document, Value, Encoding, DType

pub func main(arguments: list[str]) -> int!:
    let document = Document()
    document.add("/scene", "group")
    document.set("/scene", "answer", Value.integer(42))
    document.set("/scene", "name", Value.text("Luce Prism"))
    document.set("/scene", "position", Value.numbers([3], [1.0, 2.0, 3.0], DType.float32))
    let pixels = b"\xff\x00\x00\xff\x00\xff\x00\x80"
    document.set("/scene", "pixels", Value.array(DType.uint8, [1, 2, 4], pixels))
    for encoding in [Encoding.text, Encoding.binary, Encoding.package]:
        let copy = Document.decode(document.encode(encoding).bytes())
        let image = copy.get("/scene", "pixels")
        assert(image.dtype() == DType.uint8)
        assert(image.rank() == 3)
        assert(image.dimension(0) == 1 and image.dimension(1) == 2 and image.dimension(2) == 4)
        assert(image.bytes() == pixels)
    let encoded = document.encode(Encoding.binary, true)
    let decoded = Document.decode(encoded.bytes())
    assert(decoded.get("/scene", "answer").integer_at() == 42)
    assert(decoded.get("/scene", "name").text_at() == "Luce Prism")
    assert(decoded.get("/scene", "position").number_at(2) == 3.0)
    assert(decoded.get("/scene", "pixels").bytes() == pixels)
    let text = decoded.encode(Encoding.text).text()
    assert(Document.parse(text).element_count() == 1)
    decoded.rename("/scene", "/world")
    assert(decoded.contains("/world"))
    decoded.remove("/world")
    assert(decoded.element_count() == 0)
    print("PASS high-level Luce Prism consumer")
    return 0