@@ -79,19 +79,27 @@ pub fn path_to_cstr<P: AsRef<Path>>(p: P) -> CString {
7979use std:: error:: Error ;
8080
8181#[ derive( Debug ) ]
82- pub struct InvalidBtreeId ;
82+ pub enum BchToolsErr {
83+ InvalidBtreeId ,
84+ InvalidBkeyType ,
85+ InvalidBpos ,
86+ }
8387
84- impl fmt:: Display for InvalidBtreeId {
88+ impl fmt:: Display for BchToolsErr {
8589 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
86- write ! ( f, "invalid btree id" )
90+ match self {
91+ BchToolsErr :: InvalidBtreeId => write ! ( f, "invalid btree id" ) ,
92+ BchToolsErr :: InvalidBkeyType => write ! ( f, "invalid bkey type" ) ,
93+ BchToolsErr :: InvalidBpos => write ! ( f, "invalid bpos" ) ,
94+ }
8795 }
8896}
8997
90- impl Error for InvalidBtreeId {
98+ impl Error for BchToolsErr {
9199}
92100
93101impl FromStr for c:: btree_id {
94- type Err = InvalidBtreeId ;
102+ type Err = BchToolsErr ;
95103
96104 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
97105 let s = CString :: new ( s) . unwrap ( ) ;
@@ -101,7 +109,23 @@ impl FromStr for c::btree_id {
101109 if v >= 0 {
102110 Ok ( unsafe { std:: mem:: transmute ( v) } )
103111 } else {
104- Err ( InvalidBtreeId )
112+ Err ( BchToolsErr :: InvalidBtreeId )
113+ }
114+ }
115+ }
116+
117+ impl FromStr for c:: bch_bkey_type {
118+ type Err = BchToolsErr ;
119+
120+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
121+ let s = CString :: new ( s) . unwrap ( ) ;
122+ let p = s. as_ptr ( ) ;
123+
124+ let v = unsafe { c:: match_string ( c:: bch2_bkey_types[ ..] . as_ptr ( ) , ( -( 1 as isize ) ) as usize , p) } ;
125+ if v >= 0 {
126+ Ok ( unsafe { std:: mem:: transmute ( v) } )
127+ } else {
128+ Err ( BchToolsErr :: InvalidBkeyType )
105129 }
106130 }
107131}
@@ -134,7 +158,7 @@ impl fmt::Display for Bpos {
134158}
135159
136160impl FromStr for c:: bpos {
137- type Err = InvalidBtreeId ;
161+ type Err = BchToolsErr ;
138162
139163 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
140164 if s == "POS_MIN" {
@@ -150,12 +174,12 @@ impl FromStr for c::bpos {
150174 }
151175
152176 let mut fields = s. split ( ':' ) ;
153- let ino_str = fields. next ( ) . ok_or ( InvalidBtreeId ) ?;
154- let off_str = fields. next ( ) . ok_or ( InvalidBtreeId ) ?;
177+ let ino_str = fields. next ( ) . ok_or ( BchToolsErr :: InvalidBpos ) ?;
178+ let off_str = fields. next ( ) . ok_or ( BchToolsErr :: InvalidBpos ) ?;
155179 let snp_str = fields. next ( ) ;
156180
157- let ino: u64 = ino_str. parse ( ) . map_err ( |_| InvalidBtreeId ) ?;
158- let off: u64 = off_str. parse ( ) . map_err ( |_| InvalidBtreeId ) ?;
181+ let ino: u64 = ino_str. parse ( ) . map_err ( |_| BchToolsErr :: InvalidBpos ) ?;
182+ let off: u64 = off_str. parse ( ) . map_err ( |_| BchToolsErr :: InvalidBpos ) ?;
159183 let snp: u32 = snp_str. map ( |s| s. parse ( ) . ok ( ) ) . flatten ( ) . unwrap_or ( 0 ) ;
160184
161185 Ok ( c:: bpos { inode : ino, offset : off, snapshot : snp } )
0 commit comments