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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2019 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use libimagstore::storeid::StoreId;
use libimagstore::storeid::IntoStoreId;
use libimagstore::store::Store;
use libimagerror::errors::ErrorMsg as EM;

use toml::Value;
use failure::ResultExt;
use failure::Fallible as Result;
use failure::Error;

#[derive(Eq, PartialOrd, Ord, Debug, Clone)]
pub enum Link {
    Id          { link: StoreId },
    LinkTo   { link: StoreId },
    LinkFrom { link: StoreId },
}

impl Link {

    pub fn exists(&self, store: &Store) -> Result<bool> {
        match *self {
            Link::Id { ref link }             => store.exists(link.clone()),
            Link::LinkTo   { ref link }       => store.exists(link.clone()),
            Link::LinkFrom { ref link }       => store.exists(link.clone()),
        }
        .map_err(From::from)
    }

    pub fn to_str(&self) -> Result<String> {
        match *self {
            Link::Id { ref link }             => link.to_str(),
            Link::LinkTo   { ref link }       => link.to_str(),
            Link::LinkFrom { ref link }       => link.to_str(),
        }
        .map_err(From::from)
    }

    #[cfg(test)]
    pub(crate) fn eq_store_id(&self, id: &StoreId) -> bool {
        match self {
            Link::Id { link: ref s }             => s.eq(id),
            Link::LinkTo   { ref link }          => link.eq(id),
            Link::LinkFrom { ref link }          => link.eq(id),
        }
    }

    /// Get the StoreId inside the Link, which is always present
    pub fn get_store_id(&self) -> &StoreId {
        match self {
            Link::Id { link: ref s }             => s,
            Link::LinkTo   { ref link }          => link,
            Link::LinkFrom { ref link }          => link,
        }
    }

    pub(crate) fn to_value(&self) -> Result<Value> {
        debug!("Converting {:?} to Value", self);
        match self {
            Link::Id       { ref link } => link,
            Link::LinkTo   { ref link } => link,
            Link::LinkFrom { ref link } => link,
        }
        .to_str()
        .map(Value::String)
        .context(EM::ConversionError)
        .map_err(Error::from)
    }

}

impl ::std::cmp::PartialEq for Link {
    fn eq(&self, other: &Self) -> bool {
        debug!("Checking for equality: {:?} == {:?}", self, other);
        match (self, other) {
            (&Link::Id { link: ref a }, &Link::Id { link: ref b }) => a.eq(&b),
            (&Link::LinkTo   { link: ref a }, &Link::LinkTo   { link: ref b })=> a.eq(&b),
            (&Link::LinkFrom { link: ref a }, &Link::LinkFrom { link: ref b })=> a.eq(&b),
            _ => false,
        }
    }
}

impl std::hash::Hash for Link {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        std::mem::discriminant(self).hash(state);
        match &self {
            Link::Id { link: a } => a.hash(state),
            Link::LinkTo { link: a } => a.hash(state),
            Link::LinkFrom { link: a } => a.hash(state),
        }
    }
}

impl From<StoreId> for Link {

    fn from(s: StoreId) -> Link {
        Link::Id { link: s }
    }
}

impl Into<StoreId> for Link {
    fn into(self) -> StoreId {
        match self {
            Link::Id { link }            => link,
            Link::LinkTo   { link }      => link,
            Link::LinkFrom { link }      => link,
        }
    }
}

impl IntoStoreId for Link {
    fn into_storeid(self) -> Result<StoreId> {
        match self {
            Link::Id { link }            => Ok(link),
            Link::LinkTo   { link }      => Ok(link),
            Link::LinkFrom { link }      => Ok(link),
        }
    }
}

impl AsRef<StoreId> for Link {
    fn as_ref(&self) -> &StoreId {
        match self {
            Link::Id { ref link }            => &link,
            Link::LinkTo   { ref link }      => &link,
            Link::LinkFrom { ref link }      => &link,
        }
    }
}