@@ -118,6 +118,10 @@ use crate::util::interning::InternedString;
118
118
use crate :: util:: { Graph , internal} ;
119
119
use anyhow:: { Context as _, bail} ;
120
120
use cargo_util_schemas:: core:: SourceKind ;
121
+ use cargo_util_schemas:: lockfile:: {
122
+ EncodablePackageIdError , EncodablePackageIdErrorKind , EncodableSourceIdError ,
123
+ EncodableSourceIdErrorKind ,
124
+ } ;
121
125
use serde:: de;
122
126
use serde:: ser;
123
127
use serde:: { Deserialize , Serialize } ;
@@ -550,14 +554,16 @@ pub struct EncodableSourceId {
550
554
}
551
555
552
556
impl EncodableSourceId {
553
- pub fn new ( source : String ) -> CargoResult < Self > {
557
+ pub fn new ( source : String ) -> Result < Self , EncodableSourceIdError > {
554
558
let source_str = source. clone ( ) ;
555
- let ( kind, url) = source
556
- . split_once ( '+' )
557
- . ok_or_else ( || anyhow :: format_err! ( "invalid source `{}`" , source_str ) ) ?;
559
+ let ( kind, url) = source. split_once ( '+' ) . ok_or_else ( || {
560
+ EncodableSourceIdError ( EncodableSourceIdErrorKind :: InvalidSource ( source . clone ( ) ) . into ( ) )
561
+ } ) ?;
558
562
559
- let url =
560
- Url :: parse ( url) . map_err ( |s| anyhow:: format_err!( "invalid url `{}`: {}" , url, s) ) ?;
563
+ let url = Url :: parse ( url) . map_err ( |msg| EncodableSourceIdErrorKind :: InvalidUrl {
564
+ url : url. to_string ( ) ,
565
+ msg : msg. to_string ( ) ,
566
+ } ) ?;
561
567
562
568
let kind = match kind {
563
569
"git" => {
@@ -567,7 +573,9 @@ impl EncodableSourceId {
567
573
"registry" => SourceKind :: Registry ,
568
574
"sparse" => SourceKind :: SparseRegistry ,
569
575
"path" => SourceKind :: Path ,
570
- kind => anyhow:: bail!( "unsupported source protocol: {}" , kind) ,
576
+ kind => {
577
+ return Err ( EncodableSourceIdErrorKind :: UnsupportedSource ( kind. to_string ( ) ) . into ( ) ) ;
578
+ }
571
579
} ;
572
580
573
581
Ok ( Self {
@@ -663,9 +671,9 @@ impl fmt::Display for EncodablePackageId {
663
671
}
664
672
665
673
impl FromStr for EncodablePackageId {
666
- type Err = anyhow :: Error ;
674
+ type Err = EncodablePackageIdError ;
667
675
668
- fn from_str ( s : & str ) -> CargoResult < EncodablePackageId > {
676
+ fn from_str ( s : & str ) -> Result < EncodablePackageId , Self :: Err > {
669
677
let mut s = s. splitn ( 3 , ' ' ) ;
670
678
let name = s. next ( ) . unwrap ( ) ;
671
679
let version = s. next ( ) ;
@@ -674,7 +682,7 @@ impl FromStr for EncodablePackageId {
674
682
if let Some ( s) = s. strip_prefix ( '(' ) . and_then ( |s| s. strip_suffix ( ')' ) ) {
675
683
Some ( EncodableSourceId :: new ( s. to_string ( ) ) ?)
676
684
} else {
677
- anyhow :: bail! ( "invalid serialized PackageId" )
685
+ return Err ( EncodablePackageIdErrorKind :: InvalidSerializedPackageId . into ( ) ) ;
678
686
}
679
687
}
680
688
None => None ,
0 commit comments