@zio.dev/zio-blocks 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.md ADDED
@@ -0,0 +1,426 @@
1
+ ---
2
+ id: index
3
+ title: "ZIO Blocks"
4
+ ---
5
+
6
+ **Modular, zero-dependency building blocks for modern Scala applications.**
7
+
8
+ @PROJECT_BADGES@
9
+
10
+ ## What Is ZIO Blocks?
11
+
12
+ ZIO Blocks is a **family of type-safe, modular building blocks** for Scala applications. Each block is a standalone library with zero or minimal dependencies, designed to work with *any* Scala stack—ZIO, Cats Effect, Kyo, Ox, Akka, or plain Scala.
13
+
14
+ The philosophy is simple: **use what you need, nothing more**. Each block is independently useful, cross-platform (JVM, JS), and designed to compose with other blocks or your existing code.
15
+
16
+ ## The Blocks
17
+
18
+ | Block | Description | Status |
19
+ |-------|-------------|--------|
20
+ | **Schema** | Type-safe schemas with automatic codec derivation | ✅ Available |
21
+ | **Chunk** | High-performance immutable indexed sequences | ✅ Available |
22
+ | **Docs** | GitHub Flavored Markdown parsing and rendering | ✅ Available |
23
+ | **TypeId** | Compile-time type identity with rich metadata | ✅ Available |
24
+ | **Context** | Type-indexed heterogeneous collections | ✅ Available |
25
+ | **Streams** | Pull-based streaming primitives | 🚧 In Development |
26
+
27
+ ## Core Principles
28
+
29
+ - **Zero Lock-In**: No dependencies on ZIO, Cats Effect, or any effect system. Use with whatever stack you prefer.
30
+ - **Modular**: Each block is a separate artifact. Import only what you need.
31
+ - **Cross-Platform**: Full support for JVM and Scala.js.
32
+ - **Cross-Version**: Full support for Scala 2.13 and Scala 3.x with source compatibility—adopt Scala 3 on your timeline, not ours.
33
+ - **High Performance**: Optimized implementations that avoid boxing, minimize allocations, and leverage platform-specific features.
34
+ - **Type Safety**: Leverage Scala's type system for correctness without runtime overhead.
35
+
36
+ ---
37
+
38
+ ## Schema
39
+
40
+ The Schema block brings dynamic-language productivity to statically-typed Scala. Define your data types once, and derive codecs, validators, optics, and more automatically.
41
+
42
+ ### The Problem
43
+
44
+ In statically-typed languages, you often maintain separate codec implementations for each data format (JSON, Avro, Protobuf, etc.). Meanwhile, dynamic languages handle data effortlessly:
45
+
46
+ ```javascript
47
+ // JavaScript: one line and done
48
+ const data = await res.json();
49
+ ```
50
+
51
+ In Scala, you'd typically need separate codecs for each format—a significant productivity gap.
52
+
53
+ ### The Solution
54
+
55
+ ZIO Blocks Schema derives everything from a single schema definition:
56
+
57
+ ```scala
58
+ case class Person(name: String, age: Int)
59
+
60
+ object Person {
61
+ implicit val schema: Schema[Person] = Schema.derived
62
+ }
63
+
64
+ // Derive codecs for any format:
65
+ val jsonCodec = Schema[Person].derive(JsonFormat) // JSON
66
+ val avroCodec = Schema[Person].derive(AvroFormat) // Avro
67
+ val toonCodec = Schema[Person].derive(ToonFormat) // TOON (LLM-optimized)
68
+ val msgpackCodec = Schema[Person].derive(MessagePackFormat) // MessagePack
69
+ val thriftCodec = Schema[Person].derive(ThriftFormat) // Thrift
70
+ ```
71
+
72
+ ### Key Features
73
+
74
+ - **Universal Data Formats**: JSON, Avro, TOON (compact LLM-optimized format), MessagePack, Thrift, and BSON, with Protobuf planned.
75
+ - **High Performance**: Register-based design stores primitives directly in byte arrays, enabling zero-allocation serialization.
76
+ - **Reflective Optics**: Type-safe lenses, prisms, and traversals with embedded structural metadata.
77
+ - **Automatic Derivation**: Derive type class instances for any type with a schema.
78
+
79
+ ### Installation
80
+
81
+ ```scala
82
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema" % "@VERSION@"
83
+
84
+ // Optional format modules:
85
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema-avro" % "@VERSION@"
86
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema-toon" % "@VERSION@"
87
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema-messagepack" % "@VERSION@"
88
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema-thrift" % "@VERSION@"
89
+ libraryDependencies += "dev.zio" %% "zio-blocks-schema-bson" % "@VERSION@"
90
+ ```
91
+
92
+ ### Example: Optics
93
+
94
+ ```scala
95
+ import zio.blocks.schema._
96
+
97
+ case class Address(street: String, city: String)
98
+ case class Person(name: String, age: Int, address: Address)
99
+
100
+ object Person extends CompanionOptics[Person] {
101
+ implicit val schema: Schema[Person] = Schema.derived
102
+
103
+ val name: Lens[Person, String] = $(_.name)
104
+ val age: Lens[Person, Int] = $(_.age)
105
+ val streetName: Lens[Person, String] = $(_.address.street)
106
+ }
107
+
108
+ val person = Person("Alice", 30, Address("123 Main St", "Springfield"))
109
+ val updated = Person.age.replace(person, 31)
110
+ ```
111
+
112
+ ---
113
+
114
+ ## Chunk
115
+
116
+ A high-performance, immutable indexed sequence optimized for the patterns common in streaming, parsing, and data processing. Think of it as `Vector` but faster for the operations that matter most.
117
+
118
+ ### Why Chunk?
119
+
120
+ Standard library collections make trade-offs that aren't ideal for streaming and binary data processing:
121
+
122
+ - `Vector` is general-purpose but not optimized for concatenation patterns
123
+ - `Array` is mutable and boxes primitives when used generically
124
+ - `List` has O(n) random access
125
+
126
+ Chunk is designed for:
127
+
128
+ - **Fast concatenation** via balanced trees (Conc-Trees)
129
+ - **Zero-boxing** for primitive types with specialized builders
130
+ - **Efficient slicing** without copying
131
+ - **Seamless interop** with `ByteBuffer`, `Array`, and standard collections
132
+
133
+ ### Key Features
134
+
135
+ - **Specialized Builders**: Dedicated builders for `Byte`, `Int`, `Long`, `Double`, etc. avoid boxing overhead.
136
+ - **Balanced Concatenation**: Based on Conc-Trees for O(log n) concatenation while maintaining O(1) indexed access.
137
+ - **Bit Operations**: First-class support for bit-level operations, bit chunks backed by `Byte`, `Int`, or `Long` arrays.
138
+ - **NonEmptyChunk**: A statically-guaranteed non-empty variant for APIs that require at least one element.
139
+ - **Full Scala Collection Integration**: Implements `IndexedSeq` for seamless interop.
140
+
141
+ ### Installation
142
+
143
+ ```scala
144
+ libraryDependencies += "dev.zio" %% "zio-blocks-chunk" % "@VERSION@"
145
+ ```
146
+
147
+ ### Example
148
+
149
+ ```scala
150
+ import zio.blocks.chunk._
151
+
152
+ // Create chunks
153
+ val bytes = Chunk[Byte](1, 2, 3, 4, 5)
154
+ val moreBytes = Chunk.fromArray(Array[Byte](6, 7, 8))
155
+
156
+ // Efficient concatenation (O(log n))
157
+ val combined = bytes ++ moreBytes
158
+
159
+ // Zero-copy slicing
160
+ val slice = combined.slice(2, 6)
161
+
162
+ // Bit operations
163
+ val bits = bytes.asBitsByte
164
+ val masked = bits & Chunk.fill(bits.length)(true)
165
+
166
+ // NonEmptyChunk for type-safe non-emptiness
167
+ val nonEmpty = NonEmptyChunk(1, 2, 3)
168
+ val head: Int = nonEmpty.head // Always safe, no Option needed
169
+ ```
170
+
171
+ ---
172
+
173
+ ## Docs
174
+
175
+ A zero-dependency GitHub Flavored Markdown library for parsing, rendering, and programmatic construction of Markdown documents.
176
+
177
+ ### Why Docs?
178
+
179
+ Generating documentation, README files, or any Markdown content programmatically is common but error-prone with string concatenation. Docs provides:
180
+
181
+ - **Type-safe AST**: Build Markdown documents with compile-time guarantees
182
+ - **Compile-time validation**: The `md"..."` interpolator validates syntax at compile time
183
+ - **Multiple renderers**: Output to Markdown, HTML, or ANSI terminal
184
+ - **Round-trip parsing**: Parse Markdown to AST and render back to Markdown
185
+
186
+ ### Key Features
187
+
188
+ - **GFM Compliant**: Tables, strikethrough, autolinks, task lists, fenced code blocks
189
+ - **Zero Dependencies**: Only depends on zio-blocks-chunk
190
+ - **Cross-Platform**: Full support for JVM and Scala.js
191
+ - **Type-Safe Interpolator**: `md"# Hello $name"` with compile-time validation
192
+ - **Multiple Renderers**: Markdown, HTML (full document or fragment), ANSI terminal
193
+
194
+ ### Installation
195
+
196
+ ```scala
197
+ libraryDependencies += "dev.zio" %% "zio-blocks-docs" % "@VERSION@"
198
+ ```
199
+
200
+ ### Example
201
+
202
+ ```scala
203
+ import zio.blocks.docs._
204
+
205
+ // Parse Markdown
206
+ val doc = Parser.parse("# Hello\n\nThis is **bold** text.")
207
+ // Right(Doc(Chunk(Heading(H1, "Hello"), Paragraph(...))))
208
+
209
+ // Render to HTML
210
+ val html = doc.map(_.toHtml)
211
+ // Full HTML5 document with <html>, <head>, <body>
212
+
213
+ // Render to HTML fragment (just the content)
214
+ val fragment = doc.map(_.toHtmlFragment)
215
+ // "<h1>Hello</h1><p>This is <strong>bold</strong> text.</p>"
216
+
217
+ // Render to terminal with ANSI colors
218
+ val terminal = doc.map(_.toTerminal)
219
+
220
+ // Use the type-safe interpolator
221
+ val name = "World"
222
+ val greeting = md"# Hello $name"
223
+ // Doc containing: Heading(H1, Chunk(Text("Hello World")))
224
+
225
+ // Build documents programmatically
226
+ import zio.blocks.chunk.Chunk
227
+
228
+ val manual = Doc(Chunk(
229
+ Block.Heading(HeadingLevel.H1, Chunk(Inline.Text("API Reference"))),
230
+ Block.Paragraph(Chunk(
231
+ Inline.Text("See "),
232
+ Inline.Link(Chunk(Inline.Text("docs")), "/docs", None),
233
+ Inline.Text(" for details.")
234
+ ))
235
+ ))
236
+
237
+ // Render back to Markdown
238
+ val markdown = Renderer.render(manual)
239
+ ```
240
+
241
+ ### Supported GFM Features
242
+
243
+ | Feature | Supported |
244
+ |---------|-----------|
245
+ | Headings (ATX) | ✅ |
246
+ | Paragraphs | ✅ |
247
+ | Emphasis/Strong | ✅ |
248
+ | Code (inline & fenced) | ✅ |
249
+ | Links & Images | ✅ |
250
+ | Lists (bullet, ordered, task) | ✅ |
251
+ | Blockquotes | ✅ |
252
+ | Tables | ✅ |
253
+ | Strikethrough | ✅ |
254
+ | Autolinks | ✅ |
255
+ | Hard/Soft breaks | ✅ |
256
+ | HTML (passthrough) | ✅ |
257
+
258
+ ### Limitations
259
+
260
+ - **No frontmatter**: YAML/TOML headers are not parsed
261
+ - **No HTML entity decoding**: `&amp;` stays as-is
262
+ - **No footnotes**: GFM footnote extension not supported
263
+ - **No emoji shortcodes**: `:smile:` not converted to emoji
264
+
265
+ ---
266
+
267
+ ## TypeId
268
+
269
+ Compile-time type identity with rich metadata. TypeId captures comprehensive information about Scala types including name, owner, type parameters, variance, parent types, and annotations.
270
+
271
+ ### Key Features
272
+
273
+ - **Rich Metadata**: Captures type name, owner, kind (class/trait/object/enum), parent types, and annotations
274
+ - **Higher-Kinded Support**: Works with proper types and type constructors via `AnyKind`
275
+ - **Subtype Checking**: Runtime subtype/supertype relationship checks using compile-time extracted information
276
+ - **Cross-Platform**: Works identically on JVM and Scala.js
277
+
278
+ ### Installation
279
+
280
+ ```scala
281
+ libraryDependencies += "dev.zio" %% "zio-blocks-typeid" % "@VERSION@"
282
+ ```
283
+
284
+ ### Example
285
+
286
+ ```scala
287
+ import zio.blocks.typeid._
288
+
289
+ // Get TypeId for any type
290
+ val listId = TypeId.of[List[Int]]
291
+ println(listId.name) // "List"
292
+ println(listId.fullName) // "scala.collection.immutable.List"
293
+ println(listId.arity) // 1 (type constructor)
294
+
295
+ // Check type relationships
296
+ trait Animal
297
+ case class Dog(name: String) extends Animal
298
+
299
+ val dogId = TypeId.of[Dog]
300
+ val animalId = TypeId.of[Animal]
301
+ dogId.isSubtypeOf(animalId) // true
302
+
303
+ // Access structural information
304
+ dogId.isCaseClass // true
305
+ dogId.isSealed // false
306
+ ```
307
+
308
+ ---
309
+
310
+ ## Context
311
+
312
+ A type-indexed heterogeneous collection that stores values by their types with compile-time type safety.
313
+
314
+ ### Key Features
315
+
316
+ - **Type-Safe Lookup**: Retrieve values by type with compile-time guarantees
317
+ - **Covariant**: `Context[Specific]` is a subtype of `Context[General]`
318
+ - **Subtype Matching**: Lookup by supertype finds matching subtypes
319
+ - **Cached Access**: O(1) subsequent lookups after first retrieval
320
+
321
+ ### Installation
322
+
323
+ ```scala
324
+ libraryDependencies += "dev.zio" %% "zio-blocks-context" % "@VERSION@"
325
+ ```
326
+
327
+ ### Example
328
+
329
+ ```scala
330
+ import zio.blocks.context._
331
+
332
+ case class Config(debug: Boolean)
333
+ case class Metrics(count: Int)
334
+
335
+ // Create a context with multiple values
336
+ val ctx: Context[Config & Metrics] = Context(
337
+ Config(debug = true),
338
+ Metrics(count = 42)
339
+ )
340
+
341
+ // Retrieve values by type
342
+ val config: Config = ctx.get[Config]
343
+ val metrics: Metrics = ctx.get[Metrics]
344
+
345
+ // Add or update values
346
+ val updated = ctx.update[Metrics](m => m.copy(count = m.count + 1))
347
+
348
+ // Combine contexts
349
+ val ctx1 = Context(Config(false))
350
+ val ctx2 = Context(Metrics(0))
351
+ val merged: Context[Config & Metrics] = ctx1 ++ ctx2
352
+ ```
353
+
354
+ ---
355
+
356
+ ## Streams (In Development)
357
+
358
+ A pull-based streaming library for composable, backpressure-aware data processing.
359
+
360
+ ```scala
361
+ import zio.blocks.streams._
362
+
363
+ // Coming soon: efficient pull-based streams
364
+ // that compose with any effect system
365
+ ```
366
+
367
+ ---
368
+
369
+ ## Compatibility
370
+
371
+ ZIO Blocks works with any Scala stack:
372
+
373
+ | Stack | Compatible |
374
+ |-------|------------|
375
+ | ZIO 2.x | ✅ |
376
+ | Cats Effect 3.x | ✅ |
377
+ | Kyo | ✅ |
378
+ | Ox | ✅ |
379
+ | Akka | ✅ |
380
+ | Plain Scala | ✅ |
381
+
382
+ Each block has zero dependencies on effect systems. Use the blocks directly, or integrate them with your effect system of choice.
383
+
384
+ ## Scala & Platform Support
385
+
386
+ ZIO Blocks supports **Scala 2.13** and **Scala 3.x** with full source compatibility. Write your code once and compile it against either version—migrate to Scala 3 when your team is ready, not when your dependencies force you.
387
+
388
+ | Platform | Schema | Chunk | Docs | TypeId | Context | Streams |
389
+ |----------|--------|-------|------|--------|---------|---------|
390
+ | JVM | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
391
+ | Scala.js | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
392
+
393
+ ## Documentation
394
+
395
+ ### Core Schema Concepts
396
+
397
+ - [Schema](./reference/schema.md) - Core schema definitions and derivation
398
+ - [Reflect](./reference/reflect.md) - Structural reflection API
399
+ - [Binding](./reference/binding.md) - Runtime constructors and deconstructors
400
+ - [Registers](./reference/registers.md) - Register-based primitive storage
401
+
402
+ ### Optics & Navigation
403
+
404
+ - [Optics](./reference/optics.md) - Lenses, prisms, and traversals
405
+ - [Path Interpolator](./path-interpolator.md) - Type-safe path construction
406
+ - [DynamicValue](./reference/dynamic-value.md) - Schema-less dynamic values
407
+
408
+ ### Serialization
409
+
410
+ - [JSON](./reference/json.md) - JSON codec and parsing
411
+ - [JSON Schema](./reference/json-schema.md) - JSON Schema generation and validation
412
+ - [Formats](./reference/formats.md) - Avro, TOON, MessagePack, BSON, Thrift
413
+ - [Extension Syntax](./reference/syntax.md) - `.toJson`, `.fromJson`, and more
414
+
415
+ ### Data Operations
416
+
417
+ - [Patching](./reference/patch.md) - Serializable data transformations
418
+ - [Validation](./reference/validation.md) - Data validation and error handling
419
+ - [Schema Evolution](./reference/schema-evolution.md) - Migration and compatibility
420
+
421
+ ### Other Blocks
422
+
423
+ - [Chunk](./reference/chunk.md) - High-performance immutable sequences
424
+ - [TypeId](./reference/typeid.md) - Type identity and metadata
425
+ - [Context](./reference/context.md) - Type-indexed heterogeneous collections
426
+ - [Docs (Markdown)](./reference/docs.md) - Markdown parsing and rendering
package/package.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@zio.dev/zio-blocks",
3
+ "version": "0.0.1",
4
+ "description": "ZIO Blocks Documentation",
5
+ "license": "Apache-2.0"
6
+ }