mtcute/packages/tl/scripts/schema.ts
teidesu ec736f8590 some changes
i've been wanting to name a commit like this for my entire life, lol. seriously though, a lot has changed:
 - extracted TL-related stuff to `@mtcute/tl-utils` and `@mtcute/tl-runtime`, rewrote codegen in TS
 - updated to layer 134, moved to int64 identifiers
 - rewritten networking (mtproto), rewritten updates handling
 - *lots* of refactoring

 still a very early version though, there are a lot of improvements to be made, but at least it runs, lol

 also tl-reference will not be updated anytime soon because i want to rewrite it
2021-11-23 00:03:59 +03:00

54 lines
1.2 KiB
TypeScript

import { TlEntry, TlFullSchema } from '@mtcute/tl-utils/src/types'
import { parseFullTlSchema } from '@mtcute/tl-utils/src/schema'
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>
}
export function packTlSchema(schema: TlFullSchema, layer: number): TlPackedSchema {
const ret: TlPackedSchema = {
l: layer,
e: schema.entries,
u: {}
}
for (const name in schema.unions) {
if (!schema.unions.hasOwnProperty(name)) continue
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 (!schema.u.hasOwnProperty(name)) continue
if (!res.unions[name]) continue
res.unions[name].comment = schema.u[name]
}
return [res, schema.l]
}