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
use crate::*;

pub fn get_memory(key: impl AsRef<str>) -> Result<Option<Memory>, Error> {
    let mem = Memory::from_bytes(key.as_ref().as_bytes())?;

    let offset = unsafe { extism::var_get(mem.offset()) };
    if offset == 0 {
        return Ok(None);
    }
    let length = unsafe { extism::length(offset) };

    if length == 0 {
        return Ok(None);
    }

    let memory = MemoryHandle { offset, length };
    Ok(Some(Memory(memory)))
}

/// Gets a variable in the plug-in. This variable lives as long as the
/// plug-in is loaded.
///
/// # Arguments
///
/// * `key` - A unique string key to identify the variable
///
/// # Examples
///
/// ```
/// // let's assume we have a variable at `"my_var"`
/// // which is a u32. We can default to 0 first time we fetch it:
/// let my_var = var::get("my_var")?.unwrap_or(0u32);
/// ```
pub fn get<T: FromBytesOwned>(key: impl AsRef<str>) -> Result<Option<T>, Error> {
    match get_memory(key)?.map(|x| x.to_vec()) {
        Some(v) => Ok(Some(T::from_bytes(&v)?)),
        None => Ok(None),
    }
}

/// Set a variable in the plug-in. This variable lives as long as the
/// plug-in is loaded. The value must have a [ToMemory] implementation.
///
/// # Arguments
///
/// * `key` - A unique string key to identify the variable
/// * `val` - The value to set. Must have a [ToMemory] implementation
///
/// # Examples
///
/// ```
/// var::set("my_u32_var", 42u32)?;
/// var::set("my_str_var", "Hello, World!")?;
/// ```
pub fn set(key: impl AsRef<str>, val: impl ToMemory) -> Result<(), Error> {
    let val = val.to_memory()?;
    let key = Memory::from_bytes(key.as_ref().as_bytes())?;
    unsafe { extism::var_set(key.offset(), val.offset()) }
    Ok(())
}

/// Removes a variable from the plug-in. This variable normally lives as long as the
/// plug-in is loaded.
///
/// # Arguments
///
/// * `key` - A unique string key to identify the variable
///
/// # Examples
///
/// ```
/// var::remove("my_var")?;
/// ```
pub fn remove(key: impl AsRef<str>) -> Result<(), Error> {
    let key = Memory::from_bytes(key.as_ref().as_bytes())?;
    unsafe { extism::var_set(key.offset(), 0) };
    Ok(())
}