2024-08-13 04:53:07 +03:00
|
|
|
import type { TlEntry } from './types.js'
|
2023-10-16 19:23:53 +03:00
|
|
|
import { stringifyArgumentType } from './utils.js'
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
function normalizeType(s: string): string {
|
|
|
|
return s
|
2023-09-24 19:56:13 +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.)
|
|
|
|
*/
|
2023-09-24 01:32:22 +03:00
|
|
|
export function writeTlEntryToString(entry: TlEntry, forIdComputation = false): string {
|
2021-11-23 00:03:59 +03:00
|
|
|
let str = entry.name
|
|
|
|
|
|
|
|
if (!forIdComputation && entry.id) {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `#${entry.id.toString(16)}`
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
str += ' '
|
|
|
|
|
|
|
|
if (entry.generics) {
|
|
|
|
for (const g of entry.generics) {
|
|
|
|
if (forIdComputation) {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `${g.name}:${g.type} `
|
2021-11-23 00:03:59 +03:00
|
|
|
} else {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `{${g.name}:${g.type}} `
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const arg of entry.arguments) {
|
2023-09-24 01:32:22 +03:00
|
|
|
if (forIdComputation && arg.typeModifiers?.predicate && arg.type === 'true') {
|
2023-06-25 03:09:04 +03:00
|
|
|
continue
|
|
|
|
}
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `${arg.name}:`
|
2021-11-23 00:03:59 +03:00
|
|
|
|
2023-06-25 03:09:04 +03:00
|
|
|
const type = stringifyArgumentType(arg.type, arg.typeModifiers)
|
2021-11-23 00:03:59 +03:00
|
|
|
|
|
|
|
if (forIdComputation) {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `${normalizeType(type)} `
|
2021-11-23 00:03:59 +03:00
|
|
|
} else {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `${type} `
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-24 19:56:13 +03:00
|
|
|
const type = entry.typeModifiers ? stringifyArgumentType(entry.type, entry.typeModifiers) : entry.type
|
|
|
|
|
2021-11-23 00:03:59 +03:00
|
|
|
if (forIdComputation) {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `= ${normalizeType(type)}`
|
2021-11-23 00:03:59 +03:00
|
|
|
} else {
|
2024-08-13 04:53:07 +03:00
|
|
|
str += `= ${type};`
|
2021-11-23 00:03:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
}
|