pub fn add(
        &mut self,
        correlation_id: CorrelationId,
        key: Key,
        value: StoredValue,
    ) -> Result<AddResult, R::Error> {
        let normalized_key = key.normalize();
        let current_value = match self.get(correlation_id, &normalized_key)? {
            None => return Ok(AddResult::KeyNotFound(normalized_key)),
            Some(current_value) => current_value,
        };

        let type_name = value.type_name();
        let mismatch = || {
            Ok(AddResult::TypeMismatch(StoredValueTypeMismatch::new(
                "I32, U64, U128, U256, U512 or (String, Key) tuple".to_string(),
                type_name,
            )))
        };

        let transform = match value {
            StoredValue::CLValue(cl_value) => match *cl_value.cl_type() {
                CLType::I32 => match cl_value.into_t() {
                    Ok(value) => Transform::AddInt32(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U64 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt64(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U128 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt128(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U256 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt256(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U512 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt512(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                _ => {
                    if *cl_value.cl_type() == casper_types::named_key_type() {
                        match cl_value.into_t() {
                            Ok(name_and_key) => {
                                let map = iter::once(name_and_key).collect();
                                Transform::AddKeys(map)
                            }
                            Err(error) => return Ok(AddResult::from(error)),
                        }
                    } else {
                        return mismatch();
                    }
                }
            },
            _ => return mismatch(),
        };

        match transform.clone().apply(current_value) {
            Ok(new_value) => {
                self.cache.insert_write(normalized_key, new_value);
                self.journal.push((normalized_key, transform));
                Ok(AddResult::Success)
            }
            Err(transform::Error::TypeMismatch(type_mismatch)) => {
                Ok(AddResult::TypeMismatch(type_mismatch))
            }
            Err(transform::Error::Serialization(error)) => Ok(AddResult::Serialization(error)),
        }
    }
/// Ok(None) represents missing key to which we want to "add" some value.
/// Ok(Some(unit)) represents successful operation.
/// Err(error) is reserved for unexpected errors when accessing global
/// state.
Ok(None) represents missing key to which we want to "add" some value.
Ok ( None ) represents missing key to which we want to " add " some value .
pub fn add(
        &mut self,
        correlation_id: CorrelationId,
        key: Key,
        value: StoredValue,
    ) -> Result<AddResult, R::Error> {
        let normalized_key = key.normalize();
        let current_value = match self.get(correlation_id, &normalized_key)? {
            None => return Ok(AddResult::KeyNotFound(normalized_key)),
            Some(current_value) => current_value,
        };
        let type_name = value.type_name();
        let mismatch = || {
            Ok(AddResult::TypeMismatch(StoredValueTypeMismatch::new(
                "I32, U64, U128, U256, U512 or (String, Key) tuple".to_string(),
                type_name,
            )))
        };
        let transform = match value {
            StoredValue::CLValue(cl_value) => match *cl_value.cl_type() {
                CLType::I32 => match cl_value.into_t() {
                    Ok(value) => Transform::AddInt32(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U64 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt64(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U128 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt128(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U256 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt256(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                CLType::U512 => match cl_value.into_t() {
                    Ok(value) => Transform::AddUInt512(value),
                    Err(error) => return Ok(AddResult::from(error)),
                },
                _ => {
                    if *cl_value.cl_type() == casper_types::named_key_type() {
                        match cl_value.into_t() {
                            Ok(name_and_key) => {
                                let map = iter::once(name_and_key).collect();
                                Transform::AddKeys(map)
                            }
                            Err(error) => return Ok(AddResult::from(error)),
                        }
                    } else {
                        return mismatch();
                    }
                }
            },
            _ => return mismatch(),
        };
        match transform.clone().apply(current_value) {
            Ok(new_value) => {
                self.cache.insert_write(normalized_key, new_value);
                self.journal.push((normalized_key, transform));
                Ok(AddResult::Success)
            }
            Err(transform::Error::TypeMismatch(type_mismatch)) => {
                Ok(AddResult::TypeMismatch(type_mismatch))
            }
            Err(transform::Error::Serialization(error)) => Ok(AddResult::Serialization(error)),
        }
    }
pub fn add ( & mut self , correlation_id : CorrelationId , key : Key , value : StoredValue , ) -> Result < AddResult , R :: Error > { let normalized_key = key . normalize ( ) ; let current_value = match self . get ( correlation_id , & normalized_key ) ? { None => return Ok ( AddResult :: KeyNotFound ( normalized_key ) ) , Some ( current_value ) => current_value , } ; let type_name = value . type_name ( ) ; let mismatch = | | { Ok ( AddResult :: TypeMismatch ( StoredValueTypeMismatch :: new ( " " . to_string ( ) , type_name , ) ) ) } ; let transform = match value { StoredValue :: CLValue ( cl_value ) => match * cl_value . cl_type ( ) { CLType :: I32 => match cl_value . into_t ( ) { Ok ( value ) => Transform :: AddInt32 ( value ) , Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } , CLType :: U64 => match cl_value . into_t ( ) { Ok ( value ) => Transform :: AddUInt64 ( value ) , Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } , CLType :: U128 => match cl_value . into_t ( ) { Ok ( value ) => Transform :: AddUInt128 ( value ) , Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } , CLType :: U256 => match cl_value . into_t ( ) { Ok ( value ) => Transform :: AddUInt256 ( value ) , Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } , CLType :: U512 => match cl_value . into_t ( ) { Ok ( value ) => Transform :: AddUInt512 ( value ) , Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } , _ => { if * cl_value . cl_type ( ) == casper_types :: named_key_type ( ) { match cl_value . into_t ( ) { Ok ( name_and_key ) => { let map = iter :: once ( name_and_key ) . collect ( ) ; Transform :: AddKeys ( map ) } Err ( error ) => return Ok ( AddResult :: from ( error ) ) , } } else { return mismatch ( ) ; } } } , _ => return mismatch ( ) , } ; match transform . clone ( ) . apply ( current_value ) { Ok ( new_value ) => { self . cache . insert_write ( normalized_key , new_value ) ; self . journal . push ( ( normalized_key , transform ) ) ; Ok ( AddResult :: Success ) } Err ( transform :: Error :: TypeMismatch ( type_mismatch ) ) => { Ok ( AddResult :: TypeMismatch ( type_mismatch ) ) } Err ( transform :: Error :: Serialization ( error ) ) => Ok ( AddResult :: Serialization ( error ) ) , } }
Ok(None) represents missing key to which we want to "add" some value.
Ok ( None ) represents missing key to which we want to " add " some value .
&mut self
correlation_id
CorrelationId
key
Key
value
StoredValue
&mut self
correlation_id
CorrelationId
key
Key
value
StoredValue