argsplitter/
item.rs

1use std::{ffi::OsString, fmt};
2
3use crate::ArgError;
4
5#[cfg(doc)]
6use crate::{core::Core, ArgSplitter};
7
8/// * Item returned from [`Core::take_item`]
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum OwnedItem {
11    /// An argument that didn't start with a dash, or the special case `"-"`
12    Word(OsString),
13    /// Long option such as --verbose or --file=data.csv
14    Flag(String),
15}
16
17/// Item returned from [`ArgSplitter::item_os`]
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub enum ItemOs<'a> {
20    /// An argument that does not start with a dash
21    Word(OsString),
22    /// A short flag `-f` or a long flag `--file`. Includes the leading dashes.
23    Flag(&'a str),
24}
25
26/// Item returned from [`ArgSplitter::item`]
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub enum Item<'a> {
29    /// An argument that does not start with a dash
30    Word(String),
31    /// A short flag `-f` or a long flag `--file`. Includes the leading dashes.
32    Flag(&'a str),
33}
34
35impl fmt::Display for Item<'_> {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        match self {
38            Item::Flag(flag) => flag.fmt(f),
39            Item::Word(word) => word.fmt(f),
40        }
41    }
42}
43
44impl fmt::Display for ItemOs<'_> {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            ItemOs::Flag(flag) => flag.fmt(f),
48            ItemOs::Word(word) => word.to_string_lossy().fmt(f),
49        }
50    }
51}
52
53impl ItemOs<'_> {
54    /// Return [`ArgError::UnexpectedFlag`] or [`ArgError::UnexpectedArgument`]
55    /// depending on the type of item.
56    pub fn unexpected(&self) -> ArgError {
57        match self {
58            ItemOs::Flag(f) => ArgError::unknown_flag(f),
59            ItemOs::Word(w) => ArgError::unexpected_argument(w),
60        }
61    }
62}
63
64impl Item<'_> {
65    /// Return [`ArgError::UnexpectedFlag`] or [`ArgError::UnexpectedArgument`]
66    /// depending on the type of item.
67    pub fn unexpected(&self) -> ArgError {
68        match self {
69            Item::Flag(f) => ArgError::unknown_flag(f),
70            Item::Word(w) => ArgError::unexpected_argument(w),
71        }
72    }
73}