From 1007a30ea44abc82902acc3b154945a615ca55ce Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Tue, 16 Apr 2024 19:13:47 +0200 Subject: [PATCH] CBOR convert no unreachables (#3883) --- src/rpc/format/cbor/convert.rs | 53 ++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/rpc/format/cbor/convert.rs b/src/rpc/format/cbor/convert.rs index 9d77f689..e272fa41 100644 --- a/src/rpc/format/cbor/convert.rs +++ b/src/rpc/format/cbor/convert.rs @@ -327,7 +327,7 @@ impl TryFrom for Cbor { Number::Decimal(v) => { Ok(Cbor(Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.to_string()))))) } - _ => unreachable!(), + _ => Err("Found an unsupported Number type being converted to CBOR"), }, Value::Strand(v) => Ok(Cbor(Data::Text(v.0))), Value::Duration(v) => { @@ -390,60 +390,75 @@ impl TryFrom for Cbor { Id::Generate(_) => { return Err("Cannot encode an ungenerated Record ID into CBOR") } - _ => unreachable!(), + _ => return Err("Found an unsupported Id type being converted to CBOR"), }, ])), ))), Value::Table(v) => Ok(Cbor(Data::Tag(TAG_TABLE, Box::new(Data::Text(v.0))))), - Value::Geometry(v) => Ok(Cbor(encode_geometry(v))), + Value::Geometry(v) => Ok(Cbor(encode_geometry(v)?)), // We shouldn't reach here _ => Err("Found unsupported SurrealQL value being encoded into a CBOR value"), } } } -fn encode_geometry(v: Geometry) -> Data { +fn encode_geometry(v: Geometry) -> Result { match v { - Geometry::Point(v) => Data::Tag( + Geometry::Point(v) => Ok(Data::Tag( TAG_GEOMETRY_POINT, Box::new(Data::Array(vec![ Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.x().to_string()))), Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.y().to_string()))), ])), - ), + )), Geometry::Line(v) => { - let data = v.points().map(|v| encode_geometry(v.into())).collect::>(); + let data = v + .points() + .map(|v| encode_geometry(v.into())) + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_LINE, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_LINE, Box::new(Data::Array(data)))) } Geometry::Polygon(v) => { let data = once(v.exterior()) .chain(v.interiors()) .map(|v| encode_geometry(v.clone().into())) - .collect::>(); + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_POLYGON, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_POLYGON, Box::new(Data::Array(data)))) } Geometry::MultiPoint(v) => { - let data = v.iter().map(|v| encode_geometry((*v).into())).collect::>(); + let data = v + .iter() + .map(|v| encode_geometry((*v).into())) + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_MULTIPOINT, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_MULTIPOINT, Box::new(Data::Array(data)))) } Geometry::MultiLine(v) => { - let data = v.iter().map(|v| encode_geometry(v.clone().into())).collect::>(); + let data = v + .iter() + .map(|v| encode_geometry(v.clone().into())) + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_MULTILINE, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_MULTILINE, Box::new(Data::Array(data)))) } Geometry::MultiPolygon(v) => { - let data = v.iter().map(|v| encode_geometry(v.clone().into())).collect::>(); + let data = v + .iter() + .map(|v| encode_geometry(v.clone().into())) + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_MULTIPOLYGON, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_MULTIPOLYGON, Box::new(Data::Array(data)))) } Geometry::Collection(v) => { - let data = v.iter().map(|v| encode_geometry(v.clone())).collect::>(); + let data = v + .iter() + .map(|v| encode_geometry(v.clone())) + .collect::, &'static str>>()?; - Data::Tag(TAG_GEOMETRY_COLLECTION, Box::new(Data::Array(data))) + Ok(Data::Tag(TAG_GEOMETRY_COLLECTION, Box::new(Data::Array(data)))) } - _ => unreachable!(), + _ => Err("Found an unsupported Geometry type being converted to CBOR"), } }