11use std:: fmt:: { Debug , Display , Formatter } ;
22
3- use anyhow:: anyhow;
43use bimap:: BiBTreeMap ;
54use cid:: Cid ;
65use fvm_ipld_blockstore:: Blockstore ;
7- use fvm_ipld_encoding:: CborStore ;
6+ use fvm_ipld_encoding:: { CborStore , CborStoreError } ;
87use num_derive:: FromPrimitive ;
98use serde_repr:: { Deserialize_repr , Serialize_repr } ;
109
@@ -115,26 +114,36 @@ impl Display for Type {
115114/// A mapping of builtin actor CIDs to their respective types.
116115pub type Manifest = BiBTreeMap < Cid , Type > ;
117116
118- pub fn load_manifest < B : Blockstore > ( bs : & B , root_cid : & Cid , ver : u32 ) -> anyhow:: Result < Manifest > {
117+ pub fn load_manifest < B : Blockstore > (
118+ bs : & B ,
119+ root_cid : & Cid ,
120+ ver : u32 ,
121+ ) -> Result < Manifest , ManifestError < B > > {
119122 match ver {
120123 0 => load_manifest_v0 ( bs, root_cid) ,
121124 1 => load_manifest_v1 ( bs, root_cid) ,
122- _ => Err ( anyhow ! ( "unknown manifest version {}" , ver) ) ,
125+ _ => Err ( ManifestError :: UnknownVersion ( ver) ) ,
123126 }
124127}
125128
126- pub fn load_manifest_v0 < B : Blockstore > ( bs : & B , root_cid : & Cid ) -> anyhow:: Result < Manifest > {
129+ pub fn load_manifest_v0 < B : Blockstore > (
130+ bs : & B ,
131+ root_cid : & Cid ,
132+ ) -> Result < Manifest , ManifestError < B > > {
127133 match bs. get_cbor :: < Manifest > ( root_cid) ? {
128134 Some ( mf) => Ok ( mf) ,
129- None => Err ( anyhow ! ( "cannot find manifest root cid {}" , root_cid) ) ,
135+ None => Err ( ManifestError :: MissingRootCid ( * root_cid) ) ,
130136 }
131137}
132138
133- pub fn load_manifest_v1 < B : Blockstore > ( bs : & B , root_cid : & Cid ) -> anyhow:: Result < Manifest > {
139+ pub fn load_manifest_v1 < B : Blockstore > (
140+ bs : & B ,
141+ root_cid : & Cid ,
142+ ) -> Result < Manifest , ManifestError < B > > {
134143 let vec: Vec < ( String , Cid ) > = match bs. get_cbor ( root_cid) ? {
135144 Some ( vec) => vec,
136145 None => {
137- return Err ( anyhow ! ( "cannot find manifest root cid {}" , root_cid) ) ;
146+ return Err ( ManifestError :: MissingRootCid ( * root_cid) ) ;
138147 }
139148 } ;
140149 let mut manifest = Manifest :: new ( ) ;
@@ -145,9 +154,21 @@ pub fn load_manifest_v1<B: Blockstore>(bs: &B, root_cid: &Cid) -> anyhow::Result
145154 manifest. insert ( code_cid, t) ;
146155 }
147156 Err ( what) => {
148- return Err ( anyhow ! ( "bad builtin actor name: {}: {} " , name, what) ) ;
157+ return Err ( ManifestError :: BadBuiltinActor ( name, what) ) ;
149158 }
150159 }
151160 }
152161 Ok ( manifest)
153162}
163+
164+ #[ derive( thiserror:: Error , Debug ) ]
165+ pub enum ManifestError < BS : Blockstore > {
166+ #[ error( "unknown manifest version {0}" ) ]
167+ UnknownVersion ( u32 ) ,
168+ #[ error( "cannot find manifest root cid {0}" ) ]
169+ MissingRootCid ( Cid ) ,
170+ #[ error( "bad builtin actor name: {0}: {1}" ) ]
171+ BadBuiltinActor ( String , String ) ,
172+ #[ error( "encoding {0}" ) ]
173+ Encoding ( #[ from] CborStoreError < BS > ) ,
174+ }
0 commit comments