verifier/
errors.rs

1use reqwest::StatusCode;
2use scarb_metadata::{Metadata, PackageId};
3use std::fmt::{self, Formatter};
4use thiserror::Error;
5use url::Url;
6
7#[derive(Debug, Error)]
8pub struct MissingPackage {
9    pub package_id: PackageId,
10    pub available: Vec<PackageId>,
11}
12
13impl MissingPackage {
14    #[must_use]
15    pub fn new(package_id: &PackageId, metadata: &Metadata) -> Self {
16        Self {
17            package_id: package_id.clone(),
18            available: metadata.workspace.members.clone(),
19        }
20    }
21}
22
23impl fmt::Display for MissingPackage {
24    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
25        writeln!(
26            formatter,
27            "Couldn't find package: {}, workspace have those packages available:",
28            self.package_id
29        )?;
30
31        for package in &self.available {
32            writeln!(formatter, "{package}")?;
33        }
34
35        Ok(())
36    }
37}
38
39#[derive(Debug, Error)]
40pub struct RequestFailure {
41    pub url: Url,
42    pub status: StatusCode,
43    pub msg: String,
44}
45
46impl RequestFailure {
47    pub fn new(url: Url, status: StatusCode, msg: impl Into<String>) -> Self {
48        Self {
49            url,
50            status,
51            msg: msg.into(),
52        }
53    }
54}
55
56impl fmt::Display for RequestFailure {
57    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
58        write!(
59            formatter,
60            "{:?}\n returned {}, with:\n{}",
61            self.url, self.status, self.msg
62        )
63    }
64}
65
66// TODO: Display suggestions
67#[derive(Debug, Error)]
68pub struct MissingContract {
69    pub name: String,
70    pub available: Vec<String>,
71}
72
73impl MissingContract {
74    #[must_use]
75    pub const fn new(name: String, available: Vec<String>) -> Self {
76        Self { name, available }
77    }
78}
79
80impl fmt::Display for MissingContract {
81    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
82        let contracts = self.available.join(", ");
83        write!(
84            formatter,
85            "Contract: {} is not defined in the manifest file. Did you mean one of: {}?",
86            self.name, contracts
87        )
88    }
89}