Skip to content

Commit 77edc5b

Browse files
committed
refactor(gctx): move source init logic to ValueSource
1 parent ad29820 commit 77edc5b

File tree

1 file changed

+27
-25
lines changed
  • src/cargo/util/context

1 file changed

+27
-25
lines changed

src/cargo/util/context/de.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ impl<'de, 'gctx> de::Deserializer<'de> for Deserializer<'gctx> {
129129
//
130130
// See more comments in `value.rs` for the protocol used here.
131131
if name == value::NAME && fields == value::FIELDS {
132-
return visitor.visit_map(ValueDeserializer::new(self)?);
132+
let source = ValueSource::with_deserializer(self)?;
133+
return visitor.visit_map(ValueDeserializer::new(source));
133134
}
134135
visitor.visit_map(ConfigMapAccess::new_struct(self, fields)?)
135136
}
@@ -480,20 +481,8 @@ enum ValueSource<'gctx> {
480481
ConfigValue(CV),
481482
}
482483

483-
/// This is a deserializer that deserializes into a `Value<T>` for
484-
/// configuration.
485-
///
486-
/// This is a special deserializer because it deserializes one of its struct
487-
/// fields into the location that this configuration value was defined in.
488-
///
489-
/// See more comments in `value.rs` for the protocol used here.
490-
struct ValueDeserializer<'gctx> {
491-
hits: u32,
492-
source: ValueSource<'gctx>,
493-
}
494-
495-
impl<'gctx> ValueDeserializer<'gctx> {
496-
fn new(de: Deserializer<'gctx>) -> Result<ValueDeserializer<'gctx>, ConfigError> {
484+
impl<'gctx> ValueSource<'gctx> {
485+
fn with_deserializer(de: Deserializer<'gctx>) -> Result<ValueSource<'gctx>, ConfigError> {
497486
// Figure out where this key is defined.
498487
let definition = {
499488
let env = de.key.as_env_key();
@@ -515,17 +504,29 @@ impl<'gctx> ValueDeserializer<'gctx> {
515504
}
516505
};
517506

518-
Ok(ValueDeserializer {
519-
hits: 0,
520-
source: ValueSource::Deserializer { de, definition },
521-
})
507+
Ok(Self::Deserializer { de, definition })
522508
}
523509

524-
fn with_cv(cv: CV) -> ValueDeserializer<'gctx> {
525-
ValueDeserializer {
526-
hits: 0,
527-
source: ValueSource::ConfigValue(cv),
528-
}
510+
fn with_cv(cv: CV) -> Self {
511+
Self::ConfigValue(cv)
512+
}
513+
}
514+
515+
/// This is a deserializer that deserializes into a `Value<T>` for
516+
/// configuration.
517+
///
518+
/// This is a special deserializer because it deserializes one of its struct
519+
/// fields into the location that this configuration value was defined in.
520+
///
521+
/// See more comments in `value.rs` for the protocol used here.
522+
struct ValueDeserializer<'gctx> {
523+
hits: u32,
524+
source: ValueSource<'gctx>,
525+
}
526+
527+
impl<'gctx> ValueDeserializer<'gctx> {
528+
fn new(source: ValueSource<'gctx>) -> ValueDeserializer<'gctx> {
529+
Self { hits: 0, source }
529530
}
530531

531532
fn definition(&self) -> &Definition {
@@ -625,7 +626,8 @@ impl<'de> de::Deserializer<'de> for ArrayItemDeserializer {
625626
//
626627
// See more comments in `value.rs` for the protocol used here.
627628
if name == value::NAME && fields == value::FIELDS {
628-
return visitor.visit_map(ValueDeserializer::with_cv(self.cv));
629+
let source = ValueSource::with_cv(self.cv);
630+
return visitor.visit_map(ValueDeserializer::new(source));
629631
}
630632
visitor.visit_map(ArrayItemMapAccess::with_struct(self.cv, fields))
631633
}

0 commit comments

Comments
 (0)