Skip to main content

webgraph_cli/seq/
float.rs

1/*
2 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use crate::FloatSliceFormat;
8use anyhow::Result;
9use clap::Parser;
10use std::path::PathBuf;
11
12#[derive(Parser, Debug)]
13#[command(name = "float", about = "Converts a float sequence (e.g., centralities, ranks) between formats.", long_about = None, next_line_help = true)]
14pub struct CliArgs {
15    /// The source file path.​
16    pub src: PathBuf,
17
18    /// The destination file path.​
19    pub dst: PathBuf,
20
21    #[arg(long, value_enum, default_value_t)]
22    /// The format of the source file.​
23    pub src_fmt: FloatSliceFormat,
24
25    #[arg(long, value_enum, default_value_t)]
26    /// The format of the destination file.​
27    pub dst_fmt: FloatSliceFormat,
28
29    #[clap(long)]
30    /// Number of decimal digits for text formats.​
31    pub precision: Option<usize>,
32
33    #[clap(long)]
34    /// Treats the data as 32-bit floats instead of 64-bit.​
35    pub f32: bool,
36}
37
38pub fn main(args: CliArgs) -> Result<()> {
39    // Converts the sequence using the given float type.
40    macro_rules! convert {
41        ($F:ty) => {{
42            let data: Vec<$F> = args.src_fmt.load(&args.src)?;
43            let type_name = stringify!($F);
44            log::info!(
45                "Loaded {} {type_name} elements from {}",
46                data.len(),
47                args.src.display()
48            );
49            args.dst_fmt.store(&args.dst, &data, args.precision)?;
50            log::info!(
51                "Stored {} {type_name} elements to {}",
52                data.len(),
53                args.dst.display()
54            );
55        }};
56    }
57
58    if args.f32 {
59        convert!(f32)
60    } else {
61        convert!(f64)
62    }
63
64    Ok(())
65}