Skip to content

Commit

Permalink
Minor formatting and error improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
Magicolo committed Feb 21, 2024
1 parent becda70 commit d5d2af9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "scalp"
version = "0.2.3"
version = "0.2.4"
authors = ["Magicolo <magicololand@gmail.com>"]
edition = "2021"
readme = "README.md"
Expand Down
17 changes: 7 additions & 10 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::parse::Key;
use core::fmt;
use std::{borrow::Cow, collections::VecDeque, error};

Expand All @@ -8,9 +9,9 @@ pub enum Error {
Author(Option<String>),
License(Option<String>),

MissingOptionValue(Option<Cow<'static, str>>, Option<Cow<'static, str>>),
MissingRequiredValue(Option<Cow<'static, str>>),
DuplicateOption(Option<Cow<'static, str>>),
MissingOptionValue(Option<Cow<'static, str>>, Option<Key>),
MissingRequiredValue(Option<Key>),
DuplicateOption(Option<Key>),
UnrecognizedArgument(Cow<'static, str>, Vec<(Cow<'static, str>, usize)>),
ExcessArguments(VecDeque<Cow<'static, str>>),
DuplicateName(String),
Expand All @@ -21,13 +22,9 @@ pub enum Error {
Cow<'static, str>,
Cow<'static, str>,
Option<Cow<'static, str>>,
Option<Cow<'static, str>>,
),
FailedToParseOptionValue(
Cow<'static, str>,
Option<Cow<'static, str>>,
Option<Cow<'static, str>>,
Option<Key>,
),
FailedToParseOptionValue(Cow<'static, str>, Option<Cow<'static, str>>, Option<Key>),
DuplicateNode,
GroupNestingLimitOverflow,
InvalidIndex(usize),
Expand All @@ -42,7 +39,7 @@ pub enum Error {
InvalidSwizzleOption(char),
InvalidOptionType(Cow<'static, str>),
InvalidInitialization,
InvalidOptionValue(Cow<'static, str>, Option<Cow<'static, str>>),
InvalidOptionValue(Cow<'static, str>, Option<Key>),
}

impl error::Error for Error {}
Expand Down
2 changes: 1 addition & 1 deletion src/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ impl<'a> Helper<'a> {
if width + buffer.len() > helper.width - helper.indent {
writeln!(helper.buffer)?;
helper.indentation()?;
} else {
} else if width > 0 {
write!(helper.buffer, " ")?;
}
writeln!(helper.buffer, "{Faint}{buffer}{Reset}")?;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod build;
pub mod build;
mod case;
mod error;
mod help;
pub mod meta;
mod parse;
pub mod parse;
pub mod scope;
mod spell;
mod stack;
Expand Down
46 changes: 41 additions & 5 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use core::{cmp::min, marker::PhantomData, num::NonZeroUsize};
use std::{
borrow::Cow,
collections::{HashMap, HashSet, VecDeque},
fmt,
str::FromStr,
};

Expand All @@ -17,7 +18,7 @@ pub struct State<'a> {
short: &'a str,
long: &'a str,
set: Option<&'a RegexSet>,
key: Option<&'a Cow<'static, str>>,
key: Option<&'a Key>,
meta: Option<&'a Meta>,
index: Option<usize>,
}
Expand Down Expand Up @@ -66,6 +67,12 @@ pub struct Default<P, T>(pub(crate) P, pub(crate) T);
pub struct Environment<P, F>(pub(crate) P, pub(crate) Cow<'static, str>, pub(crate) F);
pub struct At<P = ()>(pub(crate) P);

#[derive(Clone, PartialEq)]
pub enum Key {
At(usize),
Name(Cow<'static, str>),
}

pub trait Parse {
type State;
type Value;
Expand All @@ -78,6 +85,33 @@ pub trait Any<T> {
fn any(self) -> Option<T>;
}

impl fmt::Display for Key {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Key::At(position) => write!(f, "[{}]", position),
Key::Name(name) => write!(f, "{}", name),
}
}
}

impl From<&'static str> for Key {
fn from(name: &'static str) -> Self {
Key::Name(name.into())
}
}

impl From<String> for Key {
fn from(name: String) -> Self {
Key::Name(name.into())
}
}

impl From<usize> for Key {
fn from(position: usize) -> Self {
Key::At(position)
}
}

impl<T: Stack> Stack for At<T> {
const COUNT: usize = T::COUNT;
type Push<U> = At<T::Push<U>>;
Expand Down Expand Up @@ -169,7 +203,7 @@ impl<'a> State<'a> {
&'b mut self,
meta: Option<&'b Meta>,
set: Option<&'b RegexSet>,
key: Option<&'b Cow<'static, str>>,
key: Option<&'b Key>,
index: Option<usize>,
) -> State {
let mut state = self.own();
Expand Down Expand Up @@ -316,7 +350,7 @@ impl<P: Parse> Parse for Node<P> {
return self.parse.finalize((outer, state));
}

let mut positions = self.indices.positions.iter().copied();
let mut positions = self.indices.positions.iter().copied().enumerate();
while let Some(key) = state.key(&self.indices.swizzles)? {
match self.indices.indices.get(&key).copied() {
Some(HELP) => return Err(Error::Help(None)),
Expand All @@ -325,17 +359,19 @@ impl<P: Parse> Parse for Node<P> {
Some(AUTHOR) => return Err(Error::Author(None)),
Some(BREAK) => break,
Some(index) => {
let key = Key::Name(key);
outer = self.parse.parse((
outer,
state.with(Some(&self.meta), None, Some(&key), Some(index)),
))?
}
None => match positions.next() {
Some(index) => {
Some((i, index)) => {
state.restore(key);
let key = Key::At(i);
outer = self.parse.parse((
outer,
state.with(Some(&self.meta), None, None, Some(index)),
state.with(Some(&self.meta), None, Some(&key), Some(index)),
))?
}
None => {
Expand Down

0 comments on commit d5d2af9

Please sign in to comment.