Skip to content

Commit

Permalink
fix: don't use Option to signal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Mar 9, 2021
1 parent f107bd5 commit 846c14f
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions serde_dhall/src/value.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::{BTreeMap, HashMap};
use std::result::Result as StdResult;

use dhall::builtins::Builtin;
use dhall::operations::OpKind;
Expand Down Expand Up @@ -212,13 +213,13 @@ impl Value {
x: &Nir<'cx>,
ty: &Nir<'cx>,
) -> Result<Self> {
Ok(if let Some(val) = SimpleValue::from_nir(x) {
Ok(if let Ok(val) = SimpleValue::from_nir(x) {
// The type must be simple if the value is simple.
let ty = SimpleType::from_nir(ty).unwrap();
Value {
kind: ValueKind::Val(val, Some(ty)),
}
} else if let Some(ty) = SimpleType::from_nir(x) {
} else if let Ok(ty) = SimpleType::from_nir(x) {
Value {
kind: ValueKind::Ty(ty),
}
Expand Down Expand Up @@ -256,9 +257,12 @@ impl Value {
}
}

#[derive(Debug)]
struct NotSimpleValue;

impl SimpleValue {
pub(crate) fn from_nir(nir: &Nir) -> Option<Self> {
Some(match nir.kind() {
fn from_nir(nir: &Nir) -> StdResult<Self, NotSimpleValue> {
Ok(match nir.kind() {
NirKind::Num(lit) => SimpleValue::Num(lit.clone()),
NirKind::TextLit(x) => SimpleValue::Text(
x.as_text()
Expand All @@ -275,7 +279,7 @@ impl SimpleValue {
&& kts.contains_key("mapKey")
&& kts.contains_key("mapValue")
{
return Some(SimpleValue::Record(Default::default()));
return Ok(SimpleValue::Record(Default::default()));
}
}
SimpleValue::List(vec![])
Expand Down Expand Up @@ -305,25 +309,27 @@ impl SimpleValue {
let v = Self::from_nir(
kvs.get("mapValue").unwrap(),
)?;
Some((k, v))
Ok((k, v))
}
_ => unreachable!("Internal type error"),
};
return Some(SimpleValue::Record(
return Ok(SimpleValue::Record(
xs.iter()
.map(convert_entry)
.collect::<Option<_>>()?,
.collect::<StdResult<_, _>>()?,
));
}
}
SimpleValue::List(
xs.iter().map(Self::from_nir).collect::<Option<_>>()?,
xs.iter()
.map(Self::from_nir)
.collect::<StdResult<_, _>>()?,
)
}
NirKind::RecordLit(kvs) => SimpleValue::Record(
kvs.iter()
.map(|(k, v)| Some((k.to_string(), Self::from_nir(v)?)))
.collect::<Option<_>>()?,
.map(|(k, v)| Ok((k.to_string(), Self::from_nir(v)?)))
.collect::<StdResult<_, _>>()?,
),
NirKind::UnionLit(field, x, _) => SimpleValue::Union(
field.into(),
Expand All @@ -334,7 +340,7 @@ impl SimpleValue {
{
SimpleValue::Union(field.into(), None)
}
_ => return None,
_ => return Err(NotSimpleValue),
})
}

Expand Down Expand Up @@ -450,9 +456,12 @@ impl SimpleValue {
}
}

#[derive(Debug)]
struct NotSimpleType;

impl SimpleType {
pub(crate) fn from_nir(nir: &Nir) -> Option<Self> {
Some(match nir.kind() {
fn from_nir(nir: &Nir) -> StdResult<Self, NotSimpleType> {
Ok(match nir.kind() {
NirKind::BuiltinType(b) => match b {
Builtin::Bool => SimpleType::Bool,
Builtin::Natural => SimpleType::Natural,
Expand All @@ -469,22 +478,20 @@ impl SimpleType {
}
NirKind::RecordType(kts) => SimpleType::Record(
kts.iter()
.map(|(k, v)| Some((k.into(), Self::from_nir(v)?)))
.collect::<Option<_>>()?,
.map(|(k, v)| Ok((k.into(), Self::from_nir(v)?)))
.collect::<StdResult<_, _>>()?,
),
NirKind::UnionType(kts) => SimpleType::Union(
kts.iter()
.map(|(k, v)| {
Some((
Ok((
k.into(),
v.as_ref()
.map(|v| Ok(Self::from_nir(v)?))
.transpose()?,
v.as_ref().map(Self::from_nir).transpose()?,
))
})
.collect::<Option<_>>()?,
.collect::<StdResult<_, _>>()?,
),
_ => return None,
_ => return Err(NotSimpleType),
})
}

Expand Down Expand Up @@ -565,7 +572,7 @@ impl std::fmt::Display for Value {
fn fmt(
&self,
f: &mut std::fmt::Formatter,
) -> std::result::Result<(), std::fmt::Error> {
) -> StdResult<(), std::fmt::Error> {
self.to_expr().fmt(f)
}
}
Expand All @@ -574,7 +581,7 @@ impl std::fmt::Display for SimpleType {
fn fmt(
&self,
f: &mut std::fmt::Formatter,
) -> std::result::Result<(), std::fmt::Error> {
) -> StdResult<(), std::fmt::Error> {
self.to_expr().fmt(f)
}
}
Expand Down

0 comments on commit 846c14f

Please sign in to comment.