1
1
use crate :: routes:: audio_query:: OPEN_JTALK ;
2
2
use crate :: voicevox:: user_dict:: { UserDict , UserDictWord , UserDictWordType } ;
3
3
4
+ use axum:: extract:: { Path , Query } ;
4
5
use axum:: response:: Json ;
5
6
use once_cell:: sync:: Lazy ;
6
7
use serde:: { Deserialize , Serialize } ;
@@ -36,9 +37,18 @@ pub struct VvUserDictWord {
36
37
mora_count : usize ,
37
38
surface : String ,
38
39
pronunciation : String ,
40
+ #[ serde( skip_deserializing) ]
39
41
part_of_speech_detail_1 : String ,
40
42
}
41
43
44
+ #[ derive( Debug , Serialize , Deserialize ) ]
45
+ pub struct VvUserDictWordParam {
46
+ priority : u32 ,
47
+ accent_type : usize ,
48
+ surface : String ,
49
+ pronunciation : String ,
50
+ }
51
+
42
52
impl From < VvUserDictWord > for UserDictWord {
43
53
fn from ( word : VvUserDictWord ) -> UserDictWord {
44
54
UserDictWord :: new (
@@ -51,6 +61,7 @@ impl From<VvUserDictWord> for UserDictWord {
51
61
"動詞" => UserDictWordType :: Verb ,
52
62
"形容詞" => UserDictWordType :: Adjective ,
53
63
"語尾" => UserDictWordType :: Suffix ,
64
+ "" => UserDictWordType :: ProperNoun ,
54
65
_ => {
55
66
warn ! ( "Unknown word type: {}" , & word. part_of_speech_detail_1) ;
56
67
UserDictWordType :: CommonNoun
@@ -82,6 +93,19 @@ impl From<UserDictWord> for VvUserDictWord {
82
93
}
83
94
}
84
95
96
+ impl From < VvUserDictWordParam > for UserDictWord {
97
+ fn from ( word : VvUserDictWordParam ) -> UserDictWord {
98
+ UserDictWord :: new (
99
+ & word. surface [ ..] ,
100
+ word. pronunciation ,
101
+ word. accent_type ,
102
+ UserDictWordType :: CommonNoun ,
103
+ word. priority ,
104
+ )
105
+ . unwrap ( )
106
+ }
107
+ }
108
+
85
109
pub async fn get_user_dict ( ) -> Json < HashMap < String , VvUserDictWord > > {
86
110
let user_dict = USER_DICT . lock ( ) . await ;
87
111
@@ -110,9 +134,84 @@ pub async fn import_user_dict(Json(payload): Json<HashMap<String, VvUserDictWord
110
134
. load ( temp_file. to_str ( ) . unwrap ( ) )
111
135
. map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
112
136
113
- user_dict. save ( & USER_DICT_PATH ) . unwrap ( ) ;
137
+ user_dict
138
+ . save ( & USER_DICT_PATH )
139
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
114
140
115
141
OPEN_JTALK . lock ( ) . await . use_user_dict ( & user_dict) . unwrap ( ) ;
116
142
117
143
Ok ( ( ) )
118
144
}
145
+
146
+ pub async fn post_user_dict_word ( Query ( param) : Query < VvUserDictWordParam > ) -> Result < String > {
147
+ let mut user_dict = USER_DICT . lock ( ) . await ;
148
+
149
+ let word: UserDictWord = param. into ( ) ;
150
+
151
+ let word_uuid = user_dict
152
+ . add_word ( word)
153
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
154
+
155
+ user_dict
156
+ . save ( & USER_DICT_PATH )
157
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
158
+
159
+ OPEN_JTALK
160
+ . lock ( )
161
+ . await
162
+ . use_user_dict ( & user_dict)
163
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
164
+
165
+ Ok ( word_uuid. hyphenated ( ) . to_string ( ) )
166
+ }
167
+
168
+ pub async fn delete_user_dict_word ( Path ( word_uuid) : Path < String > ) -> Result < ( ) > {
169
+ let mut user_dict = USER_DICT . lock ( ) . await ;
170
+
171
+ let word_uuid = uuid:: Uuid :: parse_str ( & word_uuid)
172
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
173
+
174
+ user_dict
175
+ . remove_word ( word_uuid)
176
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
177
+
178
+ user_dict
179
+ . save ( & USER_DICT_PATH )
180
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
181
+
182
+ OPEN_JTALK
183
+ . lock ( )
184
+ . await
185
+ . use_user_dict ( & user_dict)
186
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
187
+
188
+ Ok ( ( ) )
189
+ }
190
+
191
+ pub async fn put_user_dict_word (
192
+ Path ( word_uuid) : Path < String > ,
193
+ Query ( payload) : Query < VvUserDictWordParam > ,
194
+ ) -> Result < ( ) > {
195
+ let mut user_dict = USER_DICT . lock ( ) . await ;
196
+
197
+ let word_uuid = uuid:: Uuid :: parse_str ( & word_uuid)
198
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
199
+
200
+ let word: UserDictWord = payload. into ( ) ;
201
+
202
+ user_dict
203
+ . update_word ( word_uuid, word)
204
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
205
+
206
+ user_dict
207
+ . save ( & USER_DICT_PATH )
208
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
209
+
210
+ OPEN_JTALK
211
+ . lock ( )
212
+ . await
213
+ . use_user_dict ( & user_dict)
214
+ . map_err ( |e| Error :: DictionaryOperationFailed ( e. into ( ) ) ) ?;
215
+
216
+ Ok ( ( ) )
217
+ }
0 commit comments