7ed05abdb8
- 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.
44 lines
2.3 KiB
C#
44 lines
2.3 KiB
C#
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>
|
||
/// Type‑safe, staged builder that prevents callers from forgetting the mandatory steps
|
||
/// (source → link selection → transformer) and surfaces operational knobs as first‑class
|
||
/// 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);
|
||
}
|
||
}
|
||
}
|