@@ -18,104 +18,178 @@ use uuid::Uuid;
18
18
19
19
#[ allow( async_fn_in_trait) ]
20
20
pub trait SessionRepository {
21
- async fn request_session ( & self , user_id : UserID ) -> Result < User , String > ;
22
- async fn initialize_session ( & self ) -> Result < User , String > ;
23
- async fn delete_session ( & self ) -> Result < ( ) , String > ;
21
+ async fn request_session ( & self , user_id : UserID ) -> Result < User , ReadError > ;
22
+ async fn initialize_session ( & self ) -> Result < User , ReadError > ;
23
+ async fn delete_session ( & self ) -> Result < ( ) , DeleteError > ;
24
24
}
25
25
26
26
#[ allow( async_fn_in_trait) ]
27
27
pub trait VersionRepository {
28
- async fn read_version ( & self ) -> Result < String , String > ;
28
+ async fn read_version ( & self ) -> Result < String , ReadError > ;
29
29
}
30
30
31
31
#[ allow( async_fn_in_trait) ]
32
32
pub trait UserRepository {
33
- async fn read_users ( & self ) -> Result < Vec < User > , String > ;
34
- async fn create_user ( & self , name : Name , sex : Sex ) -> Result < User , String > ;
35
- async fn replace_user ( & self , user : User ) -> Result < User , String > ;
36
- async fn delete_user ( & self , id : UserID ) -> Result < UserID , String > ;
33
+ async fn read_users ( & self ) -> Result < Vec < User > , ReadError > ;
34
+ async fn create_user ( & self , name : Name , sex : Sex ) -> Result < User , CreateError > ;
35
+ async fn replace_user ( & self , user : User ) -> Result < User , UpdateError > ;
36
+ async fn delete_user ( & self , id : UserID ) -> Result < UserID , DeleteError > ;
37
37
}
38
38
39
39
#[ allow( async_fn_in_trait) ]
40
40
pub trait BodyWeightRepository {
41
- async fn sync_body_weight ( & self ) -> Result < Vec < BodyWeight > , String > ;
42
- async fn read_body_weight ( & self ) -> Result < Vec < BodyWeight > , String > ;
43
- async fn create_body_weight ( & self , body_weight : BodyWeight ) -> Result < BodyWeight , String > ;
44
- async fn replace_body_weight ( & self , body_weight : BodyWeight ) -> Result < BodyWeight , String > ;
45
- async fn delete_body_weight ( & self , date : NaiveDate ) -> Result < NaiveDate , String > ;
41
+ async fn sync_body_weight ( & self ) -> Result < Vec < BodyWeight > , SyncError > ;
42
+ async fn read_body_weight ( & self ) -> Result < Vec < BodyWeight > , ReadError > ;
43
+ async fn create_body_weight ( & self , body_weight : BodyWeight ) -> Result < BodyWeight , CreateError > ;
44
+ async fn replace_body_weight ( & self , body_weight : BodyWeight )
45
+ -> Result < BodyWeight , UpdateError > ;
46
+ async fn delete_body_weight ( & self , date : NaiveDate ) -> Result < NaiveDate , DeleteError > ;
46
47
}
47
48
48
49
#[ allow( async_fn_in_trait) ]
49
50
pub trait BodyFatRepository {
50
- async fn sync_body_fat ( & self ) -> Result < Vec < BodyFat > , String > ;
51
- async fn read_body_fat ( & self ) -> Result < Vec < BodyFat > , String > ;
52
- async fn create_body_fat ( & self , body_fat : BodyFat ) -> Result < BodyFat , String > ;
53
- async fn replace_body_fat ( & self , body_fat : BodyFat ) -> Result < BodyFat , String > ;
54
- async fn delete_body_fat ( & self , date : NaiveDate ) -> Result < NaiveDate , String > ;
51
+ async fn sync_body_fat ( & self ) -> Result < Vec < BodyFat > , SyncError > ;
52
+ async fn read_body_fat ( & self ) -> Result < Vec < BodyFat > , ReadError > ;
53
+ async fn create_body_fat ( & self , body_fat : BodyFat ) -> Result < BodyFat , CreateError > ;
54
+ async fn replace_body_fat ( & self , body_fat : BodyFat ) -> Result < BodyFat , UpdateError > ;
55
+ async fn delete_body_fat ( & self , date : NaiveDate ) -> Result < NaiveDate , DeleteError > ;
55
56
}
56
57
57
58
#[ allow( async_fn_in_trait) ]
58
59
pub trait PeriodRepository {
59
- async fn sync_period ( & self ) -> Result < Vec < Period > , String > ;
60
- async fn read_period ( & self ) -> Result < Vec < Period > , String > ;
61
- async fn create_period ( & self , period : Period ) -> Result < Period , String > ;
62
- async fn replace_period ( & self , period : Period ) -> Result < Period , String > ;
63
- async fn delete_period ( & self , date : NaiveDate ) -> Result < NaiveDate , String > ;
60
+ async fn sync_period ( & self ) -> Result < Vec < Period > , SyncError > ;
61
+ async fn read_period ( & self ) -> Result < Vec < Period > , ReadError > ;
62
+ async fn create_period ( & self , period : Period ) -> Result < Period , CreateError > ;
63
+ async fn replace_period ( & self , period : Period ) -> Result < Period , UpdateError > ;
64
+ async fn delete_period ( & self , date : NaiveDate ) -> Result < NaiveDate , DeleteError > ;
64
65
}
65
66
66
67
#[ allow( async_fn_in_trait) ]
67
68
pub trait ExerciseRepository {
68
- async fn sync_exercises ( & self ) -> Result < Vec < Exercise > , String > ;
69
- async fn read_exercises ( & self ) -> Result < Vec < Exercise > , String > ;
69
+ async fn sync_exercises ( & self ) -> Result < Vec < Exercise > , SyncError > ;
70
+ async fn read_exercises ( & self ) -> Result < Vec < Exercise > , ReadError > ;
70
71
async fn create_exercise (
71
72
& self ,
72
73
name : Name ,
73
74
muscles : Vec < ExerciseMuscle > ,
74
- ) -> Result < Exercise , String > ;
75
- async fn replace_exercise ( & self , exercise : Exercise ) -> Result < Exercise , String > ;
76
- async fn delete_exercise ( & self , id : ExerciseID ) -> Result < ExerciseID , String > ;
75
+ ) -> Result < Exercise , CreateError > ;
76
+ async fn replace_exercise ( & self , exercise : Exercise ) -> Result < Exercise , UpdateError > ;
77
+ async fn delete_exercise ( & self , id : ExerciseID ) -> Result < ExerciseID , DeleteError > ;
77
78
}
78
79
79
80
#[ allow( async_fn_in_trait) ]
80
81
pub trait RoutineRepository {
81
- async fn sync_routines ( & self ) -> Result < Vec < Routine > , String > ;
82
- async fn read_routines ( & self ) -> Result < Vec < Routine > , String > ;
82
+ async fn sync_routines ( & self ) -> Result < Vec < Routine > , SyncError > ;
83
+ async fn read_routines ( & self ) -> Result < Vec < Routine > , ReadError > ;
83
84
async fn create_routine (
84
85
& self ,
85
86
name : Name ,
86
87
sections : Vec < RoutinePart > ,
87
- ) -> Result < Routine , String > ;
88
+ ) -> Result < Routine , CreateError > ;
88
89
async fn modify_routine (
89
90
& self ,
90
91
id : RoutineID ,
91
92
name : Option < Name > ,
92
93
archived : Option < bool > ,
93
94
sections : Option < Vec < RoutinePart > > ,
94
- ) -> Result < Routine , String > ;
95
- async fn delete_routine ( & self , id : RoutineID ) -> Result < RoutineID , String > ;
95
+ ) -> Result < Routine , UpdateError > ;
96
+ async fn delete_routine ( & self , id : RoutineID ) -> Result < RoutineID , DeleteError > ;
96
97
}
97
98
98
99
#[ allow( async_fn_in_trait) ]
99
100
pub trait TrainingSessionRepository {
100
- async fn sync_training_sessions ( & self ) -> Result < Vec < TrainingSession > , String > ;
101
- async fn read_training_sessions ( & self ) -> Result < Vec < TrainingSession > , String > ;
101
+ async fn sync_training_sessions ( & self ) -> Result < Vec < TrainingSession > , SyncError > ;
102
+ async fn read_training_sessions ( & self ) -> Result < Vec < TrainingSession > , ReadError > ;
102
103
async fn create_training_session (
103
104
& self ,
104
105
routine_id : RoutineID ,
105
106
date : NaiveDate ,
106
107
notes : String ,
107
108
elements : Vec < TrainingSessionElement > ,
108
- ) -> Result < TrainingSession , String > ;
109
+ ) -> Result < TrainingSession , CreateError > ;
109
110
async fn modify_training_session (
110
111
& self ,
111
112
id : TrainingSessionID ,
112
113
notes : Option < String > ,
113
114
elements : Option < Vec < TrainingSessionElement > > ,
114
- ) -> Result < TrainingSession , String > ;
115
+ ) -> Result < TrainingSession , UpdateError > ;
115
116
async fn delete_training_session (
116
117
& self ,
117
118
id : TrainingSessionID ,
118
- ) -> Result < TrainingSessionID , String > ;
119
+ ) -> Result < TrainingSessionID , DeleteError > ;
120
+ }
121
+
122
+ #[ derive( Error , Debug ) ]
123
+ pub enum StorageError {
124
+ #[ error( "no connection" ) ]
125
+ NoConnection ,
126
+ #[ error( "no session" ) ]
127
+ NoSession ,
128
+ #[ error( transparent) ]
129
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
130
+ }
131
+
132
+ #[ derive( Error , Debug ) ]
133
+ pub enum SyncError {
134
+ #[ error( transparent) ]
135
+ Storage ( #[ from] StorageError ) ,
136
+ #[ error( transparent) ]
137
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
138
+ }
139
+
140
+ impl From < ReadError > for SyncError {
141
+ fn from ( value : ReadError ) -> Self {
142
+ match value {
143
+ ReadError :: Storage ( storage) => SyncError :: Storage ( storage) ,
144
+ ReadError :: Other ( other) => SyncError :: Other ( other) ,
145
+ }
146
+ }
147
+ }
148
+
149
+ #[ derive( Error , Debug ) ]
150
+ pub enum ReadError {
151
+ #[ error( transparent) ]
152
+ Storage ( #[ from] StorageError ) ,
153
+ #[ error( transparent) ]
154
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
155
+ }
156
+
157
+ #[ derive( Error , Debug ) ]
158
+ pub enum CreateError {
159
+ #[ error( "conflict" ) ]
160
+ Conflict ,
161
+ #[ error( transparent) ]
162
+ Storage ( #[ from] StorageError ) ,
163
+ #[ error( transparent) ]
164
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
165
+ }
166
+
167
+ impl From < UpdateError > for CreateError {
168
+ fn from ( value : UpdateError ) -> Self {
169
+ match value {
170
+ UpdateError :: Conflict => CreateError :: Conflict ,
171
+ UpdateError :: Storage ( storage) => CreateError :: Storage ( storage) ,
172
+ UpdateError :: Other ( other) => CreateError :: Other ( other) ,
173
+ }
174
+ }
175
+ }
176
+
177
+ #[ derive( Error , Debug ) ]
178
+ pub enum UpdateError {
179
+ #[ error( "conflict" ) ]
180
+ Conflict ,
181
+ #[ error( transparent) ]
182
+ Storage ( #[ from] StorageError ) ,
183
+ #[ error( transparent) ]
184
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
185
+ }
186
+
187
+ #[ derive( Error , Debug ) ]
188
+ pub enum DeleteError {
189
+ #[ error( transparent) ]
190
+ Storage ( #[ from] StorageError ) ,
191
+ #[ error( transparent) ]
192
+ Other ( #[ from] Box < dyn std:: error:: Error > ) ,
119
193
}
120
194
121
195
#[ derive( Debug , Clone , PartialEq , Eq ) ]
0 commit comments