diff --git a/noodles-util/Cargo.toml b/noodles-util/Cargo.toml index b6f6748c4..fbd2bd406 100644 --- a/noodles-util/Cargo.toml +++ b/noodles-util/Cargo.toml @@ -51,7 +51,7 @@ futures = { workspace = true, optional = true, features = ["std"] } tokio = { workspace = true, optional = true, features = ["fs", "io-util"] } [dev-dependencies] -tokio = { workspace = true, features = ["macros", "rt-multi-thread"] } +tokio = { workspace = true, features = ["io-std", "macros", "rt-multi-thread"] } [package.metadata.docs.rs] all-features = true @@ -83,3 +83,7 @@ required-features = ["variant"] [[example]] name = "util_variant_view" required-features = ["variant"] + +[[example]] +name = "util_variant_view_async" +required-features = ["async", "variant"] diff --git a/noodles-util/examples/util_variant_view_async.rs b/noodles-util/examples/util_variant_view_async.rs new file mode 100644 index 000000000..eafe12ff6 --- /dev/null +++ b/noodles-util/examples/util_variant_view_async.rs @@ -0,0 +1,40 @@ +//! Prints a variant file in the VCF format. +//! +//! The result matches the output of `bcftools view `. + +use std::env; + +use futures::TryStreamExt; +use noodles_util::variant; +use noodles_vcf as vcf; +use tokio::io::{self, AsyncWriteExt}; + +#[tokio::main] +async fn main() -> io::Result<()> { + let src = env::args().nth(1).expect("missing src"); + + let builder = variant::r#async::io::reader::Builder::default(); + + let mut reader = if src == "-" { + builder.build_from_reader(io::stdin()).await? + } else { + builder.build_from_path(src).await? + }; + + let header = reader.read_header().await?; + + let mut writer = vcf::r#async::io::Writer::new(io::stdout()); + writer.write_header(&header).await?; + + let mut records = reader.records(); + + while let Some(record) = records.try_next().await? { + writer + .write_variant_record(&header, record.as_ref()) + .await?; + } + + writer.get_mut().shutdown().await?; + + Ok(()) +}