Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkelPaulson committed Sep 2, 2024
1 parent 9334f29 commit ca9456d
Show file tree
Hide file tree
Showing 11 changed files with 216 additions and 220 deletions.
8 changes: 5 additions & 3 deletions core/src/app/command/tutorial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::app::{
CommandMatches, ContextAwareParse, Runnable,
};
use crate::reference::{ItemCategory, ReferenceCommand, Spell};
use crate::storage::{Change, StorageCommand};
use crate::storage::{Change, Record, StorageCommand};
use crate::time::TimeCommand;
use crate::utils::CaseInsensitiveStr;
use crate::world::npc::{Age, Ethnicity, Gender, NpcData, Species};
Expand Down Expand Up @@ -507,7 +507,8 @@ impl Runnable for TutorialCommand {
.unwrap()
.trim_start_matches(&[' ', '#'][..]);

let inn = app_meta.repository.get_by_name(inn_name).await.unwrap();
let Record { thing: inn, .. } =
app_meta.repository.get_by_name(inn_name).await.unwrap();

Ok((
ThingRef {
Expand Down Expand Up @@ -545,7 +546,8 @@ impl Runnable for TutorialCommand {
.unwrap()
.trim_start_matches(&[' ', '#'][..]);

let npc = app_meta.repository.get_by_name(npc_name).await.unwrap();
let Record { thing: npc, .. } =
app_meta.repository.get_by_name(npc_name).await.unwrap();

Ok((
ThingRef {
Expand Down
28 changes: 15 additions & 13 deletions core/src/storage/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::backup::export;
use super::{Change, RepositoryError};
use super::{Change, Record, RepositoryError};
use crate::app::{
AppMeta, Autocomplete, AutocompleteSuggestion, CommandAlias, CommandMatches, ContextAwareParse,
Event, Runnable,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Runnable for StorageCommand {
}
Self::Delete { name } => {
let result = match app_meta.repository.get_by_name(&name).await {
Ok(thing) => {
Ok(Record { thing, .. }) => {
app_meta
.repository
.modify(Change::Delete { uuid: thing.uuid, name: thing.name().to_string() })
Expand All @@ -92,7 +92,7 @@ impl Runnable for StorageCommand {
};

match result {
Ok(Some(thing)) => Ok(format!("{} was successfully deleted. Use `undo` to reverse this.", thing.name())),
Ok(Some(Record { thing, .. })) => Ok(format!("{} was successfully deleted. Use `undo` to reverse this.", thing.name())),
Ok(None) | Err(RepositoryError::NotFound) => Err(format!("There is no entity named \"{}\".", name)),
Err(_) => Err(format!("Couldn't delete `{}`.", name)),
}
Expand Down Expand Up @@ -120,10 +120,10 @@ impl Runnable for StorageCommand {
Ok("The file upload popup should appear momentarily. Please select a compatible JSON file, such as that produced by the `export` command.".to_string())
}
Self::Load { name } => {
let thing = app_meta.repository.get_by_name(&name).await;
let record = app_meta.repository.get_by_name(&name).await;
let mut save_command = None;
let output = if let Ok(thing) = thing {
if !thing.is_saved {
let output = if let Ok(Record { is_saved, thing }) = record {
if !is_saved {
save_command = Some(CommandAlias::literal(
"save",
format!("save {}", name),
Expand All @@ -150,15 +150,15 @@ impl Runnable for StorageCommand {
output
}
Self::Redo => match app_meta.repository.redo().await {
Some(Ok(thing)) => {
Some(Ok(option_record)) => {
let action = app_meta
.repository
.undo_history()
.next()
.unwrap()
.display_undo();

if let Some(thing) = thing {
if let Some(Record { thing, .. }) = option_record {
Ok(format!(
"{}\n\n_Successfully redid {}. Use `undo` to reverse this._",
thing.display_details(app_meta.repository.load_relations(&thing).await.unwrap_or_default()),
Expand All @@ -175,10 +175,10 @@ impl Runnable for StorageCommand {
None => Err("Nothing to redo.".to_string()),
},
Self::Undo => match app_meta.repository.undo().await {
Some(Ok(thing)) => {
Some(Ok(option_record)) => {
let action = app_meta.repository.get_redo().unwrap().display_redo();

if let Some(thing) = thing {
if let Some(Record { thing, .. }) = option_record {
Ok(format!(
"{}\n\n_Successfully undid {}. Use `redo` to reverse this._",
thing.display_details(app_meta.repository.load_relations(&thing).await.unwrap_or_default()),
Expand Down Expand Up @@ -305,7 +305,7 @@ impl Autocomplete for StorageCommand {
)
};

for (thing, prefix) in full_matches
for (record, prefix) in full_matches
.unwrap_or_default()
.iter()
.zip(repeat(""))
Expand All @@ -316,10 +316,12 @@ impl Autocomplete for StorageCommand {
.zip(repeat(prefix)),
)
{
if (prefix == "save " && thing.is_saved) || (prefix == "delete " && !thing.is_saved) {
if (prefix == "save " && record.is_saved) || (prefix == "delete " && !record.is_saved) {
continue;
}

let thing = &record.thing;

let suggestion_term = format!("{}{}", prefix, thing.name());
let matches = Self::parse_input(&suggestion_term, app_meta).await;

Expand All @@ -330,7 +332,7 @@ impl Autocomplete for StorageCommand {
Self::Delete { .. } => format!("remove {} from journal", thing.as_str()),
Self::Save { .. } => format!("save {} to journal", thing.as_str()),
Self::Load { .. } => {
if thing.is_saved {
if record.is_saved {
format!("{}", thing.display_description())
} else {
format!("{} (unsaved)", thing.display_description())
Expand Down
6 changes: 0 additions & 6 deletions core/src/storage/data_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ mod test {

let gandalf_the_grey = Npc {
uuid: TEST_UUID,
is_saved: true,
data: NpcData {
name: "Gandalf the Grey".into(),
..Default::default()
Expand All @@ -252,7 +251,6 @@ mod test {
ds.save_thing(
&Npc {
uuid: Uuid::new_v4(),
is_saved: true,
data: NpcData {
name: name.into(),
..Default::default()
Expand Down Expand Up @@ -291,7 +289,6 @@ mod test {

let gandalf_the_grey = Npc {
uuid: TEST_UUID,
is_saved: true,
data: NpcData {
name: "Gandalf the Grey".into(),
..Default::default()
Expand All @@ -300,7 +297,6 @@ mod test {

let gandalf_the_white = Npc {
uuid: TEST_UUID,
is_saved: true,
data: NpcData {
name: "Gandalf the White".into(),
..Default::default()
Expand Down Expand Up @@ -387,7 +383,6 @@ mod test {
fn person(uuid: Uuid) -> Thing {
Npc {
uuid,
is_saved: true,
data: NpcData::default(),
}
.into()
Expand All @@ -396,7 +391,6 @@ mod test {
fn place(uuid: Uuid) -> Thing {
Place {
uuid,
is_saved: true,
data: PlaceData::default(),
}
.into()
Expand Down
2 changes: 1 addition & 1 deletion core/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod backup;

pub use command::StorageCommand;
pub use data_store::{DataStore, MemoryDataStore, NullDataStore};
pub use repository::{Change, Error as RepositoryError, KeyValue, Repository};
pub use repository::{Change, Error as RepositoryError, KeyValue, Record, Repository};

mod command;
mod data_store;
Expand Down
Loading

0 comments on commit ca9456d

Please sign in to comment.