1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
mod edge;
mod field;
pub mod query;
mod value;

use crate::AsAny;
pub use edge::*;
pub use field::*;
pub use query::*;
pub use value::*;

use crate::{DatabaseError, DatabaseResult, Id, WeakDatabaseRc, EPHEMERAL_ID};
use derive_more::{Display, Error};
use dyn_clone::DynClone;
use std::{
    collections::{hash_map::Entry, HashMap, HashSet},
    fmt,
    time::{SystemTime, SystemTimeError, UNIX_EPOCH},
};

/// Represents some error the can occur when mutating an ent
#[derive(Debug, Display, Error)]
pub enum EntMutationError {
    #[display(fmt = "{}", source)]
    BadEdgeValueMutation { source: EdgeValueMutationError },

    #[display(fmt = "Given value for field is wrong type: {}", description)]
    WrongValueType { description: String },

    #[display(fmt = "Given edge value for edge is wrong type: {}", description)]
    WrongEdgeValueType { description: String },

    #[display(fmt = "No edge with name: {}", name)]
    NoEdge { name: String },

    #[display(fmt = "No field with name: {}", name)]
    NoField { name: String },

    #[display(fmt = "Field cannot be updated as it is immutable: {}", name)]
    FieldImmutable { name: String },

    #[display(fmt = "Field cannot be updated as it is computed: {}", name)]
    FieldComputed { name: String },

    #[display(fmt = "Failed to mark ent as updated: {}", source)]
    MarkUpdatedFailed { source: SystemTimeError },
}

/// Represents some error that can occur when converting an ent to another type
#[derive(Debug, Display, Error)]
pub enum EntConversionError {
    #[display(fmt = "Expected ent of type {}, but got {}", expected, actual)]
    EntWrongType { expected: String, actual: String },
    #[display(fmt = "Missing field {}", name)]
    FieldMissing { name: String },
    #[display(fmt = "Expected field {} to be {}, but was {}", name, expected, actual)]
    FieldWrongType {
        name: String,
        expected: ValueType,
        actual: ValueType,
    },
    #[display(fmt = "Missing edge {}", name)]
    EdgeMissing { name: String },
    #[display(fmt = "Expected edge {} to be {}, but was {}", name, expected, actual)]
    EdgeWrongType {
        name: String,
        expected: EdgeValueType,
        actual: EdgeValueType,
    },
}

/// Represents data about an ent type
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EntTypeData {
    /// Indicates an ent is a concrete type with the given static str
    /// being unique to its type
    Concrete { ty: &'static str },

    /// Indicates an ent is a wrapper around other ent types with the given
    /// static slice of static strs representing the types it wraps
    Wrapper {
        ty: &'static str,
        wrapped_tys: HashSet<&'static str>,
    },
}

/// Represents the interface for an Ent to report its type. This should align
/// with [`Ent::r#type()`] method and is used when we must know the type
/// without having an instance of an ent.
pub trait EntType {
    /// Represents data about the ent's type
    fn type_data() -> EntTypeData;

    /// Returns a static str that represents the unique type for an ent
    fn type_str() -> &'static str {
        match Self::type_data() {
            EntTypeData::Concrete { ty } => ty,
            EntTypeData::Wrapper { ty, wrapped_tys: _ } => ty,
        }
    }

    /// Indicates whether the ent represents a concrete type (like a struct)
    /// or a wrapper around one of many types (like an enum)
    fn is_concrete_type() -> bool {
        matches!(Self::type_data(), EntTypeData::Concrete { .. })
    }

    /// Represents the types that this ent wraps if it is not a concrete ent
    fn wrapped_tys() -> Option<HashSet<&'static str>> {
        match Self::type_data() {
            EntTypeData::Concrete { .. } => None,
            EntTypeData::Wrapper { ty: _, wrapped_tys } => Some(wrapped_tys),
        }
    }
}

/// Represents a wrapper around some set of ents that implement [`Ent`],
/// useful for edges that can return one of many different types that are
/// variants of an enum
pub trait EntWrapper: Sized {
    /// Returns Some(impl EntWrapper) if the wrapper is able to wrap around
    /// the given [`Ent`] trait object, otherwise returns None
    fn wrap_ent(ent: Box<dyn Ent>) -> Option<Self>;

    /// Returns true if able to wrap the given ent trait object, otherwise
    /// returns false
    fn can_wrap_ent(ent: &dyn Ent) -> bool;
}

/// Represents a builder interface for some ent, capable of building a new
/// ent instance and also saving the new ent to the database at the same time
pub trait EntBuilder {
    /// The ent that will be created
    type Output: Ent;

    /// The ent-specific builder errors that can occur
    type Error;

    /// Consumes the builder and creates a new instance of the ent
    fn finish(self) -> Result<Self::Output, Self::Error>;

    /// Consumes the builder, creates a new instance of the ent, and saves
    /// it to the database referenced by the ent
    fn finish_and_commit(self) -> Result<DatabaseResult<Self::Output>, Self::Error>;
}

/// Represents an interface to load some ent from a database
pub trait EntLoader {
    /// The ent that will be loaded
    type Output: Ent;

    /// Retrieves the ent instance with the specified id from the
    /// provided database, returning none if ent not found
    fn load_from_db(db: WeakDatabaseRc, id: Id) -> DatabaseResult<Option<Self::Output>>;

    /// Retrieves the ent instance with the specified id from the
    /// provided database, converting none into a missing ent error
    fn load_from_db_strict(db: WeakDatabaseRc, id: Id) -> DatabaseResult<Self::Output> {
        let maybe_ent = Self::load_from_db(db, id)?;

        match maybe_ent {
            Some(ent) => Ok(ent),
            None => Err(DatabaseError::MissingEnt { id }),
        }
    }

    /// Retrieves the ent instance with the specified id from
    /// the global database, returning none if ent not found
    fn load(id: Id) -> DatabaseResult<Option<Self::Output>> {
        Self::load_from_db(crate::global::db(), id)
    }

    /// Retrieves the ent instance with the specified id from
    /// the global database, converting none into a missing ent error
    fn load_strict(id: Id) -> DatabaseResult<Self::Output> {
        Self::load_from_db_strict(crate::global::db(), id)
    }
}

/// Represents the interface for a generic entity whose fields and edges
/// can be accessed by str name regardless of compile-time characteristics
///
/// Based on https://www.usenix.org/system/files/conference/atc13/atc13-bronson.pdf
#[cfg_attr(feature = "serde-1", typetag::serde(tag = "type"))]
pub trait Ent: AsAny + DynClone + Send + Sync {
    /// Represents the unique id associated with each entity instance
    fn id(&self) -> Id;

    /// Updates the id of this ent, useful for databases that want to adjust
    /// the id or when you want to produce a clone of the ent in a database
    /// by resetting its id to ephemeral prior to storing it
    fn set_id(&mut self, id: Id);

    /// Represents a unique type associated with the entity, used for
    /// lookups, indexing by type, and conversions
    fn r#type(&self) -> &str;

    /// Represents the time when the instance of the ent was created
    /// as the milliseconds since epoch (1970-01-01 00:00:00 UTC)
    fn created(&self) -> u64;

    /// Represents the time when the instance of the ent was last updated
    /// as the milliseconds since epoch (1970-01-01 00:00:00 UTC)
    fn last_updated(&self) -> u64;

    /// Updates the time when the instance of the ent was last updated to
    /// the current time in milliseconds since epoch (1970-01-01 00:00:00 UTC)
    fn mark_updated(&mut self) -> Result<(), EntMutationError>;

    /// Returns a list of definitions for fields contained by the ent
    fn field_definitions(&self) -> Vec<FieldDefinition>;

    /// Returns definition for the field with specified name
    fn field_definition(&self, name: &str) -> Option<FieldDefinition> {
        self.field_definitions()
            .into_iter()
            .find(|f| f.name() == name)
    }

    /// Returns a list of names of fields contained by the ent
    fn field_names(&self) -> Vec<String> {
        self.field_definitions()
            .into_iter()
            .map(|fd| fd.name)
            .collect()
    }

    /// Returns a copy of the value of the field with the specified name
    fn field(&self, name: &str) -> Option<Value>;

    /// Returns a copy of the type of the field with the specified name
    fn field_type(&self, name: &str) -> Option<ValueType> {
        self.field_definition(name).map(|f| f.r#type().clone())
    }

    /// Returns a copy of all fields contained by the ent and their associated values
    fn fields(&self) -> Vec<Field> {
        let mut fields = Vec::new();
        for name in self.field_names() {
            if let Some(value) = self.field(&name) {
                fields.push(Field::new(name, value));
            }
        }
        fields
    }

    /// Updates the local value of a field with the specified name, returning
    /// the old field value if updated. This will also update the last updated
    /// time for the ent.
    fn update_field(&mut self, name: &str, value: Value) -> Result<Value, EntMutationError>;

    /// Returns a list of definitions for edges contained by the ent
    fn edge_definitions(&self) -> Vec<EdgeDefinition>;

    /// Returns definition for the edge with specified name
    fn edge_definition(&self, name: &str) -> Option<EdgeDefinition> {
        self.edge_definitions()
            .into_iter()
            .find(|f| f.name() == name)
    }

    /// Returns a list of names of edges contained by the ent
    fn edge_names(&self) -> Vec<String> {
        self.edge_definitions()
            .into_iter()
            .map(|ed| ed.name)
            .collect()
    }

    /// Returns a copy of the value of the edge with the specified name
    fn edge(&self, name: &str) -> Option<EdgeValue>;

    /// Returns a copy of the type of the edge with the specified name
    fn edge_type(&self, name: &str) -> Option<EdgeValueType> {
        self.edge_definitions().into_iter().find_map(|e| {
            if e.name() == name {
                Some(*e.r#type())
            } else {
                None
            }
        })
    }

    /// Returns a copy of all edges contained by the ent and their associated values
    fn edges(&self) -> Vec<Edge> {
        let mut edges = Vec::new();
        for name in self.edge_names() {
            if let Some(value) = self.edge(&name) {
                edges.push(Edge::new(name, value));
            }
        }
        edges
    }

    /// Updates the local value of an edge with the specified name, returning
    /// the old edge value if updated. This will also update the last updated
    /// time for the ent.
    fn update_edge(&mut self, name: &str, value: EdgeValue) -> Result<EdgeValue, EntMutationError>;

    /// Connects ent to the given database so all future
    /// database-related operations will be performed against this database
    fn connect(&mut self, database: WeakDatabaseRc);

    /// Disconnects ent from any associated database. All future database
    /// operations will fail with a disconnected database error
    fn disconnect(&mut self);

    /// Returns true if ent is currently connected to a database
    fn is_connected(&self) -> bool;

    /// Loads the ents connected by the edge with the given name
    ///
    /// Requires ent to be connected to a database
    fn load_edge(&self, name: &str) -> DatabaseResult<Vec<Box<dyn Ent>>>;

    /// Clears out any locally-cached data for the ent
    fn clear_cache(&mut self);

    /// Refreshes ent by checking database for latest version and returning it
    ///
    /// Requires ent to be connected to a database
    fn refresh(&mut self) -> DatabaseResult<()>;

    /// Saves the ent to the database, updating this local instance's id
    /// if the database has reported a new id
    ///
    /// Requires ent to be connected to a database
    fn commit(&mut self) -> DatabaseResult<()>;

    /// Removes self from database, returning true if successful
    ///
    /// Requires ent to be connected to a database
    fn remove(&self) -> DatabaseResult<bool>;
}

dyn_clone::clone_trait_object!(Ent);

/// Implementation for a generic trait object of [`Ent`] that provides
/// methods to downcast into a concrete type
impl dyn Ent {
    /// Attempts to convert this dynamic Ent ref into a concrete Ent ref
    /// by downcasting
    pub fn as_ent<E: Ent>(&self) -> Option<&E> {
        self.as_any().downcast_ref::<E>()
    }

    /// Attempts to convert this dynamic Ent mutable ref into a concrete Ent
    /// mutable ref by downcasting
    pub fn as_mut_ent<E: Ent>(&mut self) -> Option<&mut E> {
        self.as_mut_any().downcast_mut::<E>()
    }

    /// Attempts to convert this dynamic Ent ref into a concrete ent by
    /// downcasting and then cloning
    pub fn to_ent<E: Ent>(&self) -> Option<E> {
        self.as_ent().map(dyn_clone::clone)
    }
}

pub trait EntExt: Ent {
    /// Loads ents of a specified type from a named edge
    fn load_edge_typed<E: Ent>(&self, name: &str) -> DatabaseResult<Vec<E>>;
}

impl<T: Ent> EntExt for T {
    fn load_edge_typed<E: Ent>(&self, name: &str) -> DatabaseResult<Vec<E>> {
        self.load_edge(name).map(|ents| {
            ents.into_iter()
                .filter_map(|ent| ent.to_ent::<E>())
                .collect()
        })
    }
}

/// Represents a general-purpose ent that is shapeless (no hard type) and
/// maintains fields and edges using internal maps. This ent can optionally
/// be connected to a database and supports additional functionality like
/// loading ents from edges when connected.
#[derive(Clone, Display)]
#[display(fmt = "{} {}", "Self::type_str()", id)]
#[cfg_attr(feature = "serde-1", derive(serde::Serialize, serde::Deserialize))]
pub struct UntypedEnt {
    #[cfg_attr(feature = "serde-1", serde(skip))]
    database: WeakDatabaseRc,
    id: Id,
    fields: HashMap<String, Field>,
    edges: HashMap<String, Edge>,
    created: u64,
    last_updated: u64,
}

impl fmt::Debug for UntypedEnt {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UntypedEnt")
            .field("id", &self.id)
            .field("fields", &self.fields)
            .field("edges", &self.edges)
            .field("created", &self.created)
            .field("last_updated", &self.last_updated)
            .finish()
    }
}

impl Eq for UntypedEnt {}

impl PartialEq for UntypedEnt {
    /// Untyped Ents are considered equal if their ids, fields, edges, creation
    /// date, and updated date are all equal
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.fields == other.fields
            && self.edges == other.edges
            && self.created == other.created
            && self.last_updated == other.last_updated
    }
}

impl UntypedEnt {
    /// Creates a new ent using the given id, field map, and edge map
    pub fn new(id: Id, fields: HashMap<String, Field>, edges: HashMap<String, Edge>) -> Self {
        Self {
            database: WeakDatabaseRc::new(),
            id,
            fields,
            edges,
            created: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("Invalid system time")
                .as_millis() as u64,
            last_updated: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("Invalid system time")
                .as_millis() as u64,
        }
    }

    /// Creates an empty ent with the provided id
    pub fn empty_with_id(id: Id) -> Self {
        Self::new(id, HashMap::new(), HashMap::new())
    }

    /// Creates a map ent with the provided id, fields from the given
    /// collection, and edges from the other given collection
    pub fn from_collections<FI: IntoIterator<Item = Field>, EI: IntoIterator<Item = Edge>>(
        id: Id,
        field_collection: FI,
        edge_collection: EI,
    ) -> Self {
        Self::new(
            id,
            field_collection
                .into_iter()
                .map(|f| (f.name().to_string(), f))
                .collect(),
            edge_collection
                .into_iter()
                .map(|e| (e.name().to_string(), e))
                .collect(),
        )
    }
}

impl Default for UntypedEnt {
    /// Creates an untyped ent with the ephemeral id
    fn default() -> Self {
        Self::empty_with_id(EPHEMERAL_ID)
    }
}

impl EntType for UntypedEnt {
    /// Represents a unique type associated with the entity, used for
    /// lookups, indexing by type, and conversions
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{UntypedEnt, EntType, EntTypeData};
    ///
    /// assert!(matches!(
    ///     UntypedEnt::type_data(),
    ///     EntTypeData::Concrete { ty: "entity::ent::UntypedEnt" },
    /// ));
    /// ```
    fn type_data() -> EntTypeData {
        EntTypeData::Concrete {
            ty: concat!(module_path!(), "::UntypedEnt"),
        }
    }
}

#[cfg_attr(feature = "serde-1", typetag::serde)]
impl Ent for UntypedEnt {
    /// Represents the unique id associated with each entity instance
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt};
    ///
    /// let ent = UntypedEnt::empty_with_id(999);
    /// assert_eq!(ent.id(), 999);
    /// ```
    fn id(&self) -> Id {
        self.id
    }

    /// Updates the id of this ent, useful for databases that want to adjust
    /// the id or when you want to produce a clone of the ent in a database
    /// by resetting its id to ephemeral prior to storing it
    fn set_id(&mut self, id: Id) {
        self.id = id;
    }

    /// Represents a unique type associated with the entity, used for
    /// lookups, indexing by type, and conversions
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{UntypedEnt, Ent};
    ///
    /// let ent = UntypedEnt::default();
    /// assert_eq!(ent.r#type(), "entity::ent::UntypedEnt");
    /// ```
    fn r#type(&self) -> &str {
        Self::type_str()
    }

    /// Represents the time when the instance of the ent was created
    /// as the milliseconds since epoch (1970-01-01 00:00:00 UTC)
    fn created(&self) -> u64 {
        self.created
    }

    /// Represents the time when the instance of the ent was last updated
    /// as the milliseconds since epoch (1970-01-01 00:00:00 UTC)
    fn last_updated(&self) -> u64 {
        self.last_updated
    }

    /// Updates the local, internal timestamp of this ent instance
    fn mark_updated(&mut self) -> Result<(), EntMutationError> {
        self.last_updated = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(|e| EntMutationError::MarkUpdatedFailed { source: e })?
            .as_millis() as u64;
        Ok(())
    }

    /// Represents the definitions of fields contained within the ent instance
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Field, FieldDefinition, Value, ValueType};
    /// use std::str::FromStr;
    ///
    /// let fields = vec![
    ///     Field::new("field1", 123u8),
    ///     Field::new("field2", Value::from("some text")),
    /// ];
    /// let ent = UntypedEnt::from_collections(0, fields.iter().cloned(), vec![]);
    ///
    /// let defs = ent.field_definitions();
    /// assert_eq!(defs.len(), 2);
    /// assert!(defs.contains(&FieldDefinition::new(
    ///     "field1",
    ///     ValueType::from_str("u8").unwrap(),
    /// )));
    /// assert!(defs.contains(&FieldDefinition::new(
    ///     "field2",
    ///     ValueType::Text,
    /// )));
    /// ```
    fn field_definitions(&self) -> Vec<FieldDefinition> {
        self.fields.values().map(FieldDefinition::from).collect()
    }

    /// Represents the names of fields contained within the ent instance
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Field, Value};
    ///
    /// let fields = vec![
    ///     Field::new("field1", 123u8),
    ///     Field::new("field2", Value::from("some text")),
    /// ];
    /// let ent = UntypedEnt::from_collections(0, fields.iter().cloned(), vec![]);
    ///
    /// let names = ent.field_names();
    /// assert_eq!(names.len(), 2);
    /// assert!(names.contains(&String::from("field1")));
    /// assert!(names.contains(&String::from("field2")));
    /// ```
    fn field_names(&self) -> Vec<String> {
        self.fields.keys().cloned().collect()
    }

    /// Returns a copy of the value of the field with the specified name
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Field, Value};
    ///
    /// let fields = vec![
    ///     Field::new("field1", 123u8),
    ///     Field::new("field2", Value::from("some text")),
    /// ];
    /// let ent = UntypedEnt::from_collections(0, fields.iter().cloned(), vec![]);
    ///
    /// assert_eq!(ent.field("field1"), Some(Value::from(123u8)));
    /// assert_eq!(ent.field("unknown"), None);
    /// ```
    fn field(&self, name: &str) -> Option<Value> {
        self.fields.get(name).map(|f| f.value().clone())
    }

    /// Updates the local value of a field with the specified name, returning
    /// the old field value if updated
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Field, Value};
    ///
    /// let fields = vec![
    ///     Field::new("field1", 123u8),
    ///     Field::new("field2", Value::from("some text")),
    /// ];
    /// let mut ent = UntypedEnt::from_collections(0, fields.iter().cloned(), vec![]);
    ///
    /// ent.update_field("field1", Value::from(5u8)).unwrap();
    /// assert_eq!(ent.field("field1"), Some(Value::from(5u8)));
    /// ```
    fn update_field(&mut self, name: &str, value: Value) -> Result<Value, EntMutationError> {
        self.mark_updated()?;

        match self.fields.entry(name.to_string()) {
            Entry::Occupied(mut x) => {
                let field = Field::new(name.to_string(), value);
                Ok(x.insert(field).into_value())
            }
            Entry::Vacant(_) => Err(EntMutationError::NoField {
                name: name.to_string(),
            }),
        }
    }

    /// Represents the definitions of edges contained within the ent instance
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Edge, EdgeDefinition, EdgeValueType};
    /// use std::str::FromStr;
    ///
    /// let edges = vec![
    ///     Edge::new("edge1", 99),
    ///     Edge::new("edge2", vec![1, 2, 3]),
    /// ];
    /// let ent = UntypedEnt::from_collections(0, vec![], edges);
    ///
    /// let defs = ent.edge_definitions();
    /// assert_eq!(defs.len(), 2);
    /// assert!(defs.contains(&EdgeDefinition::new(
    ///     "edge1",
    ///     EdgeValueType::One,
    /// )));
    /// assert!(defs.contains(&EdgeDefinition::new(
    ///     "edge2",
    ///     EdgeValueType::Many,
    /// )));
    /// ```
    fn edge_definitions(&self) -> Vec<EdgeDefinition> {
        self.edges.values().map(EdgeDefinition::from).collect()
    }

    /// Returns a list of names of edges contained by the ent
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Edge};
    ///
    /// let edges = vec![
    ///     Edge::new("edge1", 99),
    ///     Edge::new("edge2", vec![1, 2, 3]),
    /// ];
    /// let ent = UntypedEnt::from_collections(0, vec![], edges);
    ///
    /// let names = ent.edge_names();
    /// assert_eq!(names.len(), 2);
    /// assert!(names.contains(&String::from("edge1")));
    /// assert!(names.contains(&String::from("edge2")));
    /// ```
    fn edge_names(&self) -> Vec<String> {
        self.edges.keys().cloned().collect()
    }

    /// Returns a copy of the value of the edge with the specified name
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Edge, EdgeValue};
    ///
    /// let edges = vec![
    ///     Edge::new("edge1", 99),
    ///     Edge::new("edge2", vec![1, 2, 3]),
    /// ];
    /// let ent = UntypedEnt::from_collections(0,  vec![], edges);
    ///
    /// assert_eq!(ent.edge("edge1"), Some(EdgeValue::One(99)));
    /// assert_eq!(ent.edge("edge2"), Some(EdgeValue::Many(vec![1, 2, 3])));
    /// assert_eq!(ent.edge("unknown"), None);
    /// ```
    fn edge(&self, name: &str) -> Option<EdgeValue> {
        self.edges.get(name).map(|e| e.value().clone())
    }

    /// Updates the local value of an edge with the specified name, returning
    /// the old edge value if updated
    ///
    /// ## Examples
    ///
    /// ```
    /// use entity::{Ent, UntypedEnt, Edge, EdgeValue};
    ///
    /// let edges = vec![
    ///     Edge::new("edge1", 99),
    ///     Edge::new("edge2", vec![1, 2, 3]),
    /// ];
    /// let mut ent = UntypedEnt::from_collections(0,  vec![], edges);
    ///
    /// ent.update_edge("edge1", EdgeValue::One(123)).unwrap();
    /// assert_eq!(ent.edge("edge1"), Some(EdgeValue::One(123)));
    /// ```
    fn update_edge(&mut self, name: &str, value: EdgeValue) -> Result<EdgeValue, EntMutationError> {
        self.mark_updated()?;

        match self.edges.entry(name.to_string()) {
            Entry::Occupied(mut x) => {
                let edge = Edge::new(name.to_string(), value);
                Ok(x.insert(edge).into_value())
            }
            Entry::Vacant(_) => Err(EntMutationError::NoEdge {
                name: name.to_string(),
            }),
        }
    }

    /// Connects ent to the given boxed database trait object so all future
    /// database-related operations will be performed against this database
    fn connect(&mut self, database: WeakDatabaseRc) {
        self.database = database;
    }

    /// Disconnects ent from the given database. All future database-related
    /// operations will fail with a disconnected database error
    fn disconnect(&mut self) {
        self.database = WeakDatabaseRc::new();
    }

    /// Returns true if ent is currently connected to a database
    fn is_connected(&self) -> bool {
        WeakDatabaseRc::strong_count(&self.database) > 0
    }

    /// Loads the ents connected by the edge with the given name
    ///
    /// Requires ent to be connected to a database
    fn load_edge(&self, name: &str) -> DatabaseResult<Vec<Box<dyn Ent>>> {
        let database =
            WeakDatabaseRc::upgrade(&self.database).ok_or(DatabaseError::Disconnected)?;
        match self.edge(name) {
            Some(e) => e
                .to_ids()
                .into_iter()
                .filter_map(|id| database.get(id).transpose())
                .collect(),
            None => Err(DatabaseError::MissingEdge {
                name: name.to_string(),
            }),
        }
    }

    /// Clears the locally-cached, computed fields of the ent
    fn clear_cache(&mut self) {
        for fd in self.field_definitions() {
            if fd.is_computed() {
                if let Some(value) = self.fields.get_mut(&fd.name) {
                    *value = Field::new_with_attributes(
                        fd.name.to_string(),
                        Value::Optional(None),
                        fd.attributes().to_vec(),
                    );
                }
            }
        }
    }

    /// Refreshes ent by checking database for latest version and returning it
    ///
    /// Requires ent to be connected to a database
    fn refresh(&mut self) -> DatabaseResult<()> {
        let database =
            WeakDatabaseRc::upgrade(&self.database).ok_or(DatabaseError::Disconnected)?;
        let id = self.id;
        match database.get(id)? {
            Some(x) => {
                self.id = x.id();
                self.fields = x
                    .fields()
                    .into_iter()
                    .map(|f| (f.name().to_string(), f.clone()))
                    .collect();
                self.edges = x
                    .edges()
                    .into_iter()
                    .map(|e| (e.name().to_string(), e.clone()))
                    .collect();
                self.created = x.created();
                self.last_updated = x.last_updated();

                Ok(())
            }
            None => Err(DatabaseError::MissingEnt { id }),
        }
    }

    /// Saves the ent to the database, updating this local instance's id
    /// if the database has reported a new id
    ///
    /// Requires ent to be connected to a database
    fn commit(&mut self) -> DatabaseResult<()> {
        let database =
            WeakDatabaseRc::upgrade(&self.database).ok_or(DatabaseError::Disconnected)?;
        match database.insert(Box::new(Self::clone(&self))) {
            Ok(id) => {
                self.set_id(id);
                Ok(())
            }
            Err(x) => Err(x),
        }
    }

    /// Removes self from database, returning true if successful
    ///
    /// Requires ent to be connected to a database
    fn remove(&self) -> DatabaseResult<bool> {
        let database =
            WeakDatabaseRc::upgrade(&self.database).ok_or(DatabaseError::Disconnected)?;
        database.remove(self.id)
    }
}