use pedant_syntax::go::GoFactError;
use crate::resolution::read::ReadFault;
use super::limits::GoResolutionLimits;
use super::paths::PathFault;
use super::snapshot_error::GoSourceDefect;
#[derive(Debug, thiserror::Error)]
pub enum GoSourceFault {
#[error("the source could not be read: {0}")]
Unreadable(#[source] std::io::Error),
#[error("path {path} lies outside the provider's root")]
OutOfRoot {
path: Box<str>,
},
#[error("path {path} beneath the provider's root is not valid UTF-8")]
NonUtf8Path {
path: Box<str>,
},
#[error("failed to resolve {path}: {source}")]
PathRead {
path: Box<str>,
#[source]
source: std::io::Error,
},
#[error("the provider already holds its ceiling of {ceiling} source files")]
SourceFiles {
ceiling: u32,
},
#[error("the source holds more than the provider's ceiling of {ceiling} bytes")]
SourceBytes {
ceiling: u64,
},
#[error("the source passes the provider's ceiling of {ceiling} total source bytes")]
TotalBytes {
ceiling: u64,
},
#[error("the source is not valid UTF-8: {reason}")]
NonUtf8 {
reason: Box<str>,
},
#[error("the source is not valid Go: {reason}")]
Unparsed {
reason: Box<str>,
},
#[error("the source states no complete tree: {0}")]
Incomplete(GoSourceDefect),
#[error("the bounded fact walk refused: {0}")]
FactExtraction(#[source] GoFactError),
#[error("the source states no package clause")]
MissingPackageClause,
#[error("{path} was already refused by this provider")]
Refused {
path: Box<str>,
},
#[error("the source states no complete structure inventory: {reason}")]
StructureProjection {
reason: Box<str>,
},
}
impl GoSourceFault {
pub(super) fn of_read(fault: ReadFault, limits: GoResolutionLimits) -> Self {
match fault {
ReadFault::Unreadable(source) => Self::Unreadable(source),
ReadFault::SourceBytes => Self::SourceBytes {
ceiling: limits.max_source_file_bytes,
},
ReadFault::TotalBytes => Self::TotalBytes {
ceiling: limits.max_total_source_bytes,
},
}
}
}
impl From<PathFault> for GoSourceFault {
fn from(fault: PathFault) -> Self {
match fault {
PathFault::OutOfRoot { path, .. } => Self::OutOfRoot { path },
PathFault::NonUtf8 { path } => Self::NonUtf8Path { path },
PathFault::Unreadable { path, source } => Self::PathRead { path, source },
}
}
}