diff --git a/src/Kook.Net.CardMarkup/CardMarkupSerializer.cs b/src/Kook.Net.CardMarkup/CardMarkupSerializer.cs index a5fbb482..ee189c17 100644 --- a/src/Kook.Net.CardMarkup/CardMarkupSerializer.cs +++ b/src/Kook.Net.CardMarkup/CardMarkupSerializer.cs @@ -3,6 +3,10 @@ using Kook.CardMarkup.Extensions; using Kook.CardMarkup.Models; +#if NETSTANDARD2_1 || NET5_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif + namespace Kook.CardMarkup; /// @@ -242,7 +246,11 @@ public static IEnumerable Deserialize(Stream xmlStream) /// UTF-8 encoded XML file /// enumerable, will be null if return value is false /// True if deserialization is successful, otherwise false - public static bool TryDeserialize(FileInfo file, out IEnumerable? cards) + public static bool TryDeserialize(FileInfo file, +#if NETSTANDARD2_1 || NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out IEnumerable? cards) { try { @@ -262,7 +270,11 @@ public static bool TryDeserialize(FileInfo file, out IEnumerable? cards) /// UTF-8 encoded XML text /// enumerable, will be null if return value is false /// True if deserialization is successful, otherwise false - public static bool TryDeserialize(string xmlText, out IEnumerable? cards) + public static bool TryDeserialize(string xmlText, +#if NETSTANDARD2_1 || NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out IEnumerable? cards) { try { @@ -282,7 +294,11 @@ public static bool TryDeserialize(string xmlText, out IEnumerable? cards) /// UTF-8 encoded XML stream /// enumerable, will be null if return value is false /// True if deserialization is successful, otherwise false - public static bool TryDeserialize(Stream xmlStream, out IEnumerable? cards) + public static bool TryDeserialize(Stream xmlStream, +#if NETSTANDARD2_1 || NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out IEnumerable? cards) { try { diff --git a/src/Kook.Net.Core/Extensions/CardExtensions.cs b/src/Kook.Net.Core/Extensions/CardExtensions.cs index 3fff2c07..18b00c33 100644 --- a/src/Kook.Net.Core/Extensions/CardExtensions.cs +++ b/src/Kook.Net.Core/Extensions/CardExtensions.cs @@ -48,7 +48,10 @@ public static KMarkdownElementBuilder ToBuilder(this KMarkdownElement entity) public static ImageElementBuilder ToBuilder(this ImageElement entity) { if (entity is null) return null; - return new ImageElementBuilder { Source = entity.Source, Alternative = entity.Alternative, Size = entity.Size, Circle = entity.Circle }; + return new ImageElementBuilder + { + Source = entity.Source, Alternative = entity.Alternative, Size = entity.Size, Circle = entity.Circle + }; } /// @@ -57,7 +60,10 @@ public static ImageElementBuilder ToBuilder(this ImageElement entity) public static ButtonElementBuilder ToBuilder(this ButtonElement entity) { if (entity is null) return null; - return new ButtonElementBuilder { Theme = entity.Theme, Click = entity.Click, Value = entity.Value, Text = entity.Text.ToBuilder() }; + return new ButtonElementBuilder + { + Theme = entity.Theme, Click = entity.Click, Value = entity.Value, Text = entity.Text.ToBuilder() + }; } /// @@ -66,7 +72,10 @@ public static ButtonElementBuilder ToBuilder(this ButtonElement entity) public static ParagraphStructBuilder ToBuilder(this ParagraphStruct entity) { if (entity is null) return null; - return new ParagraphStructBuilder { ColumnCount = entity.ColumnCount, Fields = entity.Fields.Select(x => x.ToBuilder()).ToList() }; + return new ParagraphStructBuilder + { + ColumnCount = entity.ColumnCount, Fields = entity.Fields.Select(x => x.ToBuilder()).ToList() + }; } #endregion @@ -112,7 +121,12 @@ public static HeaderModuleBuilder ToBuilder(this HeaderModule entity) public static SectionModuleBuilder ToBuilder(this SectionModule entity) { if (entity is null) return null; - return new SectionModuleBuilder { Mode = entity.Mode, Text = entity.Text.ToBuilder(), Accessory = entity.Accessory.ToBuilder() }; + return new SectionModuleBuilder + { + Mode = entity.Mode, + Text = entity.Text.ToBuilder(), + Accessory = entity.Accessory.ToBuilder() + }; } /// @@ -193,7 +207,10 @@ public static VideoModuleBuilder ToBuilder(this VideoModule entity) public static CountdownModuleBuilder ToBuilder(this CountdownModule entity) { if (entity is null) return null; - return new CountdownModuleBuilder { Mode = entity.Mode, EndTime = entity.EndTime, StartTime = entity.StartTime }; + return new CountdownModuleBuilder + { + Mode = entity.Mode, EndTime = entity.EndTime, StartTime = entity.StartTime + }; } /// @@ -232,7 +249,10 @@ public static CardBuilder ToBuilder(this Card builder) return new CardBuilder { - Theme = builder.Theme, Size = builder.Size, Color = builder.Color, Modules = builder.Modules.Select(m => m.ToBuilder()).ToList() + Theme = builder.Theme, + Size = builder.Size, + Color = builder.Color, + Modules = builder.Modules.Select(m => m.ToBuilder()).ToList() }; } diff --git a/src/Kook.Net.Rest/Extensions/CardJsonExtension.cs b/src/Kook.Net.Rest/Extensions/CardJsonExtension.cs index 07d7453b..7ebae4da 100644 --- a/src/Kook.Net.Rest/Extensions/CardJsonExtension.cs +++ b/src/Kook.Net.Rest/Extensions/CardJsonExtension.cs @@ -4,6 +4,10 @@ using Kook.API; using Kook.Net.Converters; +#if NETSTANDARD2_1 || NET5_0_OR_GREATER +using System.Diagnostics.CodeAnalysis; +#endif + namespace Kook.Rest; /// @@ -24,9 +28,12 @@ public static class CardJsonExtension /// The json string to parse. /// The with populated values. An empty instance if method returns false. /// true if was successfully parsed. false if not. - public static bool TryParseSingle(string json, out ICardBuilder builder) + public static bool TryParseSingle(string json, +#if NETSTANDARD2_1 || NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out ICardBuilder builder) { - builder = new CardBuilder(); try { CardBase model = JsonSerializer.Deserialize(json, _options.Value); @@ -37,10 +44,12 @@ public static bool TryParseSingle(string json, out ICardBuilder builder) return true; } + builder = new CardBuilder(); return false; } catch { + builder = new CardBuilder(); return false; } } @@ -51,9 +60,12 @@ public static bool TryParseSingle(string json, out ICardBuilder builder) /// The json string to parse. /// A collection of with populated values. An empty instance if method returns false. /// true if was successfully parsed. false if not. - public static bool TryParseMany(string json, out IEnumerable builders) + public static bool TryParseMany(string json, +#if NETSTANDARD2_1 || NET5_0_OR_GREATER + [NotNullWhen(true)] +#endif + out IEnumerable builders) { - builders = Enumerable.Empty(); try { IEnumerable models = JsonSerializer.Deserialize>(json, _options.Value); @@ -64,10 +76,12 @@ public static bool TryParseMany(string json, out IEnumerable build return true; } + builders = null; return false; } catch { + builders = null; return false; } }