// ReSharper disable MemberCanBePrivate.Global
namespace AIStudio.Tools.PluginSystem;
///
/// Represents a version number for a plugin.
///
/// The major version number.
/// The minor version number.
/// The patch version number.
public readonly record struct PluginVersion(int Major, int Minor, int Patch) : IComparable
{
///
/// Represents no version number.
///
public static readonly PluginVersion NONE = new(0, 0, 0);
///
/// Tries to parse the input string as a plugin version number.
///
/// The input string to parse.
/// The parsed version number.
/// True when the input string was successfully parsed; otherwise, false.
public static bool TryParse(string input, out PluginVersion version)
{
try
{
version = Parse(input);
return true;
}
catch
{
version = NONE;
return false;
}
}
///
/// Parses the input string as a plugin version number.
///
/// The input string to parse.
/// The parsed version number.
/// The input string is not in the correct format.
public static PluginVersion Parse(string input)
{
var segments = input.Split('.');
if (segments.Length != 3)
throw new FormatException("The input string must be in the format 'major.minor.patch'.");
var major = int.Parse(segments[0]);
var minor = int.Parse(segments[1]);
var patch = int.Parse(segments[2]);
if(major < 0 || minor < 0 || patch < 0)
throw new FormatException("The major, minor, and patch numbers must be greater than or equal to 0.");
return new PluginVersion(major, minor, patch);
}
///
/// Converts the plugin version number to a string in the format 'major.minor.patch'.
///
/// The plugin version number as a string.
public override string ToString() => $"{this.Major}.{this.Minor}.{this.Patch}";
///
/// Compares the plugin version number to another plugin version number.
///
/// The other plugin version number to compare to.
/// A value indicating the relative order of the plugin version numbers.
public int CompareTo(PluginVersion other)
{
var majorCompare = this.Major.CompareTo(other.Major);
if (majorCompare != 0)
return majorCompare;
var minorCompare = this.Minor.CompareTo(other.Minor);
if (minorCompare != 0)
return minorCompare;
return this.Patch.CompareTo(other.Patch);
}
public static bool operator >(PluginVersion left, PluginVersion right) => left.CompareTo(right) > 0;
public static bool operator <(PluginVersion left, PluginVersion right) => left.CompareTo(right) < 0;
public static bool operator >=(PluginVersion left, PluginVersion right) => left.CompareTo(right) >= 0;
public static bool operator <=(PluginVersion left, PluginVersion right) => left.CompareTo(right) <= 0;
}