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
use crate::host::{Host, HostError};

use core::cmp::Ordering;
use soroban_env_common::xdr::{AccountId, ScAddress};
use soroban_env_common::{
    AddressObject, BytesObject, Compare, ConversionError, Env, EnvBase, MapObject, RawVal,
    TryFromVal, VecObject,
};

#[derive(Clone)]
pub struct Bytes {
    host: Host,
    object: BytesObject,
}

impl Compare<Bytes> for Host {
    type Error = HostError;

    fn compare(&self, a: &Bytes, b: &Bytes) -> Result<Ordering, Self::Error> {
        self.compare(&a.object.to_raw(), &b.object.to_raw())
    }
}

impl TryFromVal<Host, BytesObject> for Bytes {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &BytesObject) -> Result<Self, Self::Error> {
        Ok(Bytes {
            host: env.clone(),
            object: *val,
        })
    }
}

impl TryFromVal<Host, RawVal> for Bytes {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &RawVal) -> Result<Self, Self::Error> {
        let val = *val;
        let obj: BytesObject = val.try_into()?;
        Bytes::try_from_val(env, &obj)
    }
}

impl TryFromVal<Host, Bytes> for RawVal {
    type Error = HostError;

    fn try_from_val(_env: &Host, val: &Bytes) -> Result<RawVal, Self::Error> {
        Ok(val.object.into())
    }
}

impl From<Bytes> for BytesObject {
    fn from(b: Bytes) -> Self {
        b.object
    }
}

impl<const N: usize> From<BytesN<N>> for Bytes {
    fn from(b: BytesN<N>) -> Self {
        Self {
            host: b.host.clone(),
            object: b.object,
        }
    }
}

impl Bytes {
    pub fn push(&mut self, x: u8) -> Result<(), HostError> {
        let x32: u32 = x.into();
        self.object = self.host.bytes_push(self.object, x32.into())?;
        Ok(())
    }

    pub fn append(&mut self, other: Bytes) -> Result<(), HostError> {
        self.object = self.host.bytes_append(self.object, other.object)?;
        Ok(())
    }

    #[cfg(test)]
    pub(crate) fn to_vec(&self) -> std::vec::Vec<u8> {
        let mut res = std::vec::Vec::<u8>::new();
        let size: u32 = self.host.bytes_len(self.object).unwrap().into();
        res.resize(size as usize, 0);
        self.host
            .bytes_copy_to_slice(self.object, RawVal::U32_ZERO, &mut res[..])
            .unwrap();
        res
    }
}

#[derive(Clone)]
pub struct BytesN<const N: usize> {
    host: Host,
    object: BytesObject,
}

impl<const N: usize> TryFromVal<Host, BytesObject> for BytesN<N> {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &BytesObject) -> Result<Self, Self::Error> {
        let val = *val;
        let len: u32 = env.bytes_len(val)?.try_into()?;
        if len
            == N.try_into()
                .map_err(|_| env.err_general("bytes buffer overflow"))?
        {
            Ok(Self {
                host: env.clone(),
                object: val,
            })
        } else {
            Err(ConversionError.into())
        }
    }
}

impl<const N: usize> Compare<BytesN<N>> for Host {
    type Error = HostError;

    fn compare(&self, a: &BytesN<N>, b: &BytesN<N>) -> Result<Ordering, Self::Error> {
        self.compare(&a.object.to_raw(), &b.object.to_raw())
    }
}

impl<const N: usize> TryFromVal<Host, RawVal> for BytesN<N> {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &RawVal) -> Result<Self, Self::Error> {
        let val = *val;
        let obj: BytesObject = val.try_into()?;
        <BytesN<N>>::try_from_val(env, &obj)
    }
}

impl<const N: usize> TryFromVal<Host, BytesN<N>> for RawVal {
    type Error = HostError;

    fn try_from_val(_env: &Host, val: &BytesN<N>) -> Result<RawVal, Self::Error> {
        Ok(val.object.into())
    }
}

impl<const N: usize> From<BytesN<N>> for RawVal {
    fn from(val: BytesN<N>) -> Self {
        val.object.into()
    }
}

impl<const N: usize> From<BytesN<N>> for BytesObject {
    fn from(bytes: BytesN<N>) -> Self {
        bytes.object
    }
}

impl<const N: usize> BytesN<N> {
    pub fn compare(&self, other: &BytesN<N>) -> Result<Ordering, HostError> {
        let res = self.host.obj_cmp(self.object.into(), other.object.into())?;
        Ok(res.cmp(&0))
    }
}

impl<const N: usize> BytesN<N> {
    #[inline(always)]
    pub fn to_array(&self) -> Result<[u8; N], HostError> {
        let mut slice = [0_u8; N];
        self.host
            .bytes_copy_to_slice(self.object, RawVal::U32_ZERO, &mut slice)?;
        Ok(slice)
    }

    #[inline(always)]
    pub fn from_slice(env: &Host, items: &[u8]) -> Result<BytesN<N>, HostError> {
        Ok(BytesN {
            host: env.clone(),
            object: env.bytes_new_from_slice(items)?,
        })
    }

    #[cfg(test)]
    pub(crate) fn to_vec(&self) -> std::vec::Vec<u8> {
        Bytes::from(self.clone()).to_vec()
    }
}

#[derive(Clone)]
pub struct Map {
    host: Host,
    object: MapObject,
}

impl TryFromVal<Host, MapObject> for Map {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &MapObject) -> Result<Self, Self::Error> {
        let val = *val;
        Ok(Map {
            host: env.clone(),
            object: val,
        })
    }
}

impl Compare<Map> for Host {
    type Error = HostError;

    fn compare(&self, a: &Map, b: &Map) -> Result<Ordering, Self::Error> {
        self.compare(&a.object.to_raw(), &b.object.to_raw())
    }
}

impl TryFromVal<Host, RawVal> for Map {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &RawVal) -> Result<Self, Self::Error> {
        let val = *val;
        let obj: MapObject = val.try_into()?;
        Map::try_from_val(env, &obj)
    }
}

impl TryFromVal<Host, Map> for RawVal {
    type Error = HostError;

    fn try_from_val(_env: &Host, val: &Map) -> Result<RawVal, Self::Error> {
        Ok(val.object.into())
    }
}

impl From<Map> for MapObject {
    fn from(map: Map) -> Self {
        map.object
    }
}

impl Map {
    pub fn new(env: &Host) -> Result<Self, HostError> {
        let map = env.map_new()?;
        Ok(Self {
            host: env.clone(),
            object: map,
        })
    }

    pub fn get<K, V>(&self, k: &K) -> Result<V, HostError>
    where
        RawVal: TryFromVal<Host, K>,
        V: TryFromVal<Host, RawVal>,
        HostError: From<<RawVal as TryFromVal<Host, K>>::Error>,
        HostError: From<<V as TryFromVal<Host, RawVal>>::Error>,
    {
        let k_rv = RawVal::try_from_val(&self.host, k)?;
        let v_rv = self.host.map_get(self.object, k_rv)?;
        Ok(V::try_from_val(&self.host, &v_rv)?)
    }

    pub fn set<K, V>(&mut self, k: &K, v: &V) -> Result<(), HostError>
    where
        RawVal: TryFromVal<Host, K>,
        RawVal: TryFromVal<Host, V>,
        HostError: From<<RawVal as TryFromVal<Host, K>>::Error>,
        HostError: From<<RawVal as TryFromVal<Host, V>>::Error>,
    {
        let k_rv = RawVal::try_from_val(&self.host, k)?;
        let v_rv = RawVal::try_from_val(&self.host, v)?;
        self.object = self.host.map_put(self.object, k_rv, v_rv)?;
        Ok(())
    }
}

#[derive(Clone)]
pub struct Vec {
    host: Host,
    object: VecObject,
}

impl Compare<Vec> for Host {
    type Error = HostError;

    fn compare(&self, a: &Vec, b: &Vec) -> Result<Ordering, Self::Error> {
        self.compare(&a.object.as_raw(), &b.object.as_raw())
    }
}

impl TryFromVal<Host, VecObject> for Vec {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &VecObject) -> Result<Self, Self::Error> {
        let val = *val;
        Ok(Vec {
            host: env.clone(),
            object: val,
        })
    }
}

impl TryFromVal<Host, RawVal> for Vec {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &RawVal) -> Result<Self, Self::Error> {
        let val = *val;
        let obj: VecObject = val.try_into()?;
        Vec::try_from_val(env, &obj)
    }
}

impl TryFromVal<Host, Vec> for RawVal {
    type Error = HostError;

    fn try_from_val(_env: &Host, val: &Vec) -> Result<RawVal, Self::Error> {
        Ok(val.object.into())
    }
}

impl From<Vec> for RawVal {
    fn from(val: Vec) -> Self {
        val.object.into()
    }
}

impl From<Vec> for VecObject {
    fn from(vec: Vec) -> Self {
        vec.object
    }
}

impl TryFromVal<Host, std::vec::Vec<RawVal>> for Vec {
    type Error = HostError;

    fn try_from_val(env: &Host, vals: &std::vec::Vec<RawVal>) -> Result<Self, Self::Error> {
        let mut v = Vec::new(env)?;
        for rv in vals {
            v.push_raw(*rv)?
        }
        Ok(v)
    }
}

impl TryFromVal<Host, &std::vec::Vec<RawVal>> for Vec {
    type Error = HostError;

    fn try_from_val(env: &Host, vals: &&std::vec::Vec<RawVal>) -> Result<Self, Self::Error> {
        Vec::try_from_val(env, *vals)
    }
}

impl Vec {
    pub fn new(env: &Host) -> Result<Self, HostError> {
        let vec = env.vec_new(().into())?;
        Ok(Self {
            host: env.clone(),
            object: vec,
        })
    }

    pub fn from_slice(env: &Host, slice: &[RawVal]) -> Result<Self, HostError> {
        let vec = env.vec_new_from_slice(slice)?;
        Ok(Self {
            host: env.clone(),
            object: vec,
        })
    }

    pub fn get<T: TryFromVal<Host, RawVal>>(&self, i: u32) -> Result<T, HostError>
    where
        HostError: From<<T as TryFromVal<Host, RawVal>>::Error>,
    {
        let rv = self.host.vec_get(self.object, i.into())?;
        Ok(T::try_from_val(&self.host, &rv)?)
    }

    pub fn len(&self) -> Result<u32, HostError> {
        Ok(self.host.vec_len(self.object)?.into())
    }

    pub fn push<T>(&mut self, x: &T) -> Result<(), HostError>
    where
        RawVal: TryFromVal<Host, T>,
        HostError: From<<RawVal as TryFromVal<Host, T>>::Error>,
    {
        let rv = RawVal::try_from_val(&self.host, x)?;
        self.push_raw(rv)
    }

    pub fn push_raw(&mut self, x: RawVal) -> Result<(), HostError> {
        self.object = self.host.vec_push_back(self.object, x)?;
        Ok(())
    }
}

#[derive(Clone)]
pub struct Address {
    host: Host,
    object: AddressObject,
}

impl TryFromVal<Host, AddressObject> for Address {
    type Error = HostError;

    fn try_from_val(env: &Host, obj: &AddressObject) -> Result<Self, Self::Error> {
        Ok(Address {
            host: env.clone(),
            object: *obj,
        })
    }
}

impl TryFromVal<Host, RawVal> for Address {
    type Error = HostError;

    fn try_from_val(env: &Host, val: &RawVal) -> Result<Self, Self::Error> {
        let val = *val;
        let obj: AddressObject = val.try_into()?;
        Address::try_from_val(env, &obj)
    }
}

impl TryFromVal<Host, Address> for RawVal {
    type Error = HostError;

    fn try_from_val(_env: &Host, val: &Address) -> Result<RawVal, Self::Error> {
        Ok(val.object.into())
    }
}

impl Compare<Address> for Host {
    type Error = HostError;

    fn compare(&self, a: &Address, b: &Address) -> Result<Ordering, Self::Error> {
        self.compare(&a.object.to_raw(), &b.object.to_raw())
    }
}

impl From<Address> for AddressObject {
    fn from(a: Address) -> Self {
        a.object
    }
}

impl Address {
    pub(crate) fn from_account(env: &Host, account_id: &AccountId) -> Result<Self, HostError> {
        Address::try_from_val(
            env,
            &env.add_host_object(ScAddress::Account(account_id.clone()))?,
        )
    }

    pub(crate) fn to_sc_address(&self) -> Result<ScAddress, HostError> {
        self.host
            .visit_obj(self.object, |addr: &ScAddress| Ok(addr.clone()))
    }

    pub(crate) fn require_auth(&self) -> Result<(), HostError> {
        Ok(self.host.require_auth(self.object)?.try_into()?)
    }
}