| 1508 | |
| 1509 | /** @internal */ |
| 1510 | export const getLiterals = ( |
| 1511 | ast: AST.AST, |
| 1512 | isDecoding: boolean |
| 1513 | ): ReadonlyArray<[PropertyKey, AST.Literal]> => { |
| 1514 | switch (ast._tag) { |
| 1515 | case "Declaration": { |
| 1516 | const annotation = AST.getSurrogateAnnotation(ast) |
| 1517 | if (Option.isSome(annotation)) { |
| 1518 | return getLiterals(annotation.value, isDecoding) |
| 1519 | } |
| 1520 | break |
| 1521 | } |
| 1522 | case "TypeLiteral": { |
| 1523 | const out: Array<[PropertyKey, AST.Literal]> = [] |
| 1524 | for (let i = 0; i < ast.propertySignatures.length; i++) { |
| 1525 | const propertySignature = ast.propertySignatures[i] |
| 1526 | const type = isDecoding ? AST.encodedAST(propertySignature.type) : AST.typeAST(propertySignature.type) |
| 1527 | if (AST.isLiteral(type) && !propertySignature.isOptional) { |
| 1528 | out.push([propertySignature.name, type]) |
| 1529 | } |
| 1530 | } |
| 1531 | return out |
| 1532 | } |
| 1533 | case "TupleType": { |
| 1534 | const out: Array<[PropertyKey, AST.Literal]> = [] |
| 1535 | for (let i = 0; i < ast.elements.length; i++) { |
| 1536 | const element = ast.elements[i] |
| 1537 | const type = isDecoding ? AST.encodedAST(element.type) : AST.typeAST(element.type) |
| 1538 | if (AST.isLiteral(type) && !element.isOptional) { |
| 1539 | out.push([i, type]) |
| 1540 | } |
| 1541 | } |
| 1542 | return out |
| 1543 | } |
| 1544 | case "Refinement": |
| 1545 | return getLiterals(ast.from, isDecoding) |
| 1546 | case "Suspend": |
| 1547 | return getLiterals(ast.f(), isDecoding) |
| 1548 | case "Transformation": |
| 1549 | return getLiterals(isDecoding ? ast.from : ast.to, isDecoding) |
| 1550 | } |
| 1551 | return [] |
| 1552 | } |
| 1553 | |
| 1554 | /** |
| 1555 | * The purpose of the algorithm is to narrow down the pool of possible |