1
+ use std:: sync:: { Arc , Mutex } ;
2
+
1
3
use rust_xlsxwriter as xlsx;
2
4
use wasm_bindgen:: prelude:: * ;
3
5
@@ -20,16 +22,32 @@ use wasm_bindgen::prelude::*;
20
22
#[ derive( Clone ) ]
21
23
#[ wasm_bindgen]
22
24
pub struct DocProperties {
23
- pub ( crate ) inner : xlsx:: DocProperties ,
25
+ pub ( crate ) inner : Arc < Mutex < xlsx:: DocProperties > > ,
26
+ }
27
+
28
+ macro_rules! impl_method {
29
+ ( $self: ident. $method: ident( $( $arg: expr) ,* ) ) => {
30
+ let mut lock = $self. inner. lock( ) . unwrap( ) ;
31
+ let mut inner = std:: mem:: take( & mut * lock) ;
32
+ inner = inner. $method( $( $arg) ,* ) ;
33
+ let _ = std:: mem:: replace( & mut * lock, inner) ;
34
+ return DocProperties {
35
+ inner: Arc :: clone( & $self. inner) ,
36
+ }
37
+ } ;
24
38
}
25
39
26
40
#[ wasm_bindgen]
27
41
impl DocProperties {
42
+ pub ( crate ) fn lock ( & self ) -> std:: sync:: MutexGuard < ' _ , xlsx:: DocProperties > {
43
+ self . inner . lock ( ) . unwrap ( )
44
+ }
45
+
28
46
/// Create a new `DocProperties` class.
29
47
#[ wasm_bindgen( constructor) ]
30
48
pub fn new ( ) -> DocProperties {
31
49
DocProperties {
32
- inner : xlsx:: DocProperties :: new ( ) ,
50
+ inner : Arc :: new ( Mutex :: new ( xlsx:: DocProperties :: new ( ) ) ) ,
33
51
}
34
52
}
35
53
@@ -42,9 +60,7 @@ impl DocProperties {
42
60
/// @returns {DocProperties} - The DocProperties object.
43
61
#[ wasm_bindgen( js_name = "setTitle" , skip_jsdoc) ]
44
62
pub fn set_title ( & self , title : & str ) -> DocProperties {
45
- DocProperties {
46
- inner : self . clone ( ) . inner . set_title ( title) ,
47
- }
63
+ impl_method ! ( self . set_title( title) ) ;
48
64
}
49
65
50
66
/// Set the Subject field of the document properties.
@@ -56,9 +72,7 @@ impl DocProperties {
56
72
/// @returns {DocProperties} - The DocProperties object.
57
73
#[ wasm_bindgen( js_name = "setSubject" , skip_jsdoc) ]
58
74
pub fn set_subject ( & self , subject : & str ) -> DocProperties {
59
- DocProperties {
60
- inner : self . clone ( ) . inner . set_subject ( subject) ,
61
- }
75
+ impl_method ! ( self . set_subject( subject) ) ;
62
76
}
63
77
64
78
/// Set the Author field of the document properties.
@@ -70,9 +84,7 @@ impl DocProperties {
70
84
/// @returns {DocProperties} - The DocProperties object.
71
85
#[ wasm_bindgen( js_name = "setAuthor" , skip_jsdoc) ]
72
86
pub fn set_author ( & self , author : & str ) -> DocProperties {
73
- DocProperties {
74
- inner : self . clone ( ) . inner . set_author ( author) ,
75
- }
87
+ impl_method ! ( self . set_author( author) ) ;
76
88
}
77
89
78
90
/// Set the Manager field of the document properties.
@@ -84,9 +96,7 @@ impl DocProperties {
84
96
/// @returns {DocProperties} - The DocProperties object.
85
97
#[ wasm_bindgen( js_name = "setManager" , skip_jsdoc) ]
86
98
pub fn set_manager ( & self , manager : & str ) -> DocProperties {
87
- DocProperties {
88
- inner : self . clone ( ) . inner . set_manager ( manager) ,
89
- }
99
+ impl_method ! ( self . set_manager( manager) ) ;
90
100
}
91
101
92
102
/// Set the Company field of the document properties.
@@ -98,9 +108,7 @@ impl DocProperties {
98
108
/// @returns {DocProperties} - The DocProperties object.
99
109
#[ wasm_bindgen( js_name = "setCompany" , skip_jsdoc) ]
100
110
pub fn set_company ( & self , company : & str ) -> DocProperties {
101
- DocProperties {
102
- inner : self . clone ( ) . inner . set_company ( company) ,
103
- }
111
+ impl_method ! ( self . set_company( company) ) ;
104
112
}
105
113
106
114
/// Set the Category field of the document properties.
@@ -112,9 +120,7 @@ impl DocProperties {
112
120
/// @returns {DocProperties} - The DocProperties object.
113
121
#[ wasm_bindgen( js_name = "setCategory" , skip_jsdoc) ]
114
122
pub fn set_category ( & self , category : & str ) -> DocProperties {
115
- DocProperties {
116
- inner : self . clone ( ) . inner . set_category ( category) ,
117
- }
123
+ impl_method ! ( self . set_category( category) ) ;
118
124
}
119
125
120
126
/// Set the Keywords field of the document properties.
@@ -126,9 +132,7 @@ impl DocProperties {
126
132
/// @returns {DocProperties} - The DocProperties object.
127
133
#[ wasm_bindgen( js_name = "setKeywords" , skip_jsdoc) ]
128
134
pub fn set_keywords ( & self , keywords : & str ) -> DocProperties {
129
- DocProperties {
130
- inner : self . clone ( ) . inner . set_keywords ( keywords) ,
131
- }
135
+ impl_method ! ( self . set_keywords( keywords) ) ;
132
136
}
133
137
134
138
/// Set the Comment field of the document properties.
@@ -141,9 +145,7 @@ impl DocProperties {
141
145
/// @returns {DocProperties} - The DocProperties object.
142
146
#[ wasm_bindgen( js_name = "setComment" , skip_jsdoc) ]
143
147
pub fn set_comment ( & self , comment : & str ) -> DocProperties {
144
- DocProperties {
145
- inner : self . clone ( ) . inner . set_comment ( comment) ,
146
- }
148
+ impl_method ! ( self . set_comment( comment) ) ;
147
149
}
148
150
149
151
/// Set the Status field of the document properties.
@@ -155,9 +157,7 @@ impl DocProperties {
155
157
/// @returns {DocProperties} - The DocProperties object.
156
158
#[ wasm_bindgen( js_name = "setStatus" , skip_jsdoc) ]
157
159
pub fn set_status ( & self , status : & str ) -> DocProperties {
158
- DocProperties {
159
- inner : self . clone ( ) . inner . set_status ( status) ,
160
- }
160
+ impl_method ! ( self . set_status( status) ) ;
161
161
}
162
162
163
163
/// Set the hyperlink base field of the document properties.
@@ -169,8 +169,6 @@ impl DocProperties {
169
169
/// @returns {DocProperties} - The DocProperties object.
170
170
#[ wasm_bindgen( js_name = "setHyperlinkBase" , skip_jsdoc) ]
171
171
pub fn set_hyperlink_base ( & self , hyperlink_base : & str ) -> DocProperties {
172
- DocProperties {
173
- inner : self . clone ( ) . inner . set_hyperlink_base ( hyperlink_base) ,
174
- }
172
+ impl_method ! ( self . set_hyperlink_base( hyperlink_base) ) ;
175
173
}
176
174
}
0 commit comments