// SPDX-FileCopyrightText: 2026 Progmasoft // SPDX-License-Identifier: MIT using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; namespace Progmasoft.Tazel.Dsl.Common; /// /// A pattern of files below the directory of the project file: * stands for any part of one name, /// ? for one character, and ** for any number of directories. /// /// The pattern, with / between directories. /// Patterns of files to leave out. public sealed class Glob(string pattern, IReadOnlyList? exclude = null) { /// The pattern. public string Pattern { get; } = pattern; /// Patterns of files the glob leaves out. public IReadOnlyList Exclude { get; } = exclude ?? []; /// public override string ToString() => $"new Glob(\"{Pattern}\")"; } /// One path, or a ; a list of files may mix both. public sealed class FilePattern { private FilePattern(string? path, Glob? glob) { Path = path; Glob = glob; } /// The path, when this is one path. public string? Path { get; } /// The glob, when this is a pattern. public Glob? Glob { get; } /// One path. public static implicit operator FilePattern(string path) => FromPath(path); /// A pattern. public static implicit operator FilePattern(Glob glob) => FromGlob(glob); /// One path. public static FilePattern FromPath(string path) { ArgumentNullException.ThrowIfNull(path); return new FilePattern(path, null); } /// A pattern. public static FilePattern FromGlob(Glob glob) { ArgumentNullException.ThrowIfNull(glob); return new FilePattern(null, glob); } /// public override string ToString() => Path is null ? Glob!.ToString() : $"\"{Path}\""; } /// /// Files of a target: ["a.c", "b.c"], new Glob("src/*.c"), or both in one list. /// [CollectionBuilder(typeof(Files), nameof(Create))] public sealed class Files : IEnumerable { private readonly FilePattern[] _items; private Files(FilePattern[] items) => _items = items; /// No files. public static Files None { get; } = new([]); /// The patterns, in their order. public IReadOnlyList Items => _items; /// The files of one glob. public static implicit operator Files(Glob glob) => FromGlob(glob); /// The files of one glob. public static Files FromGlob(Glob glob) => new([FilePattern.FromGlob(glob)]); /// Builds the files of a collection expression. public static Files Create(ReadOnlySpan items) => new(items.ToArray()); /// public IEnumerator GetEnumerator() => ((IEnumerable)_items).GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } /// An amount of memory. /// The amount in bytes. public readonly record struct ByteSize(long Bytes); /// Amounts of memory written as 16L.Gibibytes(). public static class ByteSizes { /// bytes. public static ByteSize Bytes(this long amount) => new(amount); /// KiB. public static ByteSize Kibibytes(this long amount) => new(checked(amount * 1024)); /// MiB. public static ByteSize Mebibytes(this long amount) => new(checked(amount * 1024 * 1024)); /// GiB. public static ByteSize Gibibytes(this long amount) => new(checked(amount * 1024 * 1024 * 1024)); }