v0.1.0-U (vc=8): Phase U-1 + U-2 — Rust core + rustypipe search

NewPipeExtractor (Java) → strawcore (Rust) migration begins. Phase U:
- U-1: Rust toolchain + UniFFI smoke test
- U-2: rustypipe search via uniffi suspend fun, SearchViewModel swapped

What landed:
- rust/strawcore — UniFFI-exported Rust crate using proc-macros.
  Builds for arm64-v8a + armeabi-v7a + x86 + x86_64 via cargo-ndk.
  Tokio multi-thread runtime singleton drives rustypipe's async API.
- strawApp/build.gradle.kts — cargoBuildHost + cargoBuild + uniffiBindgen
  Gradle Exec tasks chained into the Android build. Generated Kotlin
  bindings land in src/main/java/uniffi/strawcore/ (gitignored).
- SearchViewModel.kt — calls uniffi.strawcore.search(query) directly.
  NewPipeExtractor still in deps for VideoDetail/Player/Channel paths;
  those move to Rust in U-3 / U-4.
- Build chain quirks beat:
  * cargo absolute path in Exec tasks (PATH wasn't propagating)
  * uniffi-bindgen needs UNSTRIPPED host .so — separate cargoBuildHost
    builds a debug-profile host lib to read metadata from
  * rustypipe rustls-tls-webpki-roots avoids the openssl-sys
    cross-compile tarpit
  * rquickjs-sys 'bindgen' feature opted in (no prebuilt Android
    bindings ship; crafting-table has libclang 14)
- crafting-table runtime install (until Dockerfile catches up):
  rustup + 4 Android targets + cargo-ndk + NDK r27c. Persists in
  /caches/cargo + /caches/android-sdk via the volume mount.

APK size: 22MB (U-1) → 37MB (U-2). libstrawcore.so 3-5MB per ABI carries
rustypipe + reqwest + tokio + rustls + rquickjs. NewPipeExtractor still
in for now (still drives detail + player + channel + feed), so the
Java half is doubled up. U-5 removes it.
This commit is contained in:
Kayos 2026-05-24 08:36:50 -07:00
parent 9550b207ab
commit 7ff5ac79e5
14 changed files with 432 additions and 29 deletions

View file

@ -0,0 +1,29 @@
// Strawcore error type — anything that can fail in our Rust core surfaces
// as one of these variants. UniFFI maps it to a Kotlin sealed class
// `StrawcoreException` so Kotlin can pattern-match on the kind.
use thiserror::Error;
#[derive(Debug, Error, uniffi::Error)]
pub enum StrawcoreError {
#[error("network: {msg}")]
Network { msg: String },
#[error("extractor: {msg}")]
Extractor { msg: String },
#[error("not found: {what}")]
NotFound { what: String },
#[error("unsupported: {detail}")]
Unsupported { detail: String },
}
impl From<rustypipe::error::Error> for StrawcoreError {
fn from(e: rustypipe::error::Error) -> Self {
// Bucket every rustypipe error into Extractor for now. Phase U-3+
// can pull apart specific cases (e.g. ageRestricted → NotFound,
// network timeouts → Network) when we have a tighter UI for it.
StrawcoreError::Extractor { msg: e.to_string() }
}
}

46
rust/strawcore/src/lib.rs Normal file
View file

@ -0,0 +1,46 @@
// strawcore — Rust core for the Straw Android app.
//
// Phase U-1: hello-world smoke test. One exported function, returns a
// String so we know JNI + UniFFI marshalling works end-to-end before we
// pull in rustypipe.
//
// Phase U-2+ adds real APIs (search, stream_info, channel_info).
use std::sync::Once;
mod error;
mod runtime;
mod search;
#[allow(unused_imports)]
use runtime::block_on;
// Re-exports so UniFFI sees the types at the crate root for macro discovery.
pub use error::StrawcoreError;
pub use search::SearchItem;
/// Initialize Android logging once. The Kotlin side calls this from
/// StrawApp.onCreate() so anything emitted via `log::info!()` shows up in
/// `adb logcat -s strawcore`.
#[uniffi::export]
pub fn init_logging() {
static ONCE: Once = Once::new();
ONCE.call_once(|| {
android_logger::init_once(
android_logger::Config::default()
.with_max_level(log::LevelFilter::Info)
.with_tag("strawcore"),
);
log::info!("strawcore initialized");
});
}
/// Smoke-test entry point — round-trip a string through JNI so we know
/// the bridge is working before pulling in rustypipe.
#[uniffi::export]
pub fn hello_from_rust(name: String) -> String {
log::info!("hello_from_rust called with name={}", name);
format!("hello {} from rust 🦀 (strawcore v{})", name, env!("CARGO_PKG_VERSION"))
}
uniffi::setup_scaffolding!();

View file

@ -0,0 +1,21 @@
// Tokio runtime singleton — rustypipe is async-first, so every exported
// function that touches the network needs to drive a runtime. Building one
// per call is wasteful; we build one shared multi-thread runtime at first
// use and `block_on` against it.
use once_cell::sync::Lazy;
use tokio::runtime::Runtime;
static RT: Lazy<Runtime> = Lazy::new(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2) // Mobile — we don't need many. Most blocks are I/O.
.enable_all()
.thread_name("strawcore-tokio")
.build()
.expect("strawcore: failed to build tokio runtime")
});
#[allow(dead_code)]
pub fn block_on<F: std::future::Future>(fut: F) -> F::Output {
RT.block_on(fut)
}

View file

@ -0,0 +1,65 @@
// Phase U-2 — search via rustypipe, exposed to Kotlin as a suspend fun.
//
// `SearchItem` mirrors the existing Kotlin `StreamItem` field for field so
// `SearchViewModel` can swap NewPipeExtractor for this with a one-line
// change. We map only Video items from rustypipe's result (it also returns
// channels and playlists which we don't need for the search list yet).
use crate::error::StrawcoreError;
use rustypipe::client::RustyPipe;
use rustypipe::model::YouTubeItem;
#[derive(Debug, Clone, uniffi::Record)]
pub struct SearchItem {
pub url: String,
pub title: String,
pub uploader: String,
pub uploader_url: Option<String>,
pub thumbnail: Option<String>,
/// Duration in seconds. 0 = live/unknown.
pub duration_seconds: i64,
/// Reported view count. 0 = unknown.
pub view_count: i64,
}
fn yt_video_url(id: &str) -> String {
format!("https://www.youtube.com/watch?v={}", id)
}
fn yt_channel_url(id: &str) -> String {
format!("https://www.youtube.com/channel/{}", id)
}
#[uniffi::export(async_runtime = "tokio")]
pub async fn search(query: String) -> Result<Vec<SearchItem>, StrawcoreError> {
log::info!("strawcore::search query={}", query);
let rp = RustyPipe::new();
let results = rp.query().search(query).await?;
let items: Vec<SearchItem> = results
.items
.items
.into_iter()
.filter_map(|item| match item {
YouTubeItem::Video(v) => Some(SearchItem {
url: yt_video_url(&v.id),
title: v.name,
uploader: v
.channel
.as_ref()
.map(|c| c.name.clone())
.unwrap_or_default(),
uploader_url: v.channel.as_ref().map(|c| yt_channel_url(&c.id)),
thumbnail: v.thumbnail.first().map(|t| t.url.clone()),
duration_seconds: v.duration.unwrap_or(0) as i64,
view_count: v.view_count.unwrap_or(0) as i64,
}),
// Channels + playlists are dropped at U-2; future phases can
// surface them as a "channels you might like" row.
_ => None,
})
.collect();
log::info!("strawcore::search returned {} videos", items.len());
Ok(items)
}

View file

@ -0,0 +1,6 @@
// Tiny entry point so `cargo run --bin uniffi-bindgen` works — the actual
// logic lives in the uniffi crate, this just delegates to its CLI.
fn main() {
uniffi::uniffi_bindgen_main()
}