public TailViewState Convert(State state) { if (state == null || state == State.Empty) { return(GetDefaultValue()); } var doc = XDocument.Parse(state.Value); var root = doc.ElementOrThrow(Structure.Root); var filename = root.ElementOrThrow(Structure.FileName); var selectedFilter = root.ElementOrThrow(Structure.SelectedFilter); var searchStates = root.Element(Structure.SearchList) .Elements(Structure.SearchItem) .Select((element, index) => { var text = XmlEx.ElementOrThrow((XElement)element, Structure.Text); var position = ParseEx.ParseInt(element.Attribute(Structure.Filter).Value).ValueOr(() => index); var filter = ParseEx.ParseBool(element.Attribute(Structure.Filter).Value).ValueOr(() => true); var useRegEx = ParseEx.ParseBool(element.Attribute(Structure.UseRegEx).Value).ValueOr(() => false); var highlight = ParseEx.ParseBool(element.Attribute(Structure.Highlight).Value).ValueOr(() => true); var alert = ParseEx.ParseBool(element.Attribute(Structure.Alert).Value).ValueOr(() => false); var ignoreCase = ParseEx.ParseBool(element.Attribute(Structure.IgnoreCase).Value).ValueOr(() => true); var swatch = element.Attribute(Structure.Swatch).Value; var hue = element.Attribute(Structure.Hue).Value; var icon = element.Attribute(Structure.Icon).Value; return(new SearchState(text, position, useRegEx, highlight, filter, alert, ignoreCase, swatch, icon, hue)); }).ToArray(); return(new TailViewState(filename, selectedFilter, searchStates)); }
/// <summary> /// Converts the string representation of a semantic version to its <see cref="SemVersion"/> /// equivalent and returns a value that indicates whether the conversion succeeded. /// </summary> /// <param name="version">The version string.</param> /// <param name="semver">When the method returns, contains a <see cref="SemVersion"/> instance equivalent /// to the version string passed in, if the version string was valid, or <see langword="null"/> if the /// version string was not valid.</param> /// <param name="strict">If set to <see langword="true"/> minor and patch version are required, /// otherwise they are optional.</param> /// <returns><see langword="false"/> when a invalid version string is passed, otherwise <see langword="true"/>.</returns> public static bool TryParse(string version, out SemVersion semver, bool strict = false) { semver = null; if (version is null) { return(false); } var match = ParseEx.Match(version); if (!match.Success) { return(false); } if (!int.TryParse(match.Groups["major"].Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var major)) { return(false); } var minorMatch = match.Groups["minor"]; int minor = 0; if (minorMatch.Success) { if (!int.TryParse(minorMatch.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out minor)) { return(false); } } else if (strict) { return(false); } var patchMatch = match.Groups["patch"]; int patch = 0; if (patchMatch.Success) { if (!int.TryParse(patchMatch.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out patch)) { return(false); } } else if (strict) { return(false); } var prerelease = match.Groups["pre"].Value; var build = match.Groups["build"].Value; semver = new SemVersion(major, minor, patch, prerelease, build); return(true); }
/// <summary> /// Converts the string representation of a semantic version to its <see cref="SemVersion"/> equivalent. /// </summary> /// <param name="version">The version string.</param> /// <param name="strict">If set to <see langword="true"/> minor and patch version are required, /// otherwise they are optional.</param> /// <returns>The <see cref="SemVersion"/> object.</returns> /// <exception cref="ArgumentNullException">The <paramref name="version"/> is <see langword="null"/>.</exception> /// <exception cref="ArgumentException">The <paramref name="version"/> has an invalid format.</exception> /// <exception cref="InvalidOperationException">The <paramref name="version"/> is missing Minor or Patch versions and <paramref name="strict"/> is <see langword="true"/>.</exception> /// <exception cref="OverflowException">The Major, Minor, or Patch versions are larger than <code>int.MaxValue</code>.</exception> public static SemVersion Parse(string version, bool strict = false) { var match = ParseEx.Match(version); if (!match.Success) { throw new ArgumentException($"Invalid version '{version}'.", nameof(version)); } var major = int.Parse(match.Groups["major"].Value, CultureInfo.InvariantCulture); var minorMatch = match.Groups["minor"]; int minor = 0; if (minorMatch.Success) { minor = int.Parse(minorMatch.Value, CultureInfo.InvariantCulture); } else if (strict) { throw new InvalidOperationException("Invalid version (no minor version given in strict mode)"); } var patchMatch = match.Groups["patch"]; int patch = 0; if (patchMatch.Success) { patch = int.Parse(patchMatch.Value, CultureInfo.InvariantCulture); } else if (strict) { throw new InvalidOperationException("Invalid version (no patch version given in strict mode)"); } var prerelease = match.Groups["pre"].Value; var build = match.Groups["build"].Value; return(new SemVersion(major, minor, patch, prerelease, build)); }