alevin_fry/
cmd_parse_utils.rs

1/*
2 * Copyright (c) 2020-2024 COMBINE-lab.
3 *
4 * This file is part of alevin-fry
5 * (see https://www.github.com/COMBINE-lab/alevin-fry).
6 *
7 * License: 3-clause BSD, see https://opensource.org/licenses/BSD-3-Clause
8 */
9use crate::quant::{ResolutionStrategy, SplicedAmbiguityModel};
10use clap;
11use std::path::{Path, PathBuf};
12
13impl clap::ValueEnum for ResolutionStrategy {
14    fn value_variants<'a>() -> &'a [Self] {
15        &[
16            Self::Trivial,
17            Self::CellRangerLike,
18            Self::CellRangerLikeEm,
19            Self::ParsimonyEm,
20            Self::Parsimony,
21            Self::ParsimonyGeneEm,
22            Self::ParsimonyGene,
23        ]
24    }
25
26    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
27        match self {
28            Self::Trivial => Some(clap::builder::PossibleValue::new("trivial")),
29            Self::CellRangerLike => Some(clap::builder::PossibleValue::new("cr-like")),
30            Self::CellRangerLikeEm => Some(clap::builder::PossibleValue::new("cr-like-em")),
31            Self::ParsimonyEm => Some(clap::builder::PossibleValue::new("parsimony-em")),
32            Self::Parsimony => Some(clap::builder::PossibleValue::new("parsimony")),
33            Self::ParsimonyGeneEm => Some(clap::builder::PossibleValue::new("parsimony-gene-em")),
34            Self::ParsimonyGene => Some(clap::builder::PossibleValue::new("parsimony-gene")),
35        }
36    }
37}
38
39impl clap::ValueEnum for SplicedAmbiguityModel {
40    fn value_variants<'a>() -> &'a [Self] {
41        &[Self::PreferAmbiguity, Self::WinnerTakeAll]
42    }
43
44    fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
45        match self {
46            Self::PreferAmbiguity => Some(clap::builder::PossibleValue::new("prefer-ambig")),
47            Self::WinnerTakeAll => Some(clap::builder::PossibleValue::new("winner-take-all")),
48        }
49    }
50}
51
52/// Checks if the path pointed to by v exists.  It can be
53/// any valid entity (e.g. disk file, FIFO, directory, etc.).
54/// If there is any issue with permissions or failure to properly
55/// resolve symlinks, or if the path is wrong, it returns
56/// an Err(String), else Ok(PathBuf).
57pub fn pathbuf_file_exists_validator(v: &str) -> Result<PathBuf, String> {
58    // NOTE: we explicitly *do not* check `is_file()` here
59    // since we want to return true even if the path is to
60    // a FIFO/named pipe.
61    if !Path::new(v).exists() {
62        Err(String::from("No valid file was found at this path."))
63    } else {
64        Ok(PathBuf::from(v))
65    }
66}
67
68/// Checks if the path pointed to by v exists and is
69/// a valid directory on disk.  If there is any issue
70/// with permissions or failure to properly
71/// resolve symlinks, or if the path is wrong, it returns
72/// an Err(String), else Ok(PathBuf).
73pub fn pathbuf_directory_exists_validator(v: &str) -> Result<PathBuf, String> {
74    if !Path::new(v).is_dir() {
75        Err(String::from("No valid directory was found at this path."))
76    } else {
77        Ok(PathBuf::from(v))
78    }
79}