aboutsummaryrefslogtreecommitdiff
path: root/arg_picker/src/value
diff options
context:
space:
mode:
Diffstat (limited to 'arg_picker/src/value')
-rw-r--r--arg_picker/src/value/flag.rs146
-rw-r--r--arg_picker/src/value/paths.rs306
-rw-r--r--arg_picker/src/value/vec_until.rs137
3 files changed, 0 insertions, 589 deletions
diff --git a/arg_picker/src/value/flag.rs b/arg_picker/src/value/flag.rs
deleted file mode 100644
index 9a9e058..0000000
--- a/arg_picker/src/value/flag.rs
+++ /dev/null
@@ -1,146 +0,0 @@
-// Doc Not Optimize
-use std::{
- fmt::{Debug, Display},
- ops::{Deref, Not},
-};
-
-/// Parsed result of a boolean-style command-line flag.
-///
-/// `Flag` is a **value type** that can be declared in [`PickerArg`].
-/// When the user passes `--verbose` on the command line, the parsed result is `Flag::Active`;
-/// when the flag is absent, the result is `Flag::Inactive`.
-///
-/// # Why not just `bool`?
-///
-/// Unlike a raw `bool`, `Flag` carries **explicit semantics** about whether
-/// the flag was actually provided by the user (`Active`) or simply omitted
-/// (`Inactive`). This distinction matters when you want to distinguish
-/// "the user intentionally omitted the flag" from "the flag was processed but
-/// resolved to false" — the `Pickable` implementation for `Flag` always
-/// returns `Parsed(Flag::Inactive)` when no matching argument is found,
-/// rather than `NotFound`, making it always succeed with a meaningful default.
-///
-/// # Conversions
-///
-/// `Flag` interoperates seamlessly with `bool`: `Flag::Active` is `true`,
-/// `Flag::Inactive` is `false`. The [`Deref`] impl allows using a `Flag`
-/// directly in boolean contexts:
-///
-/// ```
-/// # use arg_picker::value::Flag;
-/// let flag = Flag::Active;
-/// if *flag { /* runs */ }
-/// ```
-///
-/// [`PickerArg`]: crate::PickerArg
-#[derive(Default, Clone, Copy, PartialEq, Eq)]
-pub enum Flag {
- /// The flag was **not** present on the command line.
- ///
- /// This is the default state, equivalent to `false`.
- #[default]
- Inactive,
-
- /// The flag **was** present on the command line.
- ///
- /// Equivalent to `true`.
- Active,
-}
-
-impl Debug for Flag {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- Self::Inactive => write!(f, "inactive"),
- Self::Active => write!(f, "active"),
- }
- }
-}
-
-impl Display for Flag {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- Self::Inactive => write!(f, "inactive"),
- Self::Active => write!(f, "active"),
- }
- }
-}
-
-impl Flag {
- /// Converts this `Flag` into a `bool`.
- ///
- /// Returns `true` if the flag is [`Active`], `false` if [`Inactive`].
- ///
- /// # Examples
- ///
- /// ```
- /// # use arg_picker::value::Flag;
- /// assert!(Flag::Active.bool());
- /// assert!(!Flag::Inactive.bool());
- /// ```
- ///
- /// [`Active`]: Flag::Active
- /// [`Inactive`]: Flag::Inactive
- #[must_use]
- #[inline]
- pub fn bool(&self) -> bool {
- *self == Self::Active
- }
-}
-
-impl PartialEq<bool> for Flag {
- fn eq(&self, other: &bool) -> bool {
- self.bool() == *other
- }
-}
-
-/// Compares `bool` with `Flag` using `==`.
-impl PartialEq<Flag> for bool {
- fn eq(&self, other: &Flag) -> bool {
- *self == other.bool()
- }
-}
-
-impl From<bool> for Flag {
- fn from(value: bool) -> Self {
- if value { Self::Active } else { Self::Inactive }
- }
-}
-
-impl From<Flag> for bool {
- fn from(val: Flag) -> Self {
- val == Flag::Active
- }
-}
-
-/// Allows `Flag` to be used in boolean contexts via `*flag`.
-///
-/// # Examples
-///
-/// ```
-/// # use arg_picker::value::Flag;
-/// let flag = Flag::Active;
-/// if *flag {
-/// println!("flag is set");
-/// }
-/// ```
-impl Deref for Flag {
- type Target = bool;
-
- fn deref(&self) -> &bool {
- match self {
- Self::Active => &true,
- Self::Inactive => &false,
- }
- }
-}
-
-impl Not for Flag {
- type Output = Self;
-
- fn not(self) -> Self {
- match self {
- Self::Active => Self::Inactive,
- Self::Inactive => Self::Active,
- }
- }
-}
diff --git a/arg_picker/src/value/paths.rs b/arg_picker/src/value/paths.rs
deleted file mode 100644
index 83f262d..0000000
--- a/arg_picker/src/value/paths.rs
+++ /dev/null
@@ -1,306 +0,0 @@
-// Doc Not Optimize
-use std::{
- ops::{Deref, DerefMut},
- path::{Path, PathBuf},
-};
-
-/// A file path.
-///
-/// `FilePath` is a wrapper type around `PathBuf` representing an arbitrary file path.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether the path exists and is a file.
-/// If not a file, returns `NotFound`.
-#[non_exhaustive]
-pub struct FilePath {
- path: PathBuf,
-}
-
-/// A file path that should not exist.
-///
-/// `NoFilePath` is a wrapper type around `PathBuf` representing an arbitrary path
-/// that must not currently point to an existing file.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether no file exists at the given path.
-/// If a file already exists (regardless of whether it is a regular file, directory,
-/// symlink, or other type), returns `NotFound`.
-#[non_exhaustive]
-pub struct NoFilePath {
- path: PathBuf,
-}
-
-/// A directory path.
-///
-/// `DirPath` is a wrapper type around `PathBuf` representing an arbitrary existing
-/// directory path.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether a directory exists at the given path.
-/// If no directory exists, returns `NotFound`.
-#[non_exhaustive]
-pub struct DirPath {
- path: PathBuf,
-}
-
-/// A directory path that should not exist.
-///
-/// `NoDirPath` is a wrapper type around `PathBuf` representing an arbitrary path
-/// that must not currently point to an existing directory.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether no directory exists at the given path.
-/// If a directory already exists (regardless of whether it is a regular file, file,
-/// symlink, or other type), returns `NotFound`.
-#[non_exhaustive]
-pub struct NoDirPath {
- path: PathBuf,
-}
-
-/// A symbolic link path.
-///
-/// `SymlinkPath` is a wrapper type around `PathBuf` representing an existing symbolic link.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether the path exists and is a symlink.
-/// If not a symlink, returns `NotFound`.
-#[non_exhaustive]
-pub struct SymlinkPath {
- path: PathBuf,
-}
-
-/// A symbolic link path that should not exist.
-///
-/// `NoSymlinkPath` is a wrapper type around `PathBuf` representing an arbitrary path
-/// that must not currently point to an existing symbolic link.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether no symlink exists at the given path.
-/// If a symlink already exists (regardless of whether it points to a file, directory,
-/// or other type), returns `NotFound`.
-#[non_exhaustive]
-pub struct NoSymlinkPath {
- path: PathBuf,
-}
-
-/// A path that should not exist at all.
-///
-/// `NoPath` is a wrapper type around `PathBuf` representing an arbitrary path
-/// that must not currently exist on the filesystem (as a file, directory, symlink,
-/// or any other type).
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// At the moment of parsing, checks whether any filesystem entry exists at the given path.
-/// If anything exists at that path, returns `NotFound`.
-#[non_exhaustive]
-pub struct NoPath {
- path: PathBuf,
-}
-
-/// Implements common trait impls (`From`, `AsRef`, `Deref`, `DerefMut`) for a path wrapper type.
-macro_rules! impl_path_traits {
- ($type:ident) => {
- impl From<PathBuf> for $type {
- fn from(value: PathBuf) -> Self {
- $type { path: value }
- }
- }
-
- impl From<&PathBuf> for $type {
- fn from(value: &PathBuf) -> Self {
- $type {
- path: value.clone(),
- }
- }
- }
-
- impl AsRef<Path> for $type {
- fn as_ref(&self) -> &Path {
- &self.path
- }
- }
-
- impl DerefMut for $type {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.path
- }
- }
-
- impl Deref for $type {
- type Target = PathBuf;
-
- fn deref(&self) -> &Self::Target {
- &self.path
- }
- }
-
- impl From<$type> for PathBuf {
- fn from(value: $type) -> Self {
- value.path
- }
- }
-
- impl From<&$type> for PathBuf {
- fn from(value: &$type) -> Self {
- value.path.clone()
- }
- }
- };
-}
-
-impl_path_traits!(FilePath);
-impl_path_traits!(NoFilePath);
-impl_path_traits!(DirPath);
-impl_path_traits!(NoDirPath);
-impl_path_traits!(SymlinkPath);
-impl_path_traits!(NoSymlinkPath);
-impl_path_traits!(NoPath);
-
-/// Recursive file paths.
-///
-/// `RecursiveFiles` is a wrapper type around `Vec<PathBuf>` representing a list of
-/// existing files.
-///
-/// It implements `Pickable` and can be parsed by a `Picker`.
-///
-/// # Parsing Behavior
-///
-/// - If a file path is given, returns a list of length 1 containing that file.
-/// - If a directory path is given, recursively collects all files under that directory
-/// and returns them as a list.
-#[non_exhaustive]
-pub struct RecursiveFiles {
- paths: Vec<PathBuf>,
-}
-
-impl From<Vec<PathBuf>> for RecursiveFiles {
- fn from(paths: Vec<PathBuf>) -> Self {
- Self { paths }
- }
-}
-
-impl From<RecursiveFiles> for Vec<PathBuf> {
- fn from(value: RecursiveFiles) -> Self {
- value.paths
- }
-}
-
-impl AsRef<[PathBuf]> for RecursiveFiles {
- fn as_ref(&self) -> &[PathBuf] {
- &self.paths
- }
-}
-
-impl Deref for RecursiveFiles {
- type Target = Vec<PathBuf>;
-
- fn deref(&self) -> &Self::Target {
- &self.paths
- }
-}
-
-impl DerefMut for RecursiveFiles {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.paths
- }
-}
-
-impl RecursiveFiles {
- /// Returns the number of file paths.
- #[must_use]
- pub const fn len(&self) -> usize {
- self.paths.len()
- }
-
- /// Returns `true` if there are no file paths.
- #[must_use]
- pub const fn is_empty(&self) -> bool {
- self.paths.is_empty()
- }
-
- /// Returns an iterator over the file paths.
- pub fn iter(&self) -> std::slice::Iter<'_, PathBuf> {
- self.paths.iter()
- }
-}
-
-impl<'a> IntoIterator for &'a RecursiveFiles {
- type Item = &'a PathBuf;
- type IntoIter = std::slice::Iter<'a, PathBuf>;
-
- fn into_iter(self) -> Self::IntoIter {
- self.iter()
- }
-}
-
-impl From<Vec<Self>> for RecursiveFiles {
- fn from(value: Vec<Self>) -> Self {
- Self {
- paths: value.into_iter().flat_map(|r| r.paths).collect(),
- }
- }
-}
-
-/// Trait for types that can be combined into a single `RecursiveFiles`.
-pub trait IntoRecursiveFiles {
- /// Combines multiple sources of file paths into a single `RecursiveFiles`.
- fn combine(self) -> RecursiveFiles;
-}
-
-impl<T> IntoRecursiveFiles for Vec<T>
-where
- T: Into<RecursiveFiles>,
-{
- fn combine(self) -> RecursiveFiles {
- self.into_iter()
- .map(Into::into)
- .collect::<Vec<RecursiveFiles>>()
- .into()
- }
-}
-
-impl<T> IntoRecursiveFiles for &[T]
-where
- T: Into<RecursiveFiles> + Clone,
-{
- fn combine(self) -> RecursiveFiles {
- self.iter()
- .cloned()
- .map(Into::into)
- .collect::<Vec<RecursiveFiles>>()
- .into()
- }
-}
-
-impl<T, const N: usize> IntoRecursiveFiles for [T; N]
-where
- T: Into<RecursiveFiles>,
-{
- fn combine(self) -> RecursiveFiles {
- self.into_iter()
- .map(Into::into)
- .collect::<Vec<RecursiveFiles>>()
- .into()
- }
-}
diff --git a/arg_picker/src/value/vec_until.rs b/arg_picker/src/value/vec_until.rs
deleted file mode 100644
index 0f7fede..0000000
--- a/arg_picker/src/value/vec_until.rs
+++ /dev/null
@@ -1,137 +0,0 @@
-// Doc Not Optimize
-use std::marker::PhantomData;
-use std::ops::{Deref, DerefMut};
-
-use crate::{
- BoundaryCheck, MultiPickableWithBoundary, Pickable, PickerArg, PickerArgAttr, PickerArgResult,
- SinglePickable, TagPhaseContext,
- matcher_needed::Matcher,
- parselib::{MultiArgMatcher, ParserStyle},
-};
-
-/// A `Vec`-like container that stops collecting when [`BoundaryCheck`]
-/// returns `true`.
-///
-/// This type exists to signal "I know what I'm doing with boundaries"
-/// at the type level (as opposed to `Vec<T>` which greedily takes
-/// everything).
-#[derive(Debug, Clone, PartialEq, Eq, Default)]
-pub struct VecUntil<T> {
- pub(crate) inner: Vec<T>,
- _marker: PhantomData<T>,
-}
-
-impl<T> VecUntil<T> {
- /// Consumes `self` and returns the underlying [`Vec<T>`].
- #[must_use]
- pub fn into_inner(self) -> Vec<T> {
- self.inner
- }
-}
-
-impl<T> From<Vec<T>> for VecUntil<T> {
- fn from(v: Vec<T>) -> Self {
- Self {
- inner: v,
- _marker: PhantomData,
- }
- }
-}
-
-impl<T> From<VecUntil<T>> for Vec<T> {
- fn from(v: VecUntil<T>) -> Self {
- v.inner
- }
-}
-
-impl<T> Deref for VecUntil<T> {
- type Target = Vec<T>;
- fn deref(&self) -> &Vec<T> {
- &self.inner
- }
-}
-
-impl<T> DerefMut for VecUntil<T> {
- fn deref_mut(&mut self) -> &mut Vec<T> {
- &mut self.inner
- }
-}
-
-// MultiPickableWithBoundary impl
-
-impl<T> MultiPickableWithBoundary for VecUntil<T>
-where
- T: SinglePickable + BoundaryCheck,
-{
- type Checker = T;
-
- fn pick_multi(raw: Vec<String>) -> PickerArgResult<Self> {
- let mut inner = Vec::with_capacity(raw.len());
- for s in &raw {
- match T::pick_single(Some(s)) {
- PickerArgResult::Parsed(v) => inner.push(v),
- PickerArgResult::NotFound => return PickerArgResult::NotFound,
- PickerArgResult::Unparsed => {}
- }
- }
- PickerArgResult::Parsed(Self {
- inner,
- _marker: PhantomData,
- })
- }
-}
-
-// Pickable impl
-
-impl<'a, T> Pickable<'a> for VecUntil<T>
-where
- T: SinglePickable + BoundaryCheck,
-{
- fn get_attr(flag: &'a PickerArg<'a, Self>) -> PickerArgAttr {
- PickerArgAttr::positional_or_multi(flag)
- }
-
- fn tag(ctx: TagPhaseContext) -> Vec<usize> {
- let args = ctx.args;
- let is_positional = ctx.arg_info.positional;
- let positions = MultiArgMatcher::match_all(ctx.into());
- if positions.is_empty() {
- return positions;
- }
-
- let start = usize::from(!is_positional);
- if start >= positions.len() {
- return positions;
- }
-
- let mut cut = start;
- for &idx in &positions[start..] {
- if let Some(raw) = args.get(idx)
- && T::check_boundary(raw)
- {
- break;
- }
- cut += 1;
- }
-
- positions[..cut].to_vec()
- }
-
- fn pick(raw_strs: &[&str]) -> PickerArgResult<Self> {
- let strs = strip_flag(raw_strs);
- let owned: Vec<String> = strs.iter().map(|&s| s.to_string()).collect();
- <Self as MultiPickableWithBoundary>::pick_multi(owned)
- }
-}
-
-/// If the first raw string looks like a named flag (starts with the
-/// style's long or short prefix), strip it — it's the flag, not a value.
-fn strip_flag<'a>(raw_strs: &'a [&'a str]) -> &'a [&'a str] {
- if let Some(first) = raw_strs.first() {
- let style = ParserStyle::global_style();
- if first.starts_with(style.long_prefix) || first.starts_with(style.short_prefix) {
- return &raw_strs[1..];
- }
- }
- raw_strs
-}