Skip to content

Commit

Permalink
*to_msg_pack works in interactive mode!
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 7, 2025
1 parent 52d1e36 commit bec65fc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
51 changes: 24 additions & 27 deletions fastsim-core/src/vehicle/hev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,48 +399,45 @@ impl Visitor<'_> for FCOnCausesVisitor {
where
E: de::Error,
{
let inner: String = v
.strip_prefix("[")
.ok_or("Missing leading `[`")
.map_err(|err| de::Error::custom(err))?
.strip_suffix("]")
.ok_or("Missing trailing`]`")
.map_err(|err| de::Error::custom(err))?
.to_string();
let fc_on_causes_unchecked = inner
.split(",")
.map(|x| FromStr::from_str(x.trim()))
.collect::<Vec<Result<FCOnCause, derive_more::FromStrError>>>();
let mut fc_on_causes: FCOnCauses = FCOnCauses(vec![]);
for fc_on_cause_unchecked in fc_on_causes_unchecked {
fc_on_causes
.0
.push(fc_on_cause_unchecked.map_err(|err| de::Error::custom(err))?)
}
Ok(fc_on_causes)
Self::visit_str(self, &v)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
let inner: String = v
.replace("\"", "")
.strip_prefix("[")
.ok_or("Missing leading `[`")
.map_err(|err| de::Error::custom(err))?
.strip_suffix("]")
.ok_or("Missing trailing`]`")
.map_err(|err| de::Error::custom(err))?
.to_string();
let fc_on_causes_unchecked = inner
.split(",")
.map(|x| FromStr::from_str(x.trim()))
.collect::<Vec<Result<FCOnCause, derive_more::FromStrError>>>();
let fc_on_causes_str = inner.split(",").map(|x| x.trim()).collect::<Vec<&str>>();
let fc_on_causes_unchecked = fc_on_causes_str
.iter()
.map(|x| {
if x.is_empty() {
None
} else {
Some(FromStr::from_str(x))
}
})
.collect::<Vec<Option<Result<FCOnCause, derive_more::FromStrError>>>>();
let mut fc_on_causes: FCOnCauses = FCOnCauses(vec![]);
for fc_on_cause_unchecked in fc_on_causes_unchecked {
fc_on_causes
.0
.push(fc_on_cause_unchecked.map_err(|err| de::Error::custom(err))?)
for (fc_on_cause_unchecked, fc_on_cause_str) in
fc_on_causes_unchecked.into_iter().zip(fc_on_causes_str)
{
if let Some(fc_on_cause_unchecked) = fc_on_cause_unchecked {
fc_on_causes.0.push(fc_on_cause_unchecked.map_err(|err| {
de::Error::custom(format!(
"{}\nfc_on_cause_unchecked: {:?}\nfc_on_cause_str: {}",
err, fc_on_cause_unchecked, fc_on_cause_str
))
})?)
}
}
Ok(fc_on_causes)
}
Expand Down
2 changes: 1 addition & 1 deletion python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def from_pydict(cls, pydict: Dict, data_fmt: str = "msg_pack") -> Self:
obj = cls.from_msg_pack(msgpack.packb(pydict))
except Exception as err:
print(
f"{err}\nThis is a known bug in interactive python sessions. Reverting to YAML.")
f"{err}\nFalling back to YAML.")
obj = cls.from_pydict(pydict, data_fmt="yaml")
case "json":
from json import dumps
Expand Down

0 comments on commit bec65fc

Please sign in to comment.