pub trait ValueEnum: Sized + 'static {
const VARIANTS: &'static [Self];
const POSSIBLE_VALUES: &'static [&'static str];
#[doc(hidden)]
const __VARIANT_IDENTS: &'static [&'static str] = &[];
fn value_name(&self) -> &'static str;
fn from_value(value: &str) -> Option<Self>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct ParseValueEnumError;
impl core::fmt::Display for ParseValueEnumError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str("the value matches none of the possible values")
}
}
impl core::error::Error for ParseValueEnumError {}
#[doc(hidden)]
pub enum HelpPart {
Text(&'static str),
List(&'static [&'static str]),
}
const SEPARATOR: &str = ", ";
#[doc(hidden)]
pub const fn help_len(parts: &[HelpPart]) -> usize {
let mut length = 0;
let mut index = 0;
while index < parts.len() {
match parts[index] {
HelpPart::Text(text) => length += text.len(),
HelpPart::List(values) => {
let mut value = 0;
while value < values.len() {
if value > 0 {
length += SEPARATOR.len();
}
length += values[value].len();
value += 1;
}
}
}
index += 1;
}
length
}
const fn copy(bytes: &mut [u8], mut position: usize, text: &str) -> usize {
let text = text.as_bytes();
let mut index = 0;
while index < text.len() {
bytes[position] = text[index];
position += 1;
index += 1;
}
position
}
#[doc(hidden)]
pub const fn help_bytes<const N: usize>(parts: &[HelpPart]) -> [u8; N] {
let mut bytes = [0; N];
let mut position = 0;
let mut index = 0;
while index < parts.len() {
match parts[index] {
HelpPart::Text(text) => position = copy(&mut bytes, position, text),
HelpPart::List(values) => {
let mut value = 0;
while value < values.len() {
if value > 0 {
position = copy(&mut bytes, position, SEPARATOR);
}
position = copy(&mut bytes, position, values[value]);
value += 1;
}
}
}
index += 1;
}
bytes
}
#[doc(hidden)]
pub const fn help_str(bytes: &'static [u8]) -> &'static str {
match core::str::from_utf8(bytes) {
Ok(text) => text,
Err(_) => panic!("generated help is not valid UTF-8"),
}
}
const fn str_eq(left: &str, right: &str) -> bool {
let left = left.as_bytes();
let right = right.as_bytes();
if left.len() != right.len() {
return false;
}
let mut index = 0;
while index < left.len() {
if left[index] != right[index] {
return false;
}
index += 1;
}
true
}
#[doc(hidden)]
pub const fn value_name(
idents: &[&str],
values: &'static [&'static str],
ident: &str,
fallback: &'static str,
) -> &'static str {
let mut index = 0;
while index < idents.len() && index < values.len() {
if str_eq(idents[index], ident) {
return values[index];
}
index += 1;
}
fallback
}