     }
 
     /// Encapsulates the given typename with the specified external type
-    func encapsulateType(name typename: CodeTypeName, type: CodeExternalType, access: CodeAccess) -> CodeNamedType {
+    func encapsulateType(name typename: CodeTypeName, type: CodeExternalType, nestedTypes: [CodeNamedType] = [], access: CodeAccess) -> CodeStruct {
         let aliasType = type
         let propn = CodePropName("rawValue")
         let propd = CodeProperty.Declaration(name: propn, type: aliasType, access: access, mutable: true)

            if useOneOfEnums && casetypes.count >= 2 && casetypes.count <= 10 {
                let constantEnums = code.nestedTypes.compactMap({ $0 as? CodeSimpleEnum<String> })
                let topLevel = parents.isEmpty
                if topLevel { // all top-level typealias definitions go into a RawCodable
                    return aliasOneOf(casetypes, name: ename, optional: false, defined: parents.isEmpty, encapsulate: true, peerTypes: constantEnums)
                } else if code.nestedTypes.count == constantEnums.count { // if there are no nested types, or they are all constant enums, we can simply return a typealias to the OneOfX type
                    return aliasOneOf(casetypes, name: ename, optional: false, defined: parents.isEmpty, peerTypes: constantEnums)
                } else { // otherwise we need to continue to use the nested inner types in a hollow enum and return the typealias
                    let choiceName = oneOfSuffix
                    let aliasName = ename + (parents.isEmpty ? "" : choiceName) // top-level aliases are fully-qualified types because they are defined in defs and refs
                    // the enum code now just contains the nested types, so copy over only the embedded types
                    let nestedAlias = CodeTypeAlias(name: choiceName, type: oneOfType(casetypes), access: accessor(parents))
                    var nestedEnum = CodeEnum(name: ename + "Types", access: accessor(parents))
                    nestedEnum.nestedTypes = [nestedAlias] + code.nestedTypes



        enc.conforms += standardAdoptions
        enc.conforms.append(.rawCodable)

        enc.nestedTypes = nestedTypes

        for anon in [false, true] { // make both a named rawValue init as well as an anonymous one…
            let rawInit = CodeFunction.Declaration(name: "init", access: access, instance: true, exception: false, arguments: CodeTuple(elements: [(name: "rawValue", type: aliasType, value: nil, anon: anon)]), returns: CodeTuple(elements: []))
