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
//! This module holds the relevant constants and an enum required for Holochain to have 'status' metadata for entries.
//! Since Holochain uses an append-only data structure, but still wishes to provide classical features of a data
//! store such as "update" and "remove" (delete), metadata is created pointing entries forward to their 'latest' version,
//! even including an entry being marked as deleted.

use crate::eav::EntityAttributeValueIndex;

use eav::Attribute;
use std::{convert::TryInto, str::FromStr};

use holochain_persistence_api::{
    cas::content::{Address, AddressableContent, Content},
    error::PersistenceResult,
    hash::HashString,
};

use holochain_json_api::{
    error::{JsonError, JsonResult},
    json::JsonString,
};

/// Create a new [EAV](../eav/struct.EntityAttributeValue.html) with an entry address as the Entity, [CrudStatus](../eav/Attribute.html) as the attribute
/// and CrudStatus as the value.
/// This will come to represent the lifecycle status of an entry, when it gets stored in an [EAV Storage](../eav/trait.EntityAttributeValueStorage.html)
pub fn create_crud_status_eav(
    address: &Address,
    status: CrudStatus,
) -> PersistenceResult<EntityAttributeValueIndex> {
    EntityAttributeValueIndex::new(
        address,
        &Attribute::CrudStatus,
        &HashString::from(String::from(status)),
    )
}

/// Create a new [EAV](../eav/struct.EntityAttributeValue.html) with an old entry address as the Entity, [CrudLink](../eav/Attribute.html) as the attribute
/// and a new entry address as the value
pub fn create_crud_link_eav(
    from: &Address,
    to: &Address,
) -> PersistenceResult<EntityAttributeValueIndex> {
    EntityAttributeValueIndex::new(from, &Attribute::CrudLink, to)
}

/// the CRUD status of a Pair is stored using an EAV, NOT in the entry itself
#[derive(
    Copy, Clone, PartialEq, Debug, Serialize, Deserialize, DefaultJson, PartialOrd, Ord, Eq,
)]
#[serde(rename_all = "lowercase")]
pub enum CrudStatus {
    Live,
    Rejected,
    Deleted,
    Modified,
    /// CRDT resolution in progress
    Locked,
}

impl Default for CrudStatus {
    fn default() -> CrudStatus {
        CrudStatus::Live
    }
}

impl From<CrudStatus> for String {
    fn from(status: CrudStatus) -> String {
        let res_str = serde_json::to_string(&status).expect("failed to serialize CrudStatus enum");

        res_str.chars().filter(|kar| kar.is_alphabetic()).collect()
    }
}

impl FromStr for CrudStatus {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let quoted_string = format!("{:?}", s);

        let res = serde_json::from_str(quoted_string.as_ref())
            .map_err(|_| "failed to deserialize CrudStatus enum")?;

        Ok(res)
    }
}

impl AddressableContent for CrudStatus {
    fn content(&self) -> Content {
        self.to_owned().into()
    }

    fn try_from_content(content: &Content) -> JsonResult<Self> {
        content.to_owned().try_into()
    }
}

#[cfg(test)]
mod tests {
    use super::CrudStatus;
    use crate::eav::{eav_round_trip_test_runner, Attribute};

    use holochain_json_api::json::{JsonString, RawString};
    use holochain_persistence_api::cas::{
        content::{
            Address, AddressableContent, AddressableContentTestSuite, Content,
            ExampleAddressableContent,
        },
        storage::{test_content_addressable_storage, ExampleContentAddressableStorage},
    };

    #[test]
    fn crud_status_example_eav() {
        let entity_content = ExampleAddressableContent::try_from_content(&JsonString::from(
            RawString::from("example"),
        ))
        .unwrap();
        let attribute = Attribute::LinkTag("favourite-badge".into(), "some-tag".into());
        let value_content: Content =
            CrudStatus::try_from_content(&JsonString::from(CrudStatus::Rejected))
                .unwrap()
                .content();
        eav_round_trip_test_runner(entity_content, attribute, value_content);
    }

    #[test]
    /// show AddressableContent implementation
    fn addressable_content_test() {
        // from_content()
        AddressableContentTestSuite::addressable_content_trait_test::<CrudStatus>(
            JsonString::from(CrudStatus::Live),
            CrudStatus::Live,
            Address::from("QmXEyo1EepSNCmZjPzGATr8BF3GMYAKKSXbWJN9QS95jLx"),
        );
        AddressableContentTestSuite::addressable_content_trait_test::<CrudStatus>(
            JsonString::from(CrudStatus::Rejected),
            CrudStatus::Rejected,
            Address::from("QmcifaUPPN6BBmpjakau1DGx9nFb9YhoS7fZjPHwFLoRuw"),
        );
        AddressableContentTestSuite::addressable_content_trait_test::<CrudStatus>(
            JsonString::from(CrudStatus::Deleted),
            CrudStatus::Deleted,
            Address::from("QmVKAvoNaU3jrKEvPK9tc6ovJWozxS9CVuNfWB4sbbYwR9"),
        );
        AddressableContentTestSuite::addressable_content_trait_test::<CrudStatus>(
            JsonString::from(CrudStatus::Modified),
            CrudStatus::Modified,
            Address::from("QmbJmMc19gp8jNAoHSk5qY5H6LUF9qp9LTSKWFZojToAEz"),
        );
        AddressableContentTestSuite::addressable_content_trait_test::<CrudStatus>(
            JsonString::from(CrudStatus::Locked),
            CrudStatus::Locked,
            Address::from("QmUjxgPiP7wxpowjWD9t7FLrGgnNmNA1FUGHVY3BrEnKe3"),
        );
    }

    #[test]
    /// show CAS round trip
    fn cas_round_trip_test() {
        let crud_statuses = vec![
            CrudStatus::Live,
            CrudStatus::Rejected,
            CrudStatus::Deleted,
            CrudStatus::Modified,
            CrudStatus::Locked,
        ];
        AddressableContentTestSuite::addressable_content_round_trip::<
            CrudStatus,
            ExampleContentAddressableStorage,
        >(crud_statuses, test_content_addressable_storage());
    }
}