use anyhow::anyhow;
use cloud_file::CloudFile;
use futures::StreamExt; use rand::{rngs::StdRng, Rng, SeedableRng};
use std::str::from_utf8;
async fn random_line(cloud_file: &CloudFile, seed: Option<u64>) -> Result<String, anyhow::Error> {
let mut rng = if let Some(s) = seed {
StdRng::seed_from_u64(s)
} else {
StdRng::from_entropy()
};
let mut selected_line = None;
let mut line_chunks = cloud_file.stream_line_chunks().await?;
let mut index_iter = 0..;
while let Some(line_chunk) = line_chunks.next().await {
let line_chunk = line_chunk?;
let lines = from_utf8(&line_chunk)?.lines();
for line in lines {
let index = index_iter.next().unwrap();
if rng.gen_range(0..=index) == 0 {
selected_line = Some(line.to_string());
}
}
}
selected_line.ok_or_else(|| anyhow!("No lines found in the file"))
}
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
let url = "https://raw.githubusercontent.com/fastlmm/bed-sample-files/main/toydata.5chrom.fam";
let cloud_file = CloudFile::new(url)?;
let line = random_line(&cloud_file, None).await?;
println!("random line: {line}");
Ok(())
}