refactor(file): derive ripgrep zod from effect schema
This commit is contained in:
parent
75a87ffc5e
commit
573a10e2f4
2 changed files with 65 additions and 83 deletions
|
|
@ -20,117 +20,99 @@ import { text } from "node:stream/consumers"
|
||||||
|
|
||||||
import { ZipReader, BlobReader, BlobWriter } from "@zip.js/zip.js"
|
import { ZipReader, BlobReader, BlobWriter } from "@zip.js/zip.js"
|
||||||
import { Log } from "@/util/log"
|
import { Log } from "@/util/log"
|
||||||
|
import { zod } from "@/util/effect-zod"
|
||||||
|
|
||||||
export namespace Ripgrep {
|
export namespace Ripgrep {
|
||||||
const log = Log.create({ service: "ripgrep" })
|
const log = Log.create({ service: "ripgrep" })
|
||||||
const Stats = z.object({
|
const stats = Schema.Struct({
|
||||||
elapsed: z.object({
|
elapsed: Schema.Struct({
|
||||||
secs: z.number(),
|
secs: Schema.Number,
|
||||||
nanos: z.number(),
|
nanos: Schema.Number,
|
||||||
human: z.string(),
|
human: Schema.String,
|
||||||
}),
|
}),
|
||||||
searches: z.number(),
|
searches: Schema.Number,
|
||||||
searches_with_match: z.number(),
|
searches_with_match: Schema.Number,
|
||||||
bytes_searched: z.number(),
|
bytes_searched: Schema.Number,
|
||||||
bytes_printed: z.number(),
|
bytes_printed: Schema.Number,
|
||||||
matched_lines: z.number(),
|
matched_lines: Schema.Number,
|
||||||
matches: z.number(),
|
matches: Schema.Number,
|
||||||
})
|
})
|
||||||
|
|
||||||
const Begin = z.object({
|
const begin = Schema.Struct({
|
||||||
type: z.literal("begin"),
|
type: Schema.Literal("begin"),
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
export const Match = z.object({
|
|
||||||
type: z.literal("match"),
|
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
lines: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
line_number: z.number(),
|
|
||||||
absolute_offset: z.number(),
|
|
||||||
submatches: z.array(
|
|
||||||
z.object({
|
|
||||||
match: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
start: z.number(),
|
|
||||||
end: z.number(),
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const End = z.object({
|
|
||||||
type: z.literal("end"),
|
|
||||||
data: z.object({
|
|
||||||
path: z.object({
|
|
||||||
text: z.string(),
|
|
||||||
}),
|
|
||||||
binary_offset: z.number().nullable(),
|
|
||||||
stats: Stats,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const Summary = z.object({
|
|
||||||
type: z.literal("summary"),
|
|
||||||
data: z.object({
|
|
||||||
elapsed_total: z.object({
|
|
||||||
human: z.string(),
|
|
||||||
nanos: z.number(),
|
|
||||||
secs: z.number(),
|
|
||||||
}),
|
|
||||||
stats: Stats,
|
|
||||||
}),
|
|
||||||
})
|
|
||||||
|
|
||||||
const Result = z.union([Begin, Match, End, Summary])
|
|
||||||
|
|
||||||
const Hit = Schema.Struct({
|
|
||||||
type: Schema.Literal("match"),
|
|
||||||
data: Schema.Struct({
|
data: Schema.Struct({
|
||||||
path: Schema.Struct({
|
path: Schema.Struct({
|
||||||
text: Schema.String,
|
text: Schema.String,
|
||||||
}),
|
}),
|
||||||
lines: Schema.Struct({
|
|
||||||
text: Schema.String,
|
|
||||||
}),
|
|
||||||
line_number: Schema.Number,
|
|
||||||
absolute_offset: Schema.Number,
|
|
||||||
submatches: Schema.mutable(
|
|
||||||
Schema.Array(
|
|
||||||
Schema.Struct({
|
|
||||||
match: Schema.Struct({
|
|
||||||
text: Schema.String,
|
|
||||||
}),
|
|
||||||
start: Schema.Number,
|
|
||||||
end: Schema.Number,
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
const Row = Schema.Union([
|
const item = Schema.Struct({
|
||||||
Schema.Struct({ type: Schema.Literal("begin"), data: Schema.Unknown }),
|
path: Schema.Struct({
|
||||||
Hit,
|
text: Schema.String,
|
||||||
Schema.Struct({ type: Schema.Literal("end"), data: Schema.Unknown }),
|
}),
|
||||||
Schema.Struct({ type: Schema.Literal("summary"), data: Schema.Unknown }),
|
lines: Schema.Struct({
|
||||||
])
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
line_number: Schema.Number,
|
||||||
|
absolute_offset: Schema.Number,
|
||||||
|
submatches: Schema.mutable(
|
||||||
|
Schema.Array(
|
||||||
|
Schema.Struct({
|
||||||
|
match: Schema.Struct({
|
||||||
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
start: Schema.Number,
|
||||||
|
end: Schema.Number,
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
const decode = Schema.decodeUnknownSync(Schema.fromJsonString(Row))
|
const match = Schema.Struct({
|
||||||
|
type: Schema.Literal("match"),
|
||||||
|
data: item,
|
||||||
|
})
|
||||||
|
|
||||||
|
const end = Schema.Struct({
|
||||||
|
type: Schema.Literal("end"),
|
||||||
|
data: Schema.Struct({
|
||||||
|
path: Schema.Struct({
|
||||||
|
text: Schema.String,
|
||||||
|
}),
|
||||||
|
binary_offset: Schema.NullOr(Schema.Number),
|
||||||
|
stats,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const summary = Schema.Struct({
|
||||||
|
type: Schema.Literal("summary"),
|
||||||
|
data: Schema.Struct({
|
||||||
|
elapsed_total: Schema.Struct({
|
||||||
|
human: Schema.String,
|
||||||
|
nanos: Schema.Number,
|
||||||
|
secs: Schema.Number,
|
||||||
|
}),
|
||||||
|
stats,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const row = Schema.Union([begin, match, end, summary])
|
||||||
|
|
||||||
|
const decode = Schema.decodeUnknownSync(Schema.fromJsonString(row))
|
||||||
|
|
||||||
|
export const Stats = zod(stats)
|
||||||
|
export const Begin = zod(begin)
|
||||||
|
export const Item = zod(item)
|
||||||
|
export const Match = zod(match)
|
||||||
|
export const End = zod(end)
|
||||||
|
export const Summary = zod(summary)
|
||||||
|
export const Result = zod(row)
|
||||||
|
|
||||||
|
export type Stats = z.infer<typeof Stats>
|
||||||
export type Result = z.infer<typeof Result>
|
export type Result = z.infer<typeof Result>
|
||||||
export type Match = z.infer<typeof Match>
|
export type Match = z.infer<typeof Match>
|
||||||
export type Item = Match["data"]
|
export type Item = z.infer<typeof Item>
|
||||||
export type Begin = z.infer<typeof Begin>
|
export type Begin = z.infer<typeof Begin>
|
||||||
export type End = z.infer<typeof End>
|
export type End = z.infer<typeof End>
|
||||||
export type Summary = z.infer<typeof Summary>
|
export type Summary = z.infer<typeof Summary>
|
||||||
|
|
@ -434,7 +416,7 @@ export namespace Ripgrep {
|
||||||
catch: (cause) => new Error("invalid ripgrep output", { cause }),
|
catch: (cause) => new Error("invalid ripgrep output", { cause }),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
Stream.filter((row): row is Schema.Schema.Type<typeof Hit> => row.type === "match"),
|
Stream.filter((row): row is Schema.Schema.Type<typeof match> => row.type === "match"),
|
||||||
Stream.map((row): Item => row.data),
|
Stream.map((row): Item => row.data),
|
||||||
Stream.runCollect,
|
Stream.runCollect,
|
||||||
Effect.map((chunk) => [...chunk]),
|
Effect.map((chunk) => [...chunk]),
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ export const FileRoutes = lazy(() =>
|
||||||
description: "Matches",
|
description: "Matches",
|
||||||
content: {
|
content: {
|
||||||
"application/json": {
|
"application/json": {
|
||||||
schema: resolver(Ripgrep.Match.shape.data.array()),
|
schema: resolver(Ripgrep.Item.array()),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue