Skip to content

Commit de484ac

Browse files
Apply PR #22327: refactor(file): clean up ripgrep schema decoding
2 parents 292f97e + 3c272cd commit de484ac

2 files changed

Lines changed: 69 additions & 54 deletions

File tree

packages/opencode/src/file/ripgrep.ts

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2,89 +2,104 @@ import fs from "fs/promises"
22
import path from "path"
33
import { fileURLToPath } from "url"
44
import z from "zod"
5-
import { Cause, Context, Effect, Layer, Queue, Stream } from "effect"
5+
import { Cause, Context, Effect, Layer, Queue, Schema, Stream } from "effect"
66
import { ripgrep } from "ripgrep"
77
import { makeRuntime } from "@/effect/run-service"
88
import { Filesystem } from "@/util/filesystem"
99
import { Log } from "@/util/log"
10+
import { zod } from "@/util/effect-zod"
1011

1112
export namespace Ripgrep {
1213
const log = Log.create({ service: "ripgrep" })
1314

14-
const Stats = z.object({
15-
elapsed: z.object({
16-
secs: z.number(),
17-
nanos: z.number(),
18-
human: z.string(),
15+
const statsSchema = Schema.Struct({
16+
elapsed: Schema.Struct({
17+
secs: Schema.Number,
18+
nanos: Schema.Number,
19+
human: Schema.String,
1920
}),
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(),
21+
searches: Schema.Number,
22+
searches_with_match: Schema.Number,
23+
bytes_searched: Schema.Number,
24+
bytes_printed: Schema.Number,
25+
matched_lines: Schema.Number,
26+
matches: Schema.Number,
2627
})
2728

28-
const Begin = z.object({
29-
type: z.literal("begin"),
30-
data: z.object({
31-
path: z.object({
32-
text: z.string(),
29+
const beginSchema = Schema.Struct({
30+
type: Schema.Literal("begin"),
31+
data: Schema.Struct({
32+
path: Schema.Struct({
33+
text: Schema.String,
3334
}),
3435
}),
3536
})
3637

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(),
38+
const itemSchema = Schema.Struct({
39+
path: Schema.Struct({
40+
text: Schema.String,
41+
}),
42+
lines: Schema.Struct({
43+
text: Schema.String,
44+
}),
45+
line_number: Schema.Number,
46+
absolute_offset: Schema.Number,
47+
submatches: Schema.mutable(
48+
Schema.Array(
49+
Schema.Struct({
50+
match: Schema.Struct({
51+
text: Schema.String,
5252
}),
53-
start: z.number(),
54-
end: z.number(),
53+
start: Schema.Number,
54+
end: Schema.Number,
5555
}),
5656
),
57-
}),
57+
),
58+
})
59+
60+
const matchSchema = Schema.Struct({
61+
type: Schema.Literal("match"),
62+
data: itemSchema,
5863
})
5964

60-
const End = z.object({
61-
type: z.literal("end"),
62-
data: z.object({
63-
path: z.object({
64-
text: z.string(),
65+
const endSchema = Schema.Struct({
66+
type: Schema.Literal("end"),
67+
data: Schema.Struct({
68+
path: Schema.Struct({
69+
text: Schema.String,
6570
}),
66-
binary_offset: z.number().nullable(),
67-
stats: Stats,
71+
binary_offset: Schema.NullOr(Schema.Number),
72+
stats: statsSchema,
6873
}),
6974
})
7075

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(),
76+
const summarySchema = Schema.Struct({
77+
type: Schema.Literal("summary"),
78+
data: Schema.Struct({
79+
elapsed_total: Schema.Struct({
80+
human: Schema.String,
81+
nanos: Schema.Number,
82+
secs: Schema.Number,
7883
}),
79-
stats: Stats,
84+
stats: statsSchema,
8085
}),
8186
})
8287

83-
const Result = z.union([Begin, Match, End, Summary])
88+
const resultSchema = Schema.Union([beginSchema, matchSchema, endSchema, summarySchema])
89+
const decode = Schema.decodeUnknownSync(Schema.fromJsonString(resultSchema))
90+
91+
export const Stats = zod(statsSchema)
92+
export const Begin = zod(beginSchema)
93+
export const Item = zod(itemSchema)
94+
export const Match = zod(matchSchema)
95+
export const End = zod(endSchema)
96+
export const Summary = zod(summarySchema)
97+
export const Result = zod(resultSchema)
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>
@@ -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

packages/opencode/src/server/instance/file.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const FileRoutes = lazy(() =>
2222
description: "Matches",
2323
content: {
2424
"application/json": {
25-
schema: resolver(Ripgrep.Match.shape.data.array()),
25+
schema: resolver(Ripgrep.Item.array()),
2626
},
2727
},
2828
},

0 commit comments

Comments
 (0)