Files
sharp-basic/src/Sharp/Config/ConfigSource.cs
T
2025-12-30 09:17:25 +08:00

33 lines
935 B
C#

using System.Diagnostics.CodeAnalysis;
namespace Config;
public sealed class ConfigSource
{
public required string Path { get; init; }
public required ConfigFormat Format { get; init; }
public bool Optional { get; init; } = true;
public override string ToString()
{
return $"{Format}:{Path} (Optional={Optional})";
}
public static bool TryGuess(string path, [NotNullWhen(true)] out ConfigSource? source)
{
source = null;
var ext = System.IO.Path.GetExtension(path).ToLowerInvariant();
var fmt = ext switch
{
".json" => ConfigFormat.Json,
".ini" => ConfigFormat.Ini,
".xml" => ConfigFormat.Xml,
".toml" => ConfigFormat.Toml,
_ => (ConfigFormat?)null
};
if (fmt is null) return false;
source = new ConfigSource { Path = path, Format = fmt.Value };
return true;
}
}