-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaddin2.rs
129 lines (113 loc) · 3.33 KB
/
addin2.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
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
use std::error::Error;
use addin1c::{name, AddinResult, MethodInfo, Methods, PropInfo, SimpleAddin, Variant};
use chrono::Utc;
pub struct Addin2 {
prop1: i32,
last_error: Option<Box<dyn Error>>,
}
impl Addin2 {
pub fn new() -> Addin2 {
Addin2 {
prop1: 0,
last_error: None,
}
}
fn last_error(&mut self, value: &mut Variant) -> AddinResult {
match &self.last_error {
Some(err) => value
.set_str1c(err.to_string().as_str())
.map_err(|e| e.into()),
None => value.set_str1c("").map_err(|e| e.into()),
}
}
fn method1(&mut self, param: &mut Variant, ret_value: &mut Variant) -> AddinResult {
let value = param.get_i32()?;
self.prop1 = value;
ret_value.set_i32(value * 2);
Ok(())
}
fn method2(
&mut self,
param1: &mut Variant,
param2: &mut Variant,
ret_value: &mut Variant,
) -> AddinResult {
let value1 = param1.get_i32()?;
let value2 = param2.get_i32()?;
self.prop1 = value1 + value2;
ret_value.set_i32(self.prop1);
Ok(())
}
fn set_prop1(&mut self, value: &Variant) -> AddinResult {
let value = value.get_i32()?;
self.prop1 = value;
Ok(())
}
fn get_prop1(&mut self, value: &mut Variant) -> AddinResult {
value.set_i32(self.prop1);
Ok(())
}
fn panic1(&mut self, _ret_value: &mut Variant) -> AddinResult {
panic!("Panic1")
}
fn panic2(&mut self, _ret_value: &mut Variant) -> AddinResult {
panic!("Panic{}", self.prop1)
}
fn method_no_params(&mut self, _ret_value: &mut Variant) -> AddinResult {
Ok(())
}
fn utc(&mut self, ret_value: &mut Variant) -> AddinResult {
ret_value.set_date(Utc::now().into());
Ok(())
}
}
impl SimpleAddin for Addin2 {
fn name() -> &'static [u16] {
name!("Class2")
}
fn save_error(&mut self, err: Option<Box<dyn Error>>) {
self.last_error = err;
}
fn methods() -> &'static [MethodInfo<Self>] {
&[
MethodInfo {
name: name!("Method1"),
method: Methods::Method1(Self::method1),
},
MethodInfo {
name: name!("Method2"),
method: Methods::Method2(Self::method2),
},
MethodInfo {
name: name!("Panic1"),
method: Methods::Method0(Self::panic1),
},
MethodInfo {
name: name!("Panic2"),
method: Methods::Method0(Self::panic2),
},
MethodInfo {
name: name!("MethodNoParams"),
method: Methods::Method0(Self::method_no_params),
},
MethodInfo {
name: name!("Utc"),
method: Methods::Method0(Self::utc),
},
]
}
fn properties() -> &'static [PropInfo<Self>] {
&[
PropInfo {
name: name!("Prop1"),
getter: Some(Self::get_prop1),
setter: Some(Self::set_prop1),
},
PropInfo {
name: name!("LastError"),
getter: Some(Self::last_error),
setter: None,
},
]
}
}