--!nocheck -- NOTE: This must be ran in Roblox Studio to get up-to-date font values local contents = "" contents ..= "\ntype FontData = (&'static str, FontWeight, FontStyle);\n" local ENUM_IMPLEMENTATION = [[ #[derive(Debug, Clone, Copy, PartialEq)] pub(crate) enum <> { <> } impl <> { pub(crate) fn as_<>(&self) -> <> { match self { <> } } pub(crate) fn from_<>(n: <>) -> Option { match n { <> _ => None, } } } impl Default for <> { fn default() -> Self { Self::<> } } impl std::str::FromStr for <> { type Err = &'static str; fn from_str(s: &str) -> Result { match s { <> _ => Err("Unknown <>"), } } } impl std::fmt::Display for <> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", match self { <> } ) } } impl<'lua> FromLua<'lua> for <> { fn from_lua(lua_value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult { let mut message = None; if let LuaValue::UserData(ud) = &lua_value { let value = ud.borrow::()?; if value.parent.desc.name == "<>" { if let Ok(value) = <>::from_str(&value.name) { return Ok(value); } else { message = Some(format!( "Found unknown Enum.<> value '{}'", value.name )); } } else { message = Some(format!( "Expected Enum.<>, got Enum.{}", value.parent.desc.name )); } } Err(LuaError::FromLuaConversionError { from: lua_value.type_name(), to: "Enum.<>", message, }) } } impl<'lua> ToLua<'lua> for <> { fn to_lua(self, lua: &'lua Lua) -> LuaResult> { match EnumItem::from_enum_name_and_name("<>", self.to_string()) { Some(enum_item) => Ok(LuaValue::UserData(lua.create_userdata(enum_item)?)), None => Err(LuaError::ToLuaConversionError { from: "<>", to: "EnumItem", message: Some(format!("Found unknown Enum.<> value '{}'", self)), }), } } } ]] -- FontWeight enum and implementation local function makeRustEnum(enum, default, numType: string) local name = tostring(enum) name = string.gsub(name, "^Enum.", "") local defaultName = tostring(default) defaultName = string.gsub(defaultName, "^Enum.", "") defaultName = string.gsub(defaultName, "^" .. name .. ".", "") local enumNames = "" local enumToNumbers = "" local numbersToEnum = "" local stringsToEnum = "" local enumToStrings = "" for _, enum in enum:GetEnumItems() do enumNames ..= `\n{enum.Name},` enumToNumbers ..= `\nSelf::{enum.Name} => {enum.Value},` numbersToEnum ..= `\n{enum.Value} => Some(Self::{enum.Name}),` stringsToEnum ..= `\n"{enum.Name}" => Ok(Self::{enum.Name}),` enumToStrings ..= `\nSelf::{enum.Name} => "{enum.Name}",` end local mappings: { [string]: string } = { ["<>"] = name, ["<>"] = defaultName, ["<>"] = numType, ["<>"] = enumNames, ["<>"] = enumToNumbers, ["<>"] = enumToStrings, ["<>"] = numbersToEnum, ["<>"] = stringsToEnum, } local result = ENUM_IMPLEMENTATION for key, replacement in mappings do result = string.gsub(result, "(\t*)" .. key, function(tabbing) local spacing = string.gsub(tabbing, "\t", " ") local inner = string.gsub(replacement, "\n", "\n" .. spacing) inner = string.gsub(inner, "^\n+", "") return inner end) end return result end contents ..= makeRustEnum(Enum.FontWeight, Enum.FontWeight.Regular, "u16") contents ..= "\n" contents ..= makeRustEnum(Enum.FontStyle, Enum.FontStyle.Normal, "u8") contents ..= "\n" -- Font constant map from enum to font data local longestNameLen = 0 local longestFamilyLen = 0 local longestWeightLen = 0 for _, enum in Enum.Font:GetEnumItems() do longestNameLen = math.max(longestNameLen, #enum.Name) if enum ~= Enum.Font.Unknown then local font = Font.fromEnum(enum) longestFamilyLen = math.max(longestFamilyLen, #font.Family) longestWeightLen = math.max(longestWeightLen, #font.Weight.Name) end end contents ..= "\n#[rustfmt::skip]\nconst FONT_ENUM_MAP: &[(&str, Option)] = &[\n" for _, enum in Enum.Font:GetEnumItems() do if enum == Enum.Font.Unknown then contents ..= string.format( ' ("Unknown",%s None),\n', string.rep(" ", longestNameLen - #enum.Name) ) else local font = Font.fromEnum(enum) contents ..= string.format( ' ("%s",%s Some(("%s",%s FontWeight::%s,%s FontStyle::%s))),\n', enum.Name, string.rep(" ", longestNameLen - #enum.Name), font.Family, string.rep(" ", longestFamilyLen - #font.Family), font.Weight.Name, string.rep(" ", longestWeightLen - #font.Weight.Name), font.Style.Name ) end end contents ..= "];\n" print(contents)