CBOR convert no unreachables (#3883)

This commit is contained in:
Micha de Vries 2024-04-16 19:13:47 +02:00 committed by GitHub
parent 64da2d8ee3
commit 1007a30ea4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -327,7 +327,7 @@ impl TryFrom<Value> for Cbor {
Number::Decimal(v) => { Number::Decimal(v) => {
Ok(Cbor(Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.to_string()))))) 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::Strand(v) => Ok(Cbor(Data::Text(v.0))),
Value::Duration(v) => { Value::Duration(v) => {
@ -390,60 +390,75 @@ impl TryFrom<Value> for Cbor {
Id::Generate(_) => { Id::Generate(_) => {
return Err("Cannot encode an ungenerated Record ID into CBOR") 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::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 // We shouldn't reach here
_ => Err("Found unsupported SurrealQL value being encoded into a CBOR value"), _ => Err("Found unsupported SurrealQL value being encoded into a CBOR value"),
} }
} }
} }
fn encode_geometry(v: Geometry) -> Data { fn encode_geometry(v: Geometry) -> Result<Data, &'static str> {
match v { match v {
Geometry::Point(v) => Data::Tag( Geometry::Point(v) => Ok(Data::Tag(
TAG_GEOMETRY_POINT, TAG_GEOMETRY_POINT,
Box::new(Data::Array(vec![ 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.x().to_string()))),
Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.y().to_string()))), Data::Tag(TAG_STRING_DECIMAL, Box::new(Data::Text(v.y().to_string()))),
])), ])),
), )),
Geometry::Line(v) => { Geometry::Line(v) => {
let data = v.points().map(|v| encode_geometry(v.into())).collect::<Vec<Data>>(); let data = v
.points()
.map(|v| encode_geometry(v.into()))
.collect::<Result<Vec<Data>, &'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) => { Geometry::Polygon(v) => {
let data = once(v.exterior()) let data = once(v.exterior())
.chain(v.interiors()) .chain(v.interiors())
.map(|v| encode_geometry(v.clone().into())) .map(|v| encode_geometry(v.clone().into()))
.collect::<Vec<Data>>(); .collect::<Result<Vec<Data>, &'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) => { Geometry::MultiPoint(v) => {
let data = v.iter().map(|v| encode_geometry((*v).into())).collect::<Vec<Data>>(); let data = v
.iter()
.map(|v| encode_geometry((*v).into()))
.collect::<Result<Vec<Data>, &'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) => { Geometry::MultiLine(v) => {
let data = v.iter().map(|v| encode_geometry(v.clone().into())).collect::<Vec<Data>>(); let data = v
.iter()
.map(|v| encode_geometry(v.clone().into()))
.collect::<Result<Vec<Data>, &'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) => { Geometry::MultiPolygon(v) => {
let data = v.iter().map(|v| encode_geometry(v.clone().into())).collect::<Vec<Data>>(); let data = v
.iter()
.map(|v| encode_geometry(v.clone().into()))
.collect::<Result<Vec<Data>, &'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) => { Geometry::Collection(v) => {
let data = v.iter().map(|v| encode_geometry(v.clone())).collect::<Vec<Data>>(); let data = v
.iter()
.map(|v| encode_geometry(v.clone()))
.collect::<Result<Vec<Data>, &'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"),
} }
} }