@@ -232,25 +232,13 @@ impl PreparedStatement {
232232 & ' ps self ,
233233 partitioner_name : & ' ps PartitionerName ,
234234 serialized_values : & ' ps SerializedValues ,
235- ) -> Result < Option < ( PartitionKey < ' ps > , Token ) > , QueryError > {
235+ ) -> Result < Option < ( PartitionKey < ' ps > , Token ) > , PartitionKeyError > {
236236 if !self . is_token_aware ( ) {
237237 return Ok ( None ) ;
238238 }
239239
240- let partition_key =
241- self . extract_partition_key ( serialized_values)
242- . map_err ( |err| match err {
243- PartitionKeyExtractionError :: NoPkIndexValue ( _, _) => {
244- ProtocolError :: PartitionKeyExtraction
245- }
246- } ) ?;
247- let token = partition_key
248- . calculate_token ( partitioner_name)
249- . map_err ( |err| match err {
250- TokenCalculationError :: ValueTooLong ( values_len) => {
251- QueryError :: BadQuery ( BadQuery :: ValuesTooLongForKey ( values_len, u16:: MAX . into ( ) ) )
252- }
253- } ) ?;
240+ let partition_key = self . extract_partition_key ( serialized_values) ?;
241+ let token = partition_key. calculate_token ( partitioner_name) ?;
254242
255243 Ok ( Some ( ( partition_key, token) ) )
256244 }
@@ -262,7 +250,10 @@ impl PreparedStatement {
262250 // As this function creates a `PartitionKey`, it is intended rather for external usage (by users).
263251 // For internal purposes, `PartitionKey::calculate_token()` is preferred, as `PartitionKey`
264252 // is either way used internally, among others for display in traces.
265- pub fn calculate_token ( & self , values : & impl SerializeRow ) -> Result < Option < Token > , QueryError > {
253+ pub fn calculate_token (
254+ & self ,
255+ values : & impl SerializeRow ,
256+ ) -> Result < Option < Token > , PartitionKeyError > {
266257 self . calculate_token_untyped ( & self . serialize_values ( values) ?)
267258 }
268259
@@ -271,7 +262,7 @@ impl PreparedStatement {
271262 pub ( crate ) fn calculate_token_untyped (
272263 & self ,
273264 values : & SerializedValues ,
274- ) -> Result < Option < Token > , QueryError > {
265+ ) -> Result < Option < Token > , PartitionKeyError > {
275266 self . extract_partition_key_and_calculate_token ( & self . partitioner_name , values)
276267 . map ( |opt| opt. map ( |( _pk, token) | token) )
277268 }
@@ -485,31 +476,37 @@ pub enum TokenCalculationError {
485476 ValueTooLong ( usize ) ,
486477}
487478
479+ /// An error returned by [`PreparedStatement::compute_partition_key()`].
488480#[ derive( Clone , Debug , Error ) ]
481+ #[ non_exhaustive]
489482pub enum PartitionKeyError {
483+ /// Failed to extract partition key.
490484 #[ error( transparent) ]
491- PartitionKeyExtraction ( PartitionKeyExtractionError ) ,
492- #[ error( transparent) ]
493- TokenCalculation ( TokenCalculationError ) ,
494- #[ error( transparent) ]
495- Serialization ( SerializationError ) ,
496- }
485+ PartitionKeyExtraction ( #[ from] PartitionKeyExtractionError ) ,
497486
498- impl From < PartitionKeyExtractionError > for PartitionKeyError {
499- fn from ( err : PartitionKeyExtractionError ) -> Self {
500- Self :: PartitionKeyExtraction ( err)
501- }
502- }
487+ /// Failed to calculate token.
488+ #[ error( transparent) ]
489+ TokenCalculation ( #[ from] TokenCalculationError ) ,
503490
504- impl From < TokenCalculationError > for PartitionKeyError {
505- fn from ( err : TokenCalculationError ) -> Self {
506- Self :: TokenCalculation ( err)
507- }
491+ /// Failed to serialize values required to compute partition key.
492+ #[ error( transparent) ]
493+ Serialization ( #[ from] SerializationError ) ,
508494}
509495
510- impl From < SerializationError > for PartitionKeyError {
511- fn from ( err : SerializationError ) -> Self {
512- Self :: Serialization ( err)
496+ impl PartitionKeyError {
497+ /// Converts the error to [`QueryError`].
498+ pub fn into_query_error ( self ) -> QueryError {
499+ match self {
500+ PartitionKeyError :: PartitionKeyExtraction ( _) => {
501+ QueryError :: ProtocolError ( ProtocolError :: PartitionKeyExtraction )
502+ }
503+ PartitionKeyError :: TokenCalculation ( TokenCalculationError :: ValueTooLong (
504+ values_len,
505+ ) ) => QueryError :: BadQuery ( BadQuery :: ValuesTooLongForKey ( values_len, u16:: MAX . into ( ) ) ) ,
506+ PartitionKeyError :: Serialization ( err) => {
507+ QueryError :: BadQuery ( BadQuery :: SerializationError ( err) )
508+ }
509+ }
513510 }
514511}
515512
0 commit comments