Files
Beam/Beam.Fluent/DownloadBuilder.cs
T
qwsdcvghyu89 7ed05abdb8 refactor: modularize Beam into new projects and interfaces
- Introduced modularity by splitting Beam into new projects: Beam.Abstractions, Beam.Models, and Beam.Downloaders.
- Refactored existing classes into appropriate namespaces and projects.
- Replaced specific implementations with abstractions (e.g., SourceLinkBuilder to LinkBuilder, State to IState, etc.).
- Updated interfaces: added ITemplate, IArticleData, IDownloadReport, and others for improved extensibility.
- Removed deprecated classes like SourceLinkBuilder and StateChangerFactory.
- Enhanced link handling in downloaders by refactoring to use `string` over `SourceLink`.
- Consolidated shared logic under Beam.Abstractions.
2025-09-22 01:51:46 +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);
}
}
}