← Back to Demos & Examples

17_stdlib_cli_fmt_and_collections.vk

Raw .vk
// examples/17_stdlib_cli_fmt_and_collections.vk
// End-to-end demo of Veko 1.0's pure-`.vk` core standard library (0 capabilities):
// `fmt`, `cli`, `testing`, `math`, `text`, `path`, `seq`, `coll`, and `rand`.

use [
	cli
	coll
	fmt
	m    = math
	path
	rand
	seq
	t    = testing
	text
]

run-demo = fn [] i64 [
	mut suite = t/new '17-stdlib-core'

	// 1. Declarative CLI parsing (`lib/cli.vk`)
	spec = cli/CliSpec {
		name    = 'node-ctl'
		version = '1.0.0'
		summary = 'Veko cluster control CLI'
		flags   = [
			cli/FlagSpec { name = 'port', short = 'p', kind = .i64, default_val = 8443, help = 'Listen port' }
			cli/FlagSpec { name = 'verbose', short = 'v', kind = .bool, default_val = false, help = 'Verbose logs' }
			cli/FlagSpec { name = 'tag', short = 't', kind = .list, help = 'Repeatable label' }
		]
		commands = [
			cli/CmdSpec { name = 'deploy', summary = 'Deploy a service bundle', min_args = 1 }
		]
	}
	parsed = cli/parse spec ['deploy' '-v' '--port=9443' '-t' 'eu' '-t' 'prod' 'edge-gw.vkp']
	_ = t/eq mut suite parsed.command 'deploy' 'cli subcommand'
	_ = t/eq mut suite (cli/flag-i64 parsed 'port' 0) 9443 'cli port flag'
	_ = t/ok mut suite (cli/flag-bool parsed 'verbose') 'cli verbose flag'
	_ = t/eq mut suite (len (cli/flag-list parsed 'tag')) 2 'cli repeatable list flag'
	_ = t/eq mut suite parsed.args[0] 'edge-gw.vkp' 'cli positional arg'
	println (cli/render-help spec)
	println ''

	// 2. Formatting, units, radix, hexdump, & ASCII tables (`lib/fmt.vk`)
	banner = fmt/template 'Deploying {app} ({size}) in {dur}' {
		app  = parsed.args[0]
		size = (fmt/size 2621440)
		dur  = (fmt/dur 12500000)
	}
	println banner
	_ = t/eq mut suite banner 'Deploying edge-gw.vkp (2.50 MiB) in 12.50 ms' 'fmt/template + units'
	_ = t/eq mut suite (fmt/int-radix 255 16) 'ff' 'fmt/int-radix hex'
	println (fmt/table ['Subsystem' 'Metric' 'Status'] [
		['math' (fmt/format 'pi={0} gcd={1}' [m/pi (m/gcd 84 36)]) 'OK']
		['text' (text/slugify 'Veko 1.0 Standard Library!') 'OK']
		['path' (path/confine '/srv/veko' 'bundles/edge-gw.vkp') 'OK']
	])

	// 3. Math constants, checked arithmetic, bitops, & IEEE-754 transcendentals (`lib/math.vk`)
	_ = t/eq mut suite (m/add-checked/or m/i64-max 1 [-1]) -1 'math checked overflow'
	_ = t/eq mut suite (m/popcount 0xFF0F) 12 'math popcount'
	_ = t/approx mut suite (m/sqrt 2.0) m/sqrt2 0.000000000001 'math sqrt(2)'
	_ = t/approx mut suite (m/sin (m/pi / 2.0)) 1.0 0.000000000001 'math sin(pi/2)'
	_ = t/approx mut suite (m/ln m/e) 1.0 0.000000000001 'math ln(e)'

	// 4. Text & safe lexical path confinement (`lib/text.vk`, `lib/path.vk`)
	_ = t/eq mut suite (text/slugify 'Hello, Veko 1.0!') 'hello-veko-1-0' 'text slugify'
	_ = t/eq mut suite (text/utf8-len 'Veko') 4 'text utf8-len'
	_ = t/eq mut suite (path/confine/or '/srv/data' '../../etc/passwd' ['blocked']) 'blocked' 'path traversal blocked'

	// 5. Sequence algorithms (`lib/seq.vk`)
	sorted = seq/sort [42 7 19 3 99 12]
	_ = t/eq mut suite sorted [3 7 12 19 42 99] 'seq/sort'
	_ = t/eq mut suite (seq/binary-search sorted 19) 3 'seq/binary-search'
	_ = t/eq mut suite (seq/fold sorted 0 fn [acc, x] [ acc + x ]) 182 'seq/fold'

	// 6. Collections: Set, Ring, Heap, LRU, Memo (`lib/coll.vk`)
	s_a = coll/set-from ['api' 'auth' 'db']
	s_b = coll/set-from ['db' 'edge']
	_ = t/eq mut suite (coll/set-len (coll/set-union s_a s_b)) 4 'set union'
	_ = t/ok mut suite (coll/set-has? (coll/set-intersect s_a s_b) 'db') 'set intersect'

	mut ring = coll/ring-new 3
	_ = coll/ring-push mut ring 'first'
	_ = coll/ring-push mut ring 'second'
	_ = t/eq mut suite (coll/ring-pop mut ring) 'first' 'ring FIFO pop'

	mut pq = coll/heap-new
	_ = coll/heap-push mut pq 50 'low'
	_ = coll/heap-push mut pq 10 'urgent'
	_ = coll/heap-push mut pq 30 'normal'
	_ = t/eq mut suite (coll/heap-pop mut pq) 'urgent' 'min-heap pop'

	mut lru = coll/lru-new 2
	_ = coll/lru-put mut lru 'k1' 100
	_ = coll/lru-put mut lru 'k2' 200
	_ = coll/lru-get mut lru 'k1'
	_ = coll/lru-put mut lru 'k3' 300
	_ = t/ok mut suite ((not (coll/lru-has? lru 'k2')) and (coll/lru-has? lru 'k1')) 'lru eviction'

	// 7. Deterministic PRNG & CSPRNG UUID v4 (`lib/rand.vk`)
	mut rng = rand/seed 2026
	mut deck = [1 2 3 4 5 6]
	_ = rand/shuffle mut rng mut deck
	uuid = rand/uuid-v4
	_ = t/eq mut suite (len uuid) 36 'uuid-v4 length'
	println (fmt/format 'Generated UUID v4: {0}' [uuid])

	t/report suite
]

res = run-demo
if err? res [
	println (mold res)
	res
] else [
	0
]