( enumName: string, values: string[], ctx: RustCodegenCtx, description?: string, enumValueDescriptions?: EnumValueDescriptions, experimental = false, )
| 965 | // ── Enum emission ─────────────────────────────────────────────────────────── |
| 966 | |
| 967 | function emitRustStringEnum( |
| 968 | enumName: string, |
| 969 | values: string[], |
| 970 | ctx: RustCodegenCtx, |
| 971 | description?: string, |
| 972 | enumValueDescriptions?: EnumValueDescriptions, |
| 973 | experimental = false, |
| 974 | ): void { |
| 975 | if (ctx.generatedNames.has(enumName)) return; |
| 976 | ctx.generatedNames.add(enumName); |
| 977 | |
| 978 | const lines: string[] = []; |
| 979 | if (description) { |
| 980 | for (const line of description.split(/\r?\n/)) { |
| 981 | lines.push(`/// ${line}`); |
| 982 | } |
| 983 | } |
| 984 | pushRustExperimentalDocs(lines, experimental || ctx.experimentalTypeNames.has(enumName)); |
| 985 | lines.push( |
| 986 | "#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]", |
| 987 | ); |
| 988 | lines.push(`pub enum ${enumName} {`); |
| 989 | |
| 990 | const usedVariantNames = new Set<string>(); |
| 991 | const reservedVariantNames = new Set(["Unknown"]); |
| 992 | for (const value of values) { |
| 993 | const variantName = uniqueRustPascalIdentifier( |
| 994 | value, |
| 995 | usedVariantNames, |
| 996 | "Value", |
| 997 | reservedVariantNames, |
| 998 | ); |
| 999 | pushRustDoc(lines, enumValueDescriptions?.[value], " "); |
| 1000 | if (variantName !== value) { |
| 1001 | lines.push(` #[serde(rename = "${value}")]`); |
| 1002 | } |
| 1003 | lines.push(` ${variantName},`); |
| 1004 | } |
| 1005 | |
| 1006 | // Add a catch-all for forward compatibility. This is also the `Default` |
| 1007 | // variant — for wire-protocol enums an unknown/sentinel value is the only |
| 1008 | // safe default. |
| 1009 | lines.push(" /// Unknown variant for forward compatibility."); |
| 1010 | lines.push(" #[default]"); |
| 1011 | lines.push(" #[serde(other)]"); |
| 1012 | lines.push(" Unknown,"); |
| 1013 | |
| 1014 | lines.push("}"); |
| 1015 | ctx.enums.push(lines.join("\n")); |
| 1016 | } |
| 1017 | |
| 1018 | function emitRustConstStringEnum( |
| 1019 | enumName: string, |
no test coverage detected
searching dependent graphs…