1
1
// Copyright (c) 2022 Yuki Kishimoto
2
2
// Distributed under the MIT software license
3
3
4
+ use std:: fmt;
5
+ use std:: str:: FromStr ;
6
+
4
7
use serde:: { de, Deserialize , Deserializer , Serialize , Serializer } ;
5
8
6
9
use crate :: error:: Error ;
@@ -16,6 +19,12 @@ pub enum Priority {
16
19
Min = 1 ,
17
20
}
18
21
22
+ impl fmt:: Display for Priority {
23
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
24
+ write ! ( f, "{}" , self . as_str( ) )
25
+ }
26
+ }
27
+
19
28
impl Priority {
20
29
pub fn from_u8 ( priority : u8 ) -> Result < Self , Error > {
21
30
match priority {
@@ -33,6 +42,32 @@ impl Priority {
33
42
pub fn as_u8 ( & self ) -> u8 {
34
43
* self as u8
35
44
}
45
+
46
+ /// Convert to `&str`
47
+ pub fn as_str ( & self ) -> & str {
48
+ match self {
49
+ Self :: Max => "max" ,
50
+ Self :: High => "high" ,
51
+ Self :: Default => "default" ,
52
+ Self :: Low => "low" ,
53
+ Self :: Min => "min" ,
54
+ }
55
+ }
56
+ }
57
+
58
+ impl FromStr for Priority {
59
+ type Err = Error ;
60
+
61
+ fn from_str ( priority : & str ) -> Result < Self , Self :: Err > {
62
+ match priority {
63
+ "max" | "urgent" => Ok ( Self :: Max ) ,
64
+ "high" => Ok ( Self :: High ) ,
65
+ "default" => Ok ( Self :: Default ) ,
66
+ "low" => Ok ( Self :: Low ) ,
67
+ "min" => Ok ( Self :: Min ) ,
68
+ _ => Err ( Error :: UnknownPriority ) ,
69
+ }
70
+ }
36
71
}
37
72
38
73
impl Serialize for Priority {
0 commit comments