diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-02-12 16:57:30 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-02-12 16:57:30 -0600 |
| commit | a1ad9eb42b3261a36a2d15d19e79038efca678d1 (patch) | |
| tree | bd87de998944aebad933b11060d3c03e7ac1f771 /src | |
| parent | 81fec1aa56415f0813fdffce829bc2fe4228c00b (diff) | |
| download | slskd-a1ad9eb42b3261a36a2d15d19e79038efca678d1.tar.gz | |
move option validation to extensions
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Extensions.cs | 93 |
1 files changed, 66 insertions, 27 deletions
diff --git a/src/slskd/Extensions.cs b/src/slskd/Extensions.cs index 44b0f721..b7243549 100644 --- a/src/slskd/Extensions.cs +++ b/src/slskd/Extensions.cs @@ -1,10 +1,30 @@ -namespace slskd +// <copyright file="Extensions.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd { using System; + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; + using slskd.Validation; /// <summary> /// Extensions. @@ -12,20 +32,10 @@ public static class Extensions { /// <summary> - /// Converts the given path to the local format (normalizes path separators). - /// </summary> - /// <param name="path"></param> - /// <returns></returns> - public static string ToLocalOSPath(this string path) - { - return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); - } - - /// <summary> /// Returns the directory from the given path, regardless of separator format. /// </summary> - /// <param name="path"></param> - /// <returns></returns> + /// <param name="path">The path.</param> + /// <returns>The directory.</returns> public static string DirectoryName(this string path) { var separator = path.Contains('\\') ? '\\' : '/'; @@ -36,23 +46,23 @@ /// <summary> /// Returns the SHA1 hash of the given string. /// </summary> - /// <param name="str"></param> - /// <returns></returns> + /// <param name="str">The string to hash.</param> + /// <returns>The hash.</returns> public static string Sha1(this string str) { using var sha1 = new SHA1Managed(); - return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", ""); + return BitConverter.ToString(sha1.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", string.Empty); } /// <summary> /// Formats byte to nearest size (KB, MB, etc.) /// </summary> - /// <param name="value"></param> - /// <param name="decimalPlaces"></param> - /// <returns></returns> + /// <param name="value">The value to format.</param> + /// <param name="decimalPlaces">The number of decimal places to include.</param> + /// <returns>The formatted string.</returns> public static string SizeSuffix(this double value, int decimalPlaces = 1) { - string[] SizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; + string[] sizeSuffixes = { "bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB" }; if (value < 0) { return "-" + SizeSuffix(-value); } if (value == 0) { return string.Format("{0:n" + decimalPlaces + "} bytes", 0); } @@ -60,21 +70,50 @@ // mag is 0 for bytes, 1 for KB, 2, for MB, etc. int mag = (int)Math.Log(value, 1024); - // 1L << (mag * 10) == 2 ^ (10 * mag) - // [i.e. the number of bytes in the unit corresponding to mag] + // 1L << (mag * 10) == 2 ^ (10 * mag) [i.e. the number of bytes in the unit corresponding to mag] decimal adjustedSize = (decimal)value / (1L << (mag * 10)); - // make adjustment when the value is large enough that - // it would round up to 1000 or more + // make adjustment when the value is large enough that it would round up to 1000 or more if (Math.Round(adjustedSize, decimalPlaces) >= 1000) { mag += 1; adjustedSize /= 1024; } - return string.Format("{0:n" + decimalPlaces + "} {1}", + return string.Format( + "{0:n" + decimalPlaces + "} {1}", adjustedSize, - SizeSuffixes[mag]); + sizeSuffixes[mag]); + } + + /// <summary> + /// Converts the given path to the local format (normalizes path separators). + /// </summary> + /// <param name="path">The path to convert.</param> + /// <returns>The converted path.</returns> + public static string ToLocalOSPath(this string path) + { + return path.Replace('\\', Path.DirectorySeparatorChar).Replace('/', Path.DirectorySeparatorChar); + } + + /// <summary> + /// Validates options. + /// </summary> + /// <param name="options">The options instance to validate.</param> + /// <param name="result">The result of the validation, if invalid.</param> + /// <returns>A value indicating whether the instance is valid.</returns> + public static bool TryValidate(this Options options, out CompositeValidationResult result) + { + result = null; + var results = new List<ValidationResult>(); + + if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; } } -} +}
\ No newline at end of file |
