#[non_exhaustive]pub enum ConvertError {
#[non_exhaustive] KindMismatch {
want: Kind,
got: Kind,
},
#[non_exhaustive] TypeMismatch {
want: TypeCode,
got: TypeCode,
},
NotNull,
Convert(SharedError),
}Expand description
Represent failures in converting a Spanner Value to a Rust type.
§Example
let value = Value::from("not-a-bool");
let string_type = types::string();
let error = bool::from_value(&value, &string_type).expect_err("string is not a bool");
match error {
ConvertError::TypeMismatch { want, got, .. } => {
println!("Type mismatch: expected {want:?}, got {got:?}");
}
_ => {}
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
#[non_exhaustive]KindMismatch
The value kind is not as expected.
Fields
This variant is marked as non-exhaustive
#[non_exhaustive]TypeMismatch
The column type does not match the requested type.
Fields
This variant is marked as non-exhaustive
NotNull
The value is null, but the target type does not support nulls.
Convert(SharedError)
There was a problem during conversion.
Implementations§
Source§impl ConvertError
impl ConvertError
Sourcepub fn custom<E>(error: E) -> Self
pub fn custom<E>(error: E) -> Self
Creates a ConvertError::Convert wrapping a custom error.
This constructor is useful when implementing FromValue for custom domain or wrapper types.
§Example: Custom ID type
use google_cloud_spanner::types::Type;
use google_cloud_spanner::value::{ConvertError, FromValue, Value};
#[derive(Debug, PartialEq)]
struct CustomerId(u32);
impl FromValue for CustomerId {
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError> {
let raw_int = i64::from_value(value, spanner_type)?;
let parsed = u32::try_from(raw_int).map_err(ConvertError::custom)?;
Ok(CustomerId(parsed))
}
}Sourcepub fn message(message: impl Into<String>) -> Self
pub fn message(message: impl Into<String>) -> Self
Creates a ConvertError::Convert from a message string.
This constructor is useful when implementing FromValue for custom validation,
domain enums, or parsing formatted strings.
§Example: Validating numeric values
A custom wrapper type can deserialize a Spanner NUMERIC column as a String
and apply custom business validation, returning ConvertError::message on failure:
use google_cloud_spanner::types::{Type, TypeCode};
use google_cloud_spanner::value::{ConvertError, FromValue, Value};
/// A wrapper type that extracts a Spanner `NUMERIC` column, ensuring it is non-negative.
#[derive(Debug, PartialEq)]
struct PositiveNumeric(pub String);
impl FromValue for PositiveNumeric {
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError> {
if spanner_type.code() != TypeCode::Numeric {
return Err(ConvertError::type_mismatch(
TypeCode::Numeric,
spanner_type.code(),
));
}
let s = String::from_value(value, spanner_type)?;
if s.starts_with('-') {
return Err(ConvertError::message("numeric value must be non-negative"));
}
Ok(PositiveNumeric(s))
}
}§Example: Custom enum parsing
use google_cloud_spanner::types::Type;
use google_cloud_spanner::value::{ConvertError, FromValue, Value};
#[derive(Debug, PartialEq)]
enum OrderStatus {
Pending,
Shipped,
}
impl FromValue for OrderStatus {
fn from_value(value: &Value, spanner_type: &Type) -> Result<Self, ConvertError> {
let raw_status = String::from_value(value, spanner_type)?;
match raw_status.as_str() {
"PENDING" => Ok(OrderStatus::Pending),
"SHIPPED" => Ok(OrderStatus::Shipped),
other => Err(ConvertError::message(format!("unknown order status: '{other}'"))),
}
}
}Sourcepub fn kind_mismatch(want: Kind, got: Kind) -> Self
pub fn kind_mismatch(want: Kind, got: Kind) -> Self
Creates a ConvertError::KindMismatch for the expected and actual value kinds.
§Example
use google_cloud_spanner::error::ConvertError;
use google_cloud_spanner::value::Kind;
let error = ConvertError::kind_mismatch(Kind::String, Kind::Bool);
assert_eq!(error.to_string(), "expected String, got Bool");Sourcepub fn type_mismatch(want: TypeCode, got: TypeCode) -> Self
pub fn type_mismatch(want: TypeCode, got: TypeCode) -> Self
Creates a ConvertError::TypeMismatch for the expected and actual type codes.
This constructor is preferred when validating column schema types in custom
FromValue implementations.
§Example
use google_cloud_spanner::error::ConvertError;
use google_cloud_spanner::types::TypeCode;
let error = ConvertError::type_mismatch(TypeCode::Int64, TypeCode::String);
assert_eq!(error.to_string(), "type mismatch, expected Int64, got String");Sourcepub fn extract(err: &Error) -> Option<&Self>
pub fn extract(err: &Error) -> Option<&Self>
Extracts a ConvertError from a google_cloud_spanner::Error, if present.
§Example
let db_client = client.database_client("projects/p/instances/i/databases/d").build().await?;
let transaction = db_client.single_use().build();
let mut result_set = transaction
.execute_query(Statement::builder("SELECT 'not-a-number' AS text").build())
.await?;
if let Some(row) = result_set.next().await {
let result: Result<i64, _> = row?.get("text");
if let Err(error) = result {
if let Some(convert_error) = ConvertError::extract(&error) {
println!("Conversion failed: {convert_error}");
}
}
}Row deserialization returns Error with a source of RowError::TypeConversion
(wrapping ConvertError) or a direct ConvertError when value conversion fails.
This method traverses the error chain to extract the underlying ConvertError.
Trait Implementations§
Source§impl Clone for ConvertError
impl Clone for ConvertError
Source§impl Debug for ConvertError
impl Debug for ConvertError
Source§impl Display for ConvertError
impl Display for ConvertError
Source§impl Error for ConvertError
impl Error for ConvertError
Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Auto Trait Implementations§
impl !RefUnwindSafe for ConvertError
impl !UnwindSafe for ConvertError
impl Forget for ConvertError
impl Freeze for ConvertError
impl Move for ConvertError
impl Send for ConvertError
impl Sync for ConvertError
impl Unpin for ConvertError
impl UnsafeUnpin for ConvertError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request