@@ -164,27 +164,51 @@ pub type Result<T> = std::result::Result<T, Error>;
164
164
Serialize ,
165
165
Deserialize
166
166
) ]
167
- pub struct Name ( pub String , pub ShortUuid ) ;
167
+ pub enum Name {
168
+ /// Normal names for most actors.
169
+ Suffixed ( String , ShortUuid ) ,
170
+ /// Reserved names for system actors without UUIDs.
171
+ Reserved ( String ) ,
172
+ }
168
173
169
174
impl Name {
170
175
/// Create a new `Name` from a user-provided base name.
171
176
pub fn new ( name : impl Into < String > ) -> Self {
177
+ Self :: new_with_uuid ( name, Some ( ShortUuid :: generate ( ) ) )
178
+ }
179
+
180
+ /// Create a Reserved `Name` with no uuid. Only for use by system actors.
181
+ pub ( crate ) fn new_reserved ( name : impl Into < String > ) -> Self {
182
+ Self :: new_with_uuid ( name, None )
183
+ }
184
+
185
+ fn new_with_uuid ( name : impl Into < String > , uuid : Option < ShortUuid > ) -> Self {
172
186
let mut name = name. into ( ) ;
173
187
if name. is_empty ( ) {
174
188
name = "unnamed" . to_string ( ) ;
175
189
}
176
- let uuid = ShortUuid :: generate ( ) ;
177
- Self ( name, uuid)
190
+ if let Some ( uuid) = uuid {
191
+ Self :: Suffixed ( name, uuid)
192
+ } else {
193
+ Self :: Reserved ( name)
194
+ }
178
195
}
179
196
180
197
/// The name portion of this `Name`.
181
198
pub fn name ( & self ) -> & str {
182
- & self . 0
199
+ match self {
200
+ Self :: Suffixed ( n, _) => n,
201
+ Self :: Reserved ( n) => n,
202
+ }
183
203
}
184
204
185
205
/// The UUID portion of this `Name`.
206
+ /// Only valid for Name::Suffixed, if called on Name::Reserved it'll panic.
186
207
pub fn uuid ( & self ) -> & ShortUuid {
187
- & self . 1
208
+ match self {
209
+ Self :: Suffixed ( _, uuid) => uuid,
210
+ Self :: Reserved ( _) => panic ! ( "Reserved name has no UUID" ) ,
211
+ }
188
212
}
189
213
}
190
214
@@ -208,24 +232,33 @@ impl FromStr for Name {
208
232
type Err = NameParseError ;
209
233
210
234
fn from_str ( s : & str ) -> std:: result:: Result < Self , Self :: Err > {
211
- let ( name, uuid) = s. split_once ( '-' ) . ok_or ( NameParseError :: MissingSeparator ) ?;
212
- if name. is_empty ( ) {
213
- return Err ( NameParseError :: MissingName ) ;
235
+ if let Some ( ( name, uuid) ) = s. split_once ( '-' ) {
236
+ if name. is_empty ( ) {
237
+ return Err ( NameParseError :: MissingName ) ;
238
+ }
239
+ if uuid. is_empty ( ) {
240
+ return Err ( NameParseError :: MissingName ) ;
241
+ }
242
+
243
+ Ok ( Name :: new_with_uuid ( name. to_string ( ) , Some ( uuid. parse ( ) ?) ) )
244
+ } else {
245
+ if s. is_empty ( ) {
246
+ return Err ( NameParseError :: MissingName ) ;
247
+ }
248
+ Ok ( Name :: new_reserved ( s) )
214
249
}
215
- if uuid. is_empty ( ) {
216
- return Err ( NameParseError :: MissingName ) ;
217
- }
218
-
219
- let name = name. to_string ( ) ;
220
- let uuid = uuid. parse ( ) ?;
221
- Ok ( Name ( name, uuid) )
222
250
}
223
251
}
224
252
225
253
impl std:: fmt:: Display for Name {
226
254
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
227
- write ! ( f, "{}-" , self . name( ) ) ?;
228
- self . uuid ( ) . format ( f, true /*raw*/ )
255
+ match self {
256
+ Self :: Suffixed ( n, uuid) => {
257
+ write ! ( f, "{}-" , n) ?;
258
+ uuid. format ( f, true /*raw*/ )
259
+ }
260
+ Self :: Reserved ( n) => write ! ( f, "{}" , n) ,
261
+ }
229
262
}
230
263
}
231
264
0 commit comments