Phase 1 — Foundation

Mirror NPE's dependency-free spine in Rust:

* exceptions   — NetworkError + ParsingError + ContentUnavailable
                 + ExtractionError tree, with reqwest/serde_json conversions
* localization — Localization + ContentCountry, default (en, GB)
* downloader/  — Downloader trait, Request builder, Response,
                 reqwest blocking default impl
* page         — continuation-token carrier
* image        — Image + ImageSet + ResolutionLevel
                 (HEIGHT_UNKNOWN/WIDTH_UNKNOWN = -1)
* metainfo     — title/content/url/url_text grab-bag
* service      — StreamingService trait + LinkType + ServiceInfo
* newpipe      — process-global Downloader / Localization /
                 ContentCountry singleton

Foundational invariants nailed down (per SPEC §3):
* HTTP non-2xx returns Ok(Response); only 429 throws NetworkError::Recaptcha
* Response header keys lowercase-normalized
* Request.add_header PARITY with NPE bug (silent overwrite);
  append_header is our clean addition
* default Localization is en-GB
* No cookie jar in the default downloader

Tests: 7 unit + 7 live smoke against httpbin.org (gated on
'online-tests' feature). All green.
This commit is contained in:
Kayos 2026-05-24 16:32:36 -07:00
parent f44b46fab5
commit 46201c731f
16 changed files with 2689 additions and 1 deletions

68
src/newpipe.rs Normal file
View file

@ -0,0 +1,68 @@
// NewPipe singleton — mirrors NPE NewPipe.java.
//
// Holds the process-global Downloader + preferred Localization +
// preferred ContentCountry. init() once at startup, then call sites read
// the globals through these getters.
//
// Concrete service registration lands in Phase 3+ once YoutubeService
// exists. Phase 1 only wires the globals.
use std::sync::Arc;
use parking_lot::RwLock;
use crate::downloader::Downloader;
use crate::localization::{ContentCountry, Localization};
pub struct NewPipe {
downloader: RwLock<Option<Arc<dyn Downloader>>>,
preferred_localization: RwLock<Localization>,
preferred_content_country: RwLock<ContentCountry>,
}
impl NewPipe {
pub fn instance() -> &'static NewPipe {
use once_cell::sync::Lazy;
static INSTANCE: Lazy<NewPipe> = Lazy::new(|| NewPipe {
downloader: RwLock::new(None),
preferred_localization: RwLock::new(Localization::default()),
preferred_content_country: RwLock::new(ContentCountry::default()),
});
&INSTANCE
}
pub fn init(downloader: Arc<dyn Downloader>) {
*Self::instance().downloader.write() = Some(downloader);
}
pub fn init_full(
downloader: Arc<dyn Downloader>,
localization: Localization,
content_country: ContentCountry,
) {
let np = Self::instance();
*np.downloader.write() = Some(downloader);
*np.preferred_localization.write() = localization;
*np.preferred_content_country.write() = content_country;
}
pub fn downloader() -> Option<Arc<dyn Downloader>> {
Self::instance().downloader.read().clone()
}
pub fn preferred_localization() -> Localization {
Self::instance().preferred_localization.read().clone()
}
pub fn preferred_content_country() -> ContentCountry {
Self::instance().preferred_content_country.read().clone()
}
pub fn set_preferred_localization(localization: Localization) {
*Self::instance().preferred_localization.write() = localization;
}
pub fn set_preferred_content_country(content_country: ContentCountry) {
*Self::instance().preferred_content_country.write() = content_country;
}
}