using System; using Robust.Shared.Serialization; using Robust.Shared.Utility; namespace Robust.Shared.RichText; /// /// Contains a simple string of text formatted with markup tags. /// /// /// /// This type differs from by storing purely a markup string, /// rather than a full parsed object model. /// This makes it significantly more lightweight than , /// and suitable for places where markup only has to be passed around, rather than modified or interpreted. /// /// public struct FormattedString : IEquatable, ISelfSerialize { // NOTE: This type has a custom network type serializer. /// /// Represents an empty () string. /// public static readonly FormattedString Empty = new(""); /// /// The contained markup text. /// /// /// This must always be strict valid markup, i.e. parseable by . /// public readonly string Markup; [Obsolete("Do not construct FormattedString directly")] public FormattedString() { throw new NotSupportedException("Do not construct FormattedString directly"); } /// /// Internal constructor, does not validate markup is strictly valid. /// /// private FormattedString(string markup) { Markup = markup; } /// /// Create a from a strict markup string. /// /// /// The provided markup string must be strict valid markup, /// i.e. parseable by . /// /// /// Thrown of is not strict valid markup. /// /// public static FormattedString FromMarkup(string markup) { if (!FormattedMessage.ValidMarkup(markup)) throw new ArgumentException("Invalid markup string"); return new FormattedString(markup); } /// /// Create a from a permissive markup string. /// /// /// The provided markup string does not need to be strict valid markup, /// but it will be normalized to be strict if it's not. /// /// public static FormattedString FromMarkupPermissive(string markup) { // We round trip here to ensure the contents are valid. var permissive = FormattedMessage.FromMarkupPermissive(markup); return (FormattedString)permissive; } /// /// Create a from plaintext (escaping it if necessary). /// /// /// This is equivalent to /// public static FormattedString FromPlainText(string plainText) { return new FormattedString(FormattedMessage.EscapeText(plainText)); } public static explicit operator FormattedString(FormattedMessage message) { // Assumed to be valid markup returned by ToMarkup(). return new FormattedString(message.ToMarkup()); } public static explicit operator FormattedMessage(FormattedString str) { // This should never throw. return FormattedMessage.FromMarkupOrThrow(str.Markup); } public static explicit operator string(FormattedString str) { return str.Markup; } public readonly bool Equals(FormattedString other) { return other.Markup == Markup; } public readonly override bool Equals(object? obj) { return obj is FormattedString other && Equals(other); } public readonly override int GetHashCode() { return Markup.GetHashCode(); } public static bool operator ==(FormattedString left, FormattedString right) { return left.Equals(right); } public static bool operator !=(FormattedString left, FormattedString right) { return !left.Equals(right); } void ISelfSerialize.Deserialize(string value) { this = FromMarkup(value); } readonly string ISelfSerialize.Serialize() { return Markup; } }