Files
Beam/Beam.Fluent/DownloadBuilder.cs
T
qwsdcvghyu89 13c6fbaf5f save
2025-09-27 13:37:40 +10:00

44 lines
2.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using aeqw89.DataKeys;
using Beam;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using Beam.Data;
using Beam.Downloaders;
using Beam.Models;
namespace Beam.Fluent {
/// <summary>
/// Typesafe, staged builder that prevents callers from forgetting the mandatory steps
/// (source → link selection → transformer) and surfaces operational knobs as firstclass
/// methods instead of magic parameters.
/// </summary>
public static partial class DownloadBuilder<RawType, OutType> {
/* ──────────────────────────── Entry points ─────────────────────────── */
public static ILinkStage FromResource(DataKey<ResourceDictionary> dict, string kind, BeamDataContext beamDataDictionary)
=> Create(dict, beamDataDictionary, kind);
public static IAlternativeLinkStage FromScratch()
=> new LinkStage(null!, null!, null!, new());
private static ILinkStage Create(DataKey<ResourceDictionary> resourceDict, BeamDataContext data, string kind) {
var (source, initial) = Resolve(resourceDict, kind, data);
var ctxBuilder = new DownloadContextBuilder<RawType>().WithLinks([]); // placeholder, filled later.
return new LinkStage(source, initial, data, ctxBuilder);
}
private static (WebResource Source, State Initial) Resolve(DataKey<ResourceDictionary> resourceDict, string kind, BeamDataContext data) {
if (!data.ResourceDictionaries.TryGetValue(resourceDict, out var dict))
throw new KeyNotFoundException($"Novel '{resourceDict}' not found in BeamDataDictionary.");
if (!dict.Resources.TryGetValue(kind, out var sourceKey))
throw new KeyNotFoundException($"Novel kind '{kind}' not found in '{resourceDict}'");
if (!data.Resources.TryGetValue(sourceKey, out var source))
throw new KeyNotFoundException($"Novel source '{sourceKey}' was not found");
if (!data.InitialStates.TryGetValue(sourceKey.To<ImmutableState>(), out var istate))
throw new KeyNotFoundException($"Immutable state for kind '{kind}' not found");
return (source, istate);
}
}
}