use std::{num::NonZeroU64, path::PathBuf};

use anyhow::{Result, bail};
use log::error;

#[derive(Debug)]
pub struct Args {
    pub filename: PathBuf,
    pub line: Option<NonZeroU64>,
    pub col: Option<NonZeroU64>,
}

pub fn parse() -> Result<Args> {
    let mut args = std::env::args().skip(1);
    let Some(filename) = args.next().map(PathBuf::from) else {
        bail!("not enough arguments");
    };
    let line = args
        .next()
        .map(|l| {
            l.parse()
                .inspect_err(|e| error!("Failed to parse line number {l:?}: {e}"))
        })
        .transpose()?;
    let col = args
        .next()
        .map(|c| {
            c.parse()
                .inspect_err(|e| error!("Failed to parse column number {c:?}: {e}"))
        })
        .transpose()?;

    Ok(Args {
        filename,
        line,
        col,
    })
}
