mtcute/packages/tl/scripts/schema.ts

53 lines
1.1 KiB
TypeScript
Raw Normal View History

import type { TlEntry, TlFullSchema } from '@mtcute/tl-utils'
import { parseFullTlSchema } from '@mtcute/tl-utils'
// interface TlPackedUnion {
// // name
// n: string
// // comment
// d?: string
// // methods
// m: string[]
// // classes
// c: string[]
// }
export interface TlPackedSchema {
// layer
l: number
// entries
e: TlEntry[]
// unions (only comments)
u: Record<string, string>
}
2023-09-24 01:32:22 +03:00
export function packTlSchema(schema: TlFullSchema, layer: number): TlPackedSchema {
const ret: TlPackedSchema = {
l: layer,
e: schema.entries,
u: {},
}
for (const name in schema.unions) {
const union = schema.unions[name]
if (union.comment) {
ret.u[name] = union.comment
}
}
return ret
}
export function unpackTlSchema(schema: TlPackedSchema): [TlFullSchema, number] {
const res = parseFullTlSchema(schema.e)
for (const name in schema.u) {
if (!res.unions[name]) continue
res.unions[name].comment = schema.u[name]
}
return [res, schema.l]
}