2021-11-23 00:03:59 +03:00
|
|
|
import { TlEntry } from './types'
|
|
|
|
|
|
|
|
function normalizeType(s: string): string {
|
|
|
|
return s
|
2022-06-05 21:36:12 +03:00
|
|
|
.replace(/^bytes/, 'string')
|
2021-11-23 00:03:59 +03:00
|
|
|
.replace(/</g, ' ')
|
|
|
|
.replace(/>/g, '')
|
|
|
|
.replace('int53', 'long')
|
|
|
|
}
|
|
|
|
|
2022-08-29 14:33:11 +03:00
|
|
|
/**
|
|
|
|
* Generate TL definition for a given entry.
|
|
|
|
*
|
|
|
|
* @param entry Entry to generate definition for
|
|
|
|
* @param forIdComputation
|
|
|
|
* Whether to generate definition for constructor ID computation
|
|
|
|
* (it has slightly different syntax, will not contain `true` flags, etc.)
|
|
|
|
*/
|
2021-11-23 00:03:59 +03:00
|
|
|
export function writeTlEntryToString(
|
|
|
|
entry: TlEntry,
|
2023-06-05 03:30:48 +03:00
|
|
|
forIdComputation = false,
|
2021-11-23 00:03:59 +03:00
|
|
|
): string {
|
|
|
|
let str = entry.name
|
|
|
|
|
|
|
|
if (!forIdComputation && entry.id) {
|
|
|
|
str += '#' + entry.id.toString(16)
|
|
|
|
}
|
|
|
|
|
|
|
|
str += ' '
|
|
|
|
|
|
|
|
if (entry.generics) {
|
|
|
|
for (const g of entry.generics) {
|
|
|
|
if (forIdComputation) {
|
|
|
|
str += g.name + ':' + g.type + ' '
|
|
|
|
} else {
|
|
|
|
str += '{' + g.name + ':' + g.type + '} '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const arg of entry.arguments) {
|
|
|
|
if (forIdComputation && arg.predicate && arg.type === 'true') continue
|
|
|
|
|
|
|
|
str += arg.name + ':'
|
|
|
|
|
|
|
|
if (arg.predicate) {
|
|
|
|
str += arg.predicate + '?'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forIdComputation) {
|
|
|
|
str += normalizeType(arg.type) + ' '
|
|
|
|
} else {
|
|
|
|
str += arg.type + ' '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (forIdComputation) {
|
|
|
|
str += '= ' + normalizeType(entry.type)
|
|
|
|
} else {
|
|
|
|
str += '= ' + entry.type + ';'
|
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|