soroban_env_common/
string.rs

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
use crate::{declare_tag_based_object_wrapper, Env, TryFromVal, Val};

#[cfg(feature = "std")]
use crate::{
    xdr::{ScErrorCode, ScErrorType},
    TryIntoVal,
};

declare_tag_based_object_wrapper!(StringObject);

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, StringObject> for String {
    type Error = crate::Error;

    fn try_from_val(env: &E, v: &StringObject) -> Result<Self, Self::Error> {
        let len: u32 = env.string_len(*v).map_err(Into::into)?.into();
        let len = len as usize;
        let mut vec = std::vec![0; len];
        env.string_copy_to_slice(*v, Val::U32_ZERO, &mut vec)
            .map_err(Into::into)?;
        String::from_utf8(vec).map_err(|_| {
            crate::Error::from_type_and_code(ScErrorType::Value, ScErrorCode::InvalidInput)
        })
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, Val> for String {
    type Error = crate::Error;

    #[inline(always)]
    fn try_from_val(env: &E, val: &Val) -> Result<Self, Self::Error> {
        let so: StringObject = val.try_into()?;
        so.try_into_val(env)
    }
}

impl<E: Env> TryFromVal<E, &str> for StringObject {
    type Error = crate::Error;
    #[inline(always)]
    fn try_from_val(env: &E, val: &&str) -> Result<StringObject, Self::Error> {
        env.string_new_from_slice(val.as_bytes())
            .map_err(Into::into)
    }
}

impl<E: Env> TryFromVal<E, &str> for Val {
    type Error = crate::Error;
    #[inline(always)]
    fn try_from_val(env: &E, val: &&str) -> Result<Val, Self::Error> {
        Ok(StringObject::try_from_val(env, val)?.into())
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, String> for StringObject {
    type Error = crate::Error;

    fn try_from_val(env: &E, v: &String) -> Result<Self, Self::Error> {
        v.as_str().try_into_val(env)
    }
}

#[cfg(feature = "std")]
impl<E: Env> TryFromVal<E, String> for Val {
    type Error = crate::Error;

    fn try_from_val(env: &E, v: &String) -> Result<Self, Self::Error> {
        v.as_str().try_into_val(env)
    }
}