Skip to content

Commit

Permalink
mv type juggling for Thing to traits
Browse files Browse the repository at this point in the history
Replace the into_(place|npc)(_data)? functions with implementations of
the TryFrom trait, because that's what it's for.
  • Loading branch information
MikkelPaulson committed Sep 9, 2024
1 parent 0797334 commit f49e8a4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 51 deletions.
10 changes: 5 additions & 5 deletions core/src/storage/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::storage::{DataStore, MemoryDataStore};
use crate::time::Time;
use crate::utils::CaseInsensitiveStr;
use crate::world::npc::{NpcData, NpcRelations};
use crate::world::place::{PlaceData, PlaceRelations};
use crate::world::place::{Place, PlaceData, PlaceRelations};
use crate::world::thing::{Thing, ThingData, ThingRelations};
use crate::Uuid;
use futures::join;
Expand Down Expand Up @@ -180,9 +180,9 @@ impl Repository {

let parent = {
let parent_result = if let Some(uuid) = parent_uuid.value() {
self.get_by_uuid(uuid)
.await
.and_then(|record| record.thing.into_place().map_err(|_| Error::NotFound))
self.get_by_uuid(uuid).await.and_then(|record| {
Place::try_from(record.thing).map_err(|_| Error::NotFound)
})
} else {
Err(Error::NotFound)
};
Expand All @@ -198,7 +198,7 @@ impl Repository {
let grandparent = {
let grandparent_result = if let Some(uuid) = parent.data.location_uuid.value() {
self.get_by_uuid(uuid).await.and_then(|record| {
record.thing.into_place().map_err(|_| Error::NotFound)
Place::try_from(record.thing).map_err(|_| Error::NotFound)
})
} else {
Err(Error::NotFound)
Expand Down
108 changes: 62 additions & 46 deletions core/src/world/thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,6 @@ impl Thing {
self.data.gender()
}

pub fn into_place(self) -> Result<Place, Thing> {
if let ThingData::Place(place) = self.data {
Ok(Place {
uuid: self.uuid,
data: place,
})
} else {
Err(self)
}
}

pub fn into_npc(self) -> Result<Npc, Thing> {
if let ThingData::Npc(npc) = self.data {
Ok(Npc {
uuid: self.uuid,
data: npc,
})
} else {
Err(self)
}
}

pub fn display_summary(&self) -> SummaryView {
self.data.display_summary()
}
Expand Down Expand Up @@ -139,14 +117,6 @@ impl ThingData {
}
}

pub fn into_place_data(self) -> Result<PlaceData, ThingData> {
if let Self::Place(place) = self {
Ok(place)
} else {
Err(self)
}
}

pub fn npc_data(&self) -> Option<&NpcData> {
if let Self::Npc(npc) = self {
Some(npc)
Expand All @@ -155,14 +125,6 @@ impl ThingData {
}
}

pub fn into_npc_data(self) -> Result<NpcData, ThingData> {
if let Self::Npc(npc) = self {
Ok(npc)
} else {
Err(self)
}
}

pub fn display_summary(&self) -> SummaryView {
SummaryView(self)
}
Expand Down Expand Up @@ -215,6 +177,36 @@ impl From<Place> for Thing {
}
}

impl TryFrom<Thing> for Npc {
type Error = Thing;

fn try_from(thing: Thing) -> Result<Self, Self::Error> {
if let ThingData::Npc(npc) = thing.data {
Ok(Npc {
uuid: thing.uuid,
data: npc,
})
} else {
Err(thing)
}
}
}

impl TryFrom<Thing> for Place {
type Error = Thing;

fn try_from(thing: Thing) -> Result<Self, Self::Error> {
if let ThingData::Place(place) = thing.data {
Ok(Place {
uuid: thing.uuid,
data: place,
})
} else {
Err(thing)
}
}
}

impl From<NpcData> for ThingData {
fn from(npc: NpcData) -> Self {
ThingData::Npc(npc)
Expand All @@ -227,6 +219,30 @@ impl From<PlaceData> for ThingData {
}
}

impl TryFrom<ThingData> for NpcData {
type Error = ThingData;

fn try_from(thing_data: ThingData) -> Result<Self, Self::Error> {
if let ThingData::Npc(npc) = thing_data {
Ok(npc)
} else {
Err(thing_data)
}
}
}

impl TryFrom<ThingData> for PlaceData {
type Error = ThingData;

fn try_from(thing_data: ThingData) -> Result<Self, Self::Error> {
if let ThingData::Place(place) = thing_data {
Ok(place)
} else {
Err(thing_data)
}
}
}

impl From<NpcRelations> for ThingRelations {
fn from(input: NpcRelations) -> Self {
Self::Npc(input)
Expand Down Expand Up @@ -365,20 +381,20 @@ mod test {
let thing = place();
assert!(thing.data.place_data().is_some());
assert!(thing.data.npc_data().is_none());
assert!(thing.data.clone().into_place_data().is_ok());
assert!(thing.data.clone().into_npc_data().is_err());
assert!(thing.clone().into_place().is_ok());
assert!(thing.into_npc().is_err());
assert!(PlaceData::try_from(thing.data.clone()).is_ok());
assert!(NpcData::try_from(thing.data.clone()).is_err());
assert!(Place::try_from(thing.clone()).is_ok());
assert!(Npc::try_from(thing).is_err());
}

{
let thing = npc();
assert!(thing.data.npc_data().is_some());
assert!(thing.data.place_data().is_none());
assert!(thing.data.clone().into_npc_data().is_ok());
assert!(thing.data.clone().into_place_data().is_err());
assert!(thing.clone().into_npc().is_ok());
assert!(thing.into_place().is_err());
assert!(NpcData::try_from(thing.data.clone()).is_ok());
assert!(PlaceData::try_from(thing.data.clone()).is_err());
assert!(Npc::try_from(thing.clone()).is_ok());
assert!(Place::try_from(thing).is_err());
}
}

Expand Down

0 comments on commit f49e8a4

Please sign in to comment.