// #nullable enable using System; using System.Text; namespace CarpaNet.Utilities; /// /// Helper methods for working with NSIDs (Namespaced Identifiers). /// public static class NsidHelper { /// /// Converts an NSID to a C# namespace. /// Example: "app.bsky.feed.post" -> "AppBsky.Feed" /// public static string ToNamespace(string nsid, string? rootNamespace = null) { if (string.IsNullOrEmpty(nsid)) { return rootNamespace ?? "ATProto"; } var parts = nsid.Split('.'); if (parts.Length < 3) { return rootNamespace ?? "ATProto"; } var sb = new StringBuilder(); // First two parts become the root namespace (e.g., "app.bsky" -> "AppBsky") sb.Append(ToPascalCase(parts[0])); sb.Append(ToPascalCase(parts[1])); // Middle parts become nested namespaces (excluding the last part which is the type name) for (int i = 2; i < parts.Length - 1; i++) { sb.Append('.'); sb.Append(ToPascalCase(parts[i])); } if (!string.IsNullOrEmpty(rootNamespace)) { return $"{rootNamespace}.{sb}"; } return sb.ToString(); } /// /// Extracts the type name from an NSID. /// Example: "app.bsky.feed.post" -> "Post" /// public static string ToTypeName(string nsid) { if (string.IsNullOrEmpty(nsid)) { return "Unknown"; } var parts = nsid.Split('.'); if (parts.Length == 0) { return "Unknown"; } return ToPascalCase(parts[parts.Length - 1]); } /// /// Extracts the type name from a reference string. /// Handles both global refs (NSID) and local refs (#name). /// public static string RefToTypeName(string refString) { if (string.IsNullOrEmpty(refString)) { return "Unknown"; } // Local reference: #typeName if (refString.StartsWith("#")) { return ToPascalCase(refString.Substring(1)); } // Global reference with fragment: nsid#typeName var hashIndex = refString.IndexOf('#'); if (hashIndex >= 0) { return ToPascalCase(refString.Substring(hashIndex + 1)); } // Global reference without fragment (main type) return ToTypeName(refString); } /// /// Extracts the namespace from a reference string. /// public static string RefToNamespace(string refString, string currentNsid, string? rootNamespace = null) { if (string.IsNullOrEmpty(refString)) { return ToNamespace(currentNsid, rootNamespace); } // Local reference: use current namespace if (refString.StartsWith("#")) { return ToNamespace(currentNsid, rootNamespace); } // Global reference with fragment var hashIndex = refString.IndexOf('#'); if (hashIndex >= 0) { var nsid = refString.Substring(0, hashIndex); return ToNamespace(nsid, rootNamespace); } // Global reference without fragment return ToNamespace(refString, rootNamespace); } /// /// Gets the full qualified type name from a reference. /// public static string RefToFullTypeName(string refString, string currentNsid, string? rootNamespace = null) { var ns = RefToNamespace(refString, currentNsid, rootNamespace); var typeName = RefToTypeName(refString); return $"{ns}.{typeName}"; } /// /// Converts a string to PascalCase. /// public static string ToPascalCase(string input) { if (string.IsNullOrEmpty(input)) { return input; } var sb = new StringBuilder(); bool capitalizeNext = true; foreach (char c in input) { if (c == '-' || c == '_') { capitalizeNext = true; continue; } if (capitalizeNext) { sb.Append(char.ToUpperInvariant(c)); capitalizeNext = false; } else { sb.Append(c); } } return sb.ToString(); } /// /// Converts a string to camelCase. /// public static string ToCamelCase(string input) { var pascal = ToPascalCase(input); if (string.IsNullOrEmpty(pascal)) { return pascal; } return char.ToLowerInvariant(pascal[0]) + pascal.Substring(1); } /// /// Checks if a string is a valid C# identifier and escapes reserved keywords. /// public static string EscapeIdentifier(string identifier) { if (string.IsNullOrEmpty(identifier)) { return "_"; } // Reserved C# keywords var keywords = new[] { "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "record" }; foreach (var keyword in keywords) { if (identifier.Equals(keyword, StringComparison.OrdinalIgnoreCase)) { return "@" + identifier; } } // Ensure first character is valid if (!char.IsLetter(identifier[0]) && identifier[0] != '_') { identifier = "_" + identifier; } return identifier; } /// /// Strips the @ escape prefix from an identifier if present. /// Useful for generating derived names (like interface names) from escaped identifiers. /// public static string StripEscapePrefix(string identifier) { if (string.IsNullOrEmpty(identifier)) { return identifier; } if (identifier.StartsWith("@")) { return identifier.Substring(1); } return identifier; } }