|
| 1 | +/*++ |
| 2 | +
|
| 3 | +Copyright (C) 2023 Autodesk Inc. |
| 4 | +
|
| 5 | +All rights reserved. |
| 6 | +
|
| 7 | +Redistribution and use in source and binary forms, with or without modification, |
| 8 | +are permitted provided that the following conditions are met: |
| 9 | +
|
| 10 | +1. Redistributions of source code must retain the above copyright notice, this |
| 11 | +list of conditions and the following disclaimer. |
| 12 | +2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +this list of conditions and the following disclaimer in the documentation |
| 14 | +and/or other materials provided with the distribution. |
| 15 | +
|
| 16 | +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR |
| 20 | +ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | +(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | +LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | +SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +
|
| 27 | +--*/ |
| 28 | + |
| 29 | +////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 30 | +// languagerust.go |
| 31 | +// functions to generate the Rust-layer of a library's API (can be used in bindings or implementation) |
| 32 | +////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 33 | + |
| 34 | +package main |
| 35 | + |
| 36 | +import ( |
| 37 | + "fmt" |
| 38 | + "strings" |
| 39 | +) |
| 40 | + |
| 41 | +func writeRustBaseTypeDefinitions(componentdefinition ComponentDefinition, w LanguageWriter, NameSpace string, BaseName string) error { |
| 42 | + w.Writeln("#[allow(unused_imports)]") |
| 43 | + w.Writeln("use std::ffi;") |
| 44 | + w.Writeln("") |
| 45 | + w.Writeln("/*************************************************************************************************************************") |
| 46 | + w.Writeln(" Version definition for %s", NameSpace) |
| 47 | + w.Writeln("**************************************************************************************************************************/") |
| 48 | + w.Writeln("") |
| 49 | + w.Writeln("const %s_VERSION_MAJOR : usize = %d;", strings.ToUpper(NameSpace), majorVersion(componentdefinition.Version)) |
| 50 | + w.Writeln("const %s_VERSION_MINOR : usize = %d;", strings.ToUpper(NameSpace), minorVersion(componentdefinition.Version)) |
| 51 | + w.Writeln("const %s_VERSION_MICRO : usize= %d;", strings.ToUpper(NameSpace), microVersion(componentdefinition.Version)) |
| 52 | + w.Writeln("const %s_VERSION_PRERELEASEINFO : &str = \"%s\";", strings.ToUpper(NameSpace), preReleaseInfo(componentdefinition.Version)) |
| 53 | + w.Writeln("const %s_VERSION_BUILDINFO : &str = \"%s\";", strings.ToUpper(NameSpace), buildInfo(componentdefinition.Version)) |
| 54 | + |
| 55 | + w.Writeln("") |
| 56 | + w.Writeln("") |
| 57 | + |
| 58 | + if len(componentdefinition.Enums) > 0 { |
| 59 | + w.Writeln("/*************************************************************************************************************************") |
| 60 | + w.Writeln(" Enum definitions for %s", NameSpace) |
| 61 | + w.Writeln("**************************************************************************************************************************/") |
| 62 | + w.Writeln("") |
| 63 | + for i := 0; i < len(componentdefinition.Enums); i++ { |
| 64 | + enuminfo := componentdefinition.Enums[i] |
| 65 | + w.Writeln("#[repr(C, u16)]") |
| 66 | + w.Writeln("#[allow(non_snake_case)]") |
| 67 | + w.Writeln("pub enum %s {", enuminfo.Name) |
| 68 | + for j := 0; j < len(enuminfo.Options); j++ { |
| 69 | + option := enuminfo.Options[j] |
| 70 | + sep := "," |
| 71 | + if j == len(enuminfo.Options)-1 { |
| 72 | + sep = "" |
| 73 | + } |
| 74 | + w.Writeln(" pub %s = %d%s", option.Name, option.Value, sep) |
| 75 | + } |
| 76 | + w.Writeln("}") |
| 77 | + w.Writeln("") |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if len(componentdefinition.Structs) > 0 { |
| 82 | + w.Writeln("/*************************************************************************************************************************") |
| 83 | + w.Writeln(" Interface Struct definitions for %s", NameSpace) |
| 84 | + w.Writeln("**************************************************************************************************************************/") |
| 85 | + w.Writeln("") |
| 86 | + for i := 0; i < len(componentdefinition.Structs); i++ { |
| 87 | + structinfo := componentdefinition.Structs[i] |
| 88 | + w.Writeln("#[repr(C)]") |
| 89 | + w.Writeln("#[allow(non_snake_case)]") |
| 90 | + w.Writeln("pub struct %s {", structinfo.Name) |
| 91 | + for j := 0; j < len(structinfo.Members); j++ { |
| 92 | + member := structinfo.Members[j] |
| 93 | + last := j == len(structinfo.Members)-1 |
| 94 | + err := writeRustMemberLine(member, w, structinfo.Name, last) |
| 95 | + if err != nil { |
| 96 | + return err |
| 97 | + } |
| 98 | + } |
| 99 | + w.Writeln("}") |
| 100 | + w.Writeln("") |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
| 106 | + |
| 107 | +func writeRustMemberLine(member ComponentDefinitionMember, w LanguageWriter, StructName string, last bool) error { |
| 108 | + suffix := "," |
| 109 | + if last { |
| 110 | + suffix = "" |
| 111 | + } |
| 112 | + arraysuffix := suffix |
| 113 | + if member.Rows > 0 { |
| 114 | + if member.Columns > 0 { |
| 115 | + arraysuffix = fmt.Sprintf("[%d][%d]%s", member.Columns, member.Rows, suffix) |
| 116 | + } else { |
| 117 | + arraysuffix = fmt.Sprintf("[%d]%s", member.Rows, suffix) |
| 118 | + } |
| 119 | + } |
| 120 | + switch member.Type { |
| 121 | + case "uint8": |
| 122 | + w.Writeln(" pub %s: u8%s", member.Name, arraysuffix) |
| 123 | + case "uint16": |
| 124 | + w.Writeln(" pub %s: u16%s", member.Name, arraysuffix) |
| 125 | + case "uint32": |
| 126 | + w.Writeln(" pub %s: u32%s", member.Name, arraysuffix) |
| 127 | + case "uint64": |
| 128 | + w.Writeln(" pub %s: u64%s", member.Name, arraysuffix) |
| 129 | + case "int8": |
| 130 | + w.Writeln(" pub %s: i8%s", member.Name, arraysuffix) |
| 131 | + case "int16": |
| 132 | + w.Writeln(" pub %s: i16%s", member.Name, arraysuffix) |
| 133 | + case "int32": |
| 134 | + w.Writeln(" pub %s: i32%s", member.Name, arraysuffix) |
| 135 | + case "int64": |
| 136 | + w.Writeln(" pub %s: i64%s", member.Name, arraysuffix) |
| 137 | + case "bool": |
| 138 | + w.Writeln(" pub %s: bool%s", member.Name, arraysuffix) |
| 139 | + case "single": |
| 140 | + w.Writeln(" pub %s: f32%s", member.Name, arraysuffix) |
| 141 | + case "double": |
| 142 | + w.Writeln(" pub %s: f64%s", member.Name, arraysuffix) |
| 143 | + case "pointer": |
| 144 | + w.Writeln(" pub %s: c_void%s", member.Name, arraysuffix) |
| 145 | + case "string": |
| 146 | + return fmt.Errorf("it is not possible for struct %s to contain a string value", StructName) |
| 147 | + case "class", "optionalclass": |
| 148 | + return fmt.Errorf("it is not possible for struct %s to contain a handle value", StructName) |
| 149 | + case "enum": |
| 150 | + w.Writeln(" pub %s: u16%s", member.Name, arraysuffix) |
| 151 | + } |
| 152 | + return nil |
| 153 | +} |
0 commit comments