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) => {
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<Value> 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<Data, &'static str> {
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::<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) => {
let data = once(v.exterior())
.chain(v.interiors())
.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) => {
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) => {
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) => {
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) => {
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"),
}
}