fix(parser): Unify handling of AnyValue's type id

This commit is contained in:
Ed Page 2022-05-13 16:42:36 -05:00
parent 9920d5baf5
commit bc6da813bd
4 changed files with 10 additions and 20 deletions

View file

@ -1,7 +1,7 @@
use std::any::TypeId;
use std::sync::Arc;
use crate::parser::AnyValue;
use crate::parser::AnyValueId;
/// Parse/validate argument values
///
@ -174,15 +174,10 @@ impl ValueParser {
}
/// Describes the content of `Arc<Any>`
pub fn type_id(&self) -> TypeId {
pub fn type_id(&self) -> AnyValueId {
self.any_value_parser().type_id()
}
/// Describes the content of `Arc<Any>`
pub fn type_name(&self) -> &'static str {
self.any_value_parser().type_name()
}
/// Reflect on enumerated value properties
///
/// Error checking should not be done with this; it is mostly targeted at user-facing
@ -217,7 +212,7 @@ impl<'help> std::fmt::Debug for ValueParser {
ValueParserInner::String => f.debug_struct("ValueParser::string").finish(),
ValueParserInner::OsString => f.debug_struct("ValueParser::os_string").finish(),
ValueParserInner::PathBuf => f.debug_struct("ValueParser::path_buf").finish(),
ValueParserInner::Other(o) => write!(f, "ValueParser::other({})", o.type_name()),
ValueParserInner::Other(o) => write!(f, "ValueParser::other({:?})", o.type_id()),
}
}
}
@ -225,7 +220,7 @@ impl<'help> std::fmt::Debug for ValueParser {
// Require people to implement `TypedValueParser` rather than `AnyValueParser`:
// - Make implementing the user-facing trait easier
// - Enforce in the type-system that a given `AnyValueParser::parse` always returns the same type
// on each call and that it matches `type_id` / `type_name`
// on each call and that it matches `type_id`
/// Parse/validate argument values into a `Arc<Any>`
///
/// This is a type-erased wrapper for [`TypedValueParser`].
@ -251,10 +246,7 @@ pub trait AnyValueParser: private::AnyValueParserSealed {
) -> Result<AnyValue, crate::Error>;
/// Describes the content of `Arc<Any>`
fn type_id(&self) -> TypeId;
/// Describes the content of `Arc<Any>`
fn type_name(&self) -> &'static str;
fn type_id(&self) -> AnyValueId;
/// Reflect on enumerated value properties
///
@ -292,12 +284,8 @@ where
Ok(AnyValue::new(value))
}
fn type_id(&self) -> TypeId {
TypeId::of::<T>()
}
fn type_name(&self) -> &'static str {
std::any::type_name::<T>()
fn type_id(&self) -> AnyValueId {
AnyValueId::of::<T>()
}
fn possible_values(

View file

@ -25,7 +25,7 @@ impl std::fmt::Debug for AnyValue {
}
#[derive(Copy, Clone)]
pub(crate) struct AnyValueId {
pub struct AnyValueId {
type_id: std::any::TypeId,
#[cfg(debug_assertions)]
type_name: &'static str,

View file

@ -7,5 +7,6 @@ pub use arg_matches::{ArgMatches, Indices, OsValues, Values};
pub use value_source::ValueSource;
pub(crate) use any_value::AnyValue;
pub(crate) use any_value::AnyValueId;
pub(crate) use arg_matches::SubCommand;
pub(crate) use matched_arg::MatchedArg;

View file

@ -10,6 +10,7 @@ pub(crate) mod features;
pub(crate) use self::arg_matcher::ArgMatcher;
pub(crate) use self::matches::AnyValue;
pub(crate) use self::matches::AnyValueId;
pub(crate) use self::matches::{MatchedArg, SubCommand};
pub(crate) use self::parser::{ParseState, Parser};
pub(crate) use self::validator::Validator;