|
| 1 | +// Copyright (c) 2024 Roberto Raggi <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +export type Type = BaseType; |
| 22 | + |
| 23 | +export type BaseTypeName = "string" | "integer" | "uinteger"; |
| 24 | + |
| 25 | +export type BaseType = { |
| 26 | + kind: "base"; |
| 27 | + name: BaseTypeName; |
| 28 | +}; |
| 29 | + |
| 30 | +export type EnumerationValue = { |
| 31 | + name: string; |
| 32 | + value: string; |
| 33 | +}; |
| 34 | + |
| 35 | +export type MetaData = {}; |
| 36 | + |
| 37 | +export type Enumeration = { |
| 38 | + documentation?: string; |
| 39 | + name: string; |
| 40 | + since?: string; |
| 41 | + supportsCustomValues?: boolean; |
| 42 | + type: BaseType; |
| 43 | + values: EnumerationValue[]; |
| 44 | +}; |
| 45 | + |
| 46 | +export type Notification = {}; |
| 47 | + |
| 48 | +export type Request = {}; |
| 49 | + |
| 50 | +export type Structure = {}; |
| 51 | + |
| 52 | +export type TypeAlias = {}; |
| 53 | + |
| 54 | +export type MetaModel = { |
| 55 | + metaData: MetaData; |
| 56 | + enumerations: Enumeration[]; |
| 57 | + notifications: Notification[]; |
| 58 | + requests: Request[]; |
| 59 | + structures: Structure[]; |
| 60 | + typeAliases: TypeAlias[]; |
| 61 | +}; |
| 62 | + |
| 63 | +export function getEnumBaseType(enumeration: Enumeration) { |
| 64 | + switch (enumeration.type.name) { |
| 65 | + case "integer": |
| 66 | + return " : int"; |
| 67 | + case "uinteger": |
| 68 | + return " : unsigned int"; |
| 69 | + default: |
| 70 | + return ""; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +export function toUpperCamelCase(name: string) { |
| 75 | + return name[0].toUpperCase() + name.slice(1); |
| 76 | +} |
| 77 | + |
| 78 | +export function getEnumeratorName(enumerator: EnumerationValue) { |
| 79 | + const name = toUpperCamelCase(enumerator.name); |
| 80 | + return `k${name}`; |
| 81 | +} |
| 82 | + |
| 83 | +export function getEnumeratorInitializer( |
| 84 | + enumeration: Enumeration, |
| 85 | + enumerator: EnumerationValue |
| 86 | +) { |
| 87 | + if (enumeration.type.name === "string") { |
| 88 | + return ""; |
| 89 | + } |
| 90 | + return ` = ${enumerator.value}`; |
| 91 | +} |
0 commit comments