summaryrefslogtreecommitdiffstats
path: root/libimagruby/src/storeid.rs
blob: a82006499c595143861e90c5b4a86e6743c91087 (plain)
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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015, 2016 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 std::path::PathBuf;

use ruru::{Class, Object, AnyObject, Boolean, RString, NilClass, VerifiedObject};

use libimagstore::storeid::StoreId;
use util::Unwrap;
use util::Wrap;

wrappable_struct!(StoreId, StoreIdWrapper, STOREID_WRAPPER);
class!(RStoreId);
impl_wrap!(StoreId => STOREID_WRAPPER);
impl_unwrap!(RStoreId => StoreId => STOREID_WRAPPER);
impl_verified_object!(RStoreId);


methods!(
    RStoreId,
    itself,

    fn r_storeid_new(base: RString, id: RString) -> AnyObject {
        let base = match base.map(|b| b.to_string()).map(PathBuf::from) {
            Ok(base) => base,
            Err(e) => {
                // TODO: Exception!
                error!("Building StoreId object failed: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        };

        let id = match id.map(|id| id.to_string()).map(PathBuf::from) {
            Ok(id) => id,
            Err(e) => {
                // TODO: Exception!
                error!("Building StoreId object failed: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        };

        match StoreId::new(Some(base), id) {
            Ok(sid) => Class::from_existing("RStoreId").wrap_data(sid, &*STOREID_WRAPPER),
            Err(e) => {
                // TODO: Exception!
                error!("Building StoreId object failed: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        }
    }

    fn r_storeid_new_baseless(id: RString) -> AnyObject {
        let id = match id.map(|id| id.to_string()).map(PathBuf::from) {
            Ok(id) => id,
            Err(e) => {
                // TODO: Exception!
                error!("Building StoreId object failed: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        };

        match StoreId::new(None, id) {
            Ok(sid) => Class::from_existing("RStoreId").wrap_data(sid, &*STOREID_WRAPPER),
            Err(e) => {
                // TODO: Exception!
                error!("Building StoreId object failed: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        }
    }

    fn r_storeid_without_base() -> RStoreId {
        let withoutbase : StoreId = itself.get_data(&*STOREID_WRAPPER).clone().without_base();
        Class::from_existing("RStoreId").wrap_data(withoutbase, &*STOREID_WRAPPER)
    }

    fn r_storeid_with_base(base: RString) -> AnyObject {
        let base : PathBuf = match base.map(|b| b.to_string()).map(PathBuf::from) {
            Ok(pb) => pb,
            Err(e) => {
                // TODO: Exception!
                error!("Error: {:?}", e);
                return AnyObject::from(NilClass::new().value());
            },
        };
        let withoutbase : StoreId = itself.get_data(&*STOREID_WRAPPER).clone().with_base(base);
        Class::from_existing("RStoreId").wrap_data(withoutbase, &*STOREID_WRAPPER)
    }

    fn r_storeid_into_pathbuf() -> AnyObject {
        itself.get_data(&*STOREID_WRAPPER)
            .clone()
            .into_pathbuf()
            // TODO: No unwraps
            .map(|pb| pb.to_str().map(String::from).unwrap())
            .as_ref()
            .map(|s| AnyObject::from(RString::new(s).value()))
            // TODO: Exception!
            .unwrap_or(AnyObject::from(NilClass::new().value()))
    }

    fn r_storeid_exists() -> Boolean {
        Boolean::new(itself.get_data(&*STOREID_WRAPPER).exists())
    }

    fn r_storeid_to_str() -> AnyObject {
        itself.get_data(&*STOREID_WRAPPER)
            .to_str()
            .as_ref()
            .map(|s| AnyObject::from(RString::new(s).value()))
            // TODO: Exception!
            .unwrap_or(AnyObject::from(NilClass::new().value()))
    }

    fn r_storeid_local() -> RString {
        let local = itself.get_data(&*STOREID_WRAPPER).local();
        let local = local.to_str().unwrap(); // TODO: No unwraps
        RString::new(local)
    }

);

pub fn setup() -> Class {
    let mut class = Class::new("RStoreId", None);
    class.define(|itself| {
        itself.def_self("new"          , r_storeid_new);
        itself.def_self("new_baseless" , r_storeid_new_baseless);

        itself.def("without_base"      , r_storeid_without_base);
        itself.def("with_base"         , r_storeid_with_base);
        itself.def("into_pathbuf"      , r_storeid_into_pathbuf);
        itself.def("exists"            , r_storeid_exists);
        itself.def("to_str"            , r_storeid_to_str);
        itself.def("local"             , r_storeid_local);
    });
    class
}