@@ -2,93 +2,108 @@ import fs from "fs/promises"
22import path from "path"
33import { fileURLToPath } from "url"
44import z from "zod"
5- import { Cause , Context , Effect , Layer , Queue , Stream } from "effect"
5+ import { Cause , Context , Effect , Layer , Queue , Schema , Stream } from "effect"
66import { ripgrep } from "ripgrep"
77import { makeRuntime } from "@/effect/run-service"
88import { Filesystem } from "@/util/filesystem"
99import { Log } from "@/util/log"
10+ import { zod } from "@/util/effect-zod"
1011
1112export namespace Ripgrep {
1213 const log = Log . create ( { service : "ripgrep" } )
13-
14- const Stats = z . object ( {
15- elapsed : z . object ( {
16- secs : z . number ( ) ,
17- nanos : z . number ( ) ,
18- human : z . string ( ) ,
14+ const stats = Schema . Struct ( {
15+ elapsed : Schema . Struct ( {
16+ secs : Schema . Number ,
17+ nanos : Schema . Number ,
18+ human : Schema . String ,
1919 } ) ,
20- searches : z . number ( ) ,
21- searches_with_match : z . number ( ) ,
22- bytes_searched : z . number ( ) ,
23- bytes_printed : z . number ( ) ,
24- matched_lines : z . number ( ) ,
25- matches : z . number ( ) ,
20+ searches : Schema . Number ,
21+ searches_with_match : Schema . Number ,
22+ bytes_searched : Schema . Number ,
23+ bytes_printed : Schema . Number ,
24+ matched_lines : Schema . Number ,
25+ matches : Schema . Number ,
2626 } )
2727
28- const Begin = z . object ( {
29- type : z . literal ( "begin" ) ,
30- data : z . object ( {
31- path : z . object ( {
32- text : z . string ( ) ,
28+ const begin = Schema . Struct ( {
29+ type : Schema . Literal ( "begin" ) ,
30+ data : Schema . Struct ( {
31+ path : Schema . Struct ( {
32+ text : Schema . String ,
3333 } ) ,
3434 } ) ,
3535 } )
3636
37- export const Match = z . object ( {
38- type : z . literal ( "match" ) ,
39- data : z . object ( {
40- path : z . object ( {
41- text : z . string ( ) ,
42- } ) ,
43- lines : z . object ( {
44- text : z . string ( ) ,
45- } ) ,
46- line_number : z . number ( ) ,
47- absolute_offset : z . number ( ) ,
48- submatches : z . array (
49- z . object ( {
50- match : z . object ( {
51- text : z . string ( ) ,
37+ const item = Schema . Struct ( {
38+ path : Schema . Struct ( {
39+ text : Schema . String ,
40+ } ) ,
41+ lines : Schema . Struct ( {
42+ text : Schema . String ,
43+ } ) ,
44+ line_number : Schema . Number ,
45+ absolute_offset : Schema . Number ,
46+ submatches : Schema . mutable (
47+ Schema . Array (
48+ Schema . Struct ( {
49+ match : Schema . Struct ( {
50+ text : Schema . String ,
5251 } ) ,
53- start : z . number ( ) ,
54- end : z . number ( ) ,
52+ start : Schema . Number ,
53+ end : Schema . Number ,
5554 } ) ,
5655 ) ,
57- } ) ,
56+ ) ,
57+ } )
58+
59+ const match = Schema . Struct ( {
60+ type : Schema . Literal ( "match" ) ,
61+ data : item ,
5862 } )
5963
60- const End = z . object ( {
61- type : z . literal ( "end" ) ,
62- data : z . object ( {
63- path : z . object ( {
64- text : z . string ( ) ,
64+ const end = Schema . Struct ( {
65+ type : Schema . Literal ( "end" ) ,
66+ data : Schema . Struct ( {
67+ path : Schema . Struct ( {
68+ text : Schema . String ,
6569 } ) ,
66- binary_offset : z . number ( ) . nullable ( ) ,
67- stats : Stats ,
70+ binary_offset : Schema . NullOr ( Schema . Number ) ,
71+ stats,
6872 } ) ,
6973 } )
7074
71- const Summary = z . object ( {
72- type : z . literal ( "summary" ) ,
73- data : z . object ( {
74- elapsed_total : z . object ( {
75- human : z . string ( ) ,
76- nanos : z . number ( ) ,
77- secs : z . number ( ) ,
75+ const summary = Schema . Struct ( {
76+ type : Schema . Literal ( "summary" ) ,
77+ data : Schema . Struct ( {
78+ elapsed_total : Schema . Struct ( {
79+ human : Schema . String ,
80+ nanos : Schema . Number ,
81+ secs : Schema . Number ,
7882 } ) ,
79- stats : Stats ,
83+ stats,
8084 } ) ,
8185 } )
8286
83- const Result = z . union ( [ Begin , Match , End , Summary ] )
87+ const result = Schema . Union ( [ begin , match , end , summary ] )
88+
89+ const decode = Schema . decodeUnknownSync ( Schema . fromJsonString ( result ) )
90+
91+ export const Stats = zod ( stats )
92+ export const Begin = zod ( begin )
93+ export const Item = zod ( item )
94+ export const Match = zod ( match )
95+ export const End = zod ( end )
96+ export const Summary = zod ( summary )
97+ export const Result = zod ( result )
8498
99+ export type Stats = z . infer < typeof Stats >
85100 export type Result = z . infer < typeof Result >
86101 export type Match = z . infer < typeof Match >
87- export type Item = Match [ "data" ]
102+ export type Item = z . infer < typeof Item >
88103 export type Begin = z . infer < typeof Begin >
89104 export type End = z . infer < typeof End >
90105 export type Summary = z . infer < typeof Summary >
91- export type Row = Match [ "data" ]
106+ export type Row = Item
92107
93108 export interface SearchResult {
94109 items : Item [ ]
@@ -264,7 +279,7 @@ export namespace Ripgrep {
264279 . trim ( )
265280 . split ( / \r ? \n / )
266281 . filter ( Boolean )
267- . map ( ( line ) => Result . parse ( JSON . parse ( line ) ) )
282+ . map ( ( line ) => decode ( line ) )
268283 . flatMap ( ( item ) => ( item . type === "match" ? [ row ( item . data ) ] : [ ] ) )
269284 }
270285
0 commit comments