use std::fmt;
use graphql_parser::Pos;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PageSizeArgument {
First,
Last,
}
impl PageSizeArgument {
pub const fn as_str(self) -> &'static str {
match self {
Self::First => "first",
Self::Last => "last",
}
}
}
impl fmt::Display for PageSizeArgument {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Position {
pub line: usize,
pub column: usize,
}
impl From<Pos> for Position {
fn from(pos: Pos) -> Self {
Self {
line: pos.line,
column: pos.column,
}
}
}
impl fmt::Display for Position {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.line, self.column)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum NodeCountError {
Parse {
message: String,
},
NoOperation,
MultipleOperations {
names: Vec<String>,
},
UnboundVariable {
field: String,
argument: PageSizeArgument,
variable: String,
position: Position,
},
PageSizeOutOfRange {
field: String,
argument: PageSizeArgument,
value: i64,
position: Position,
},
PageSizeNotAnInteger {
field: String,
argument: PageSizeArgument,
found: String,
position: Position,
},
UndefinedFragment {
name: String,
position: Position,
},
FragmentCycle {
name: String,
position: Position,
},
Overflow {
field: String,
position: Position,
},
}
impl fmt::Display for NodeCountError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Parse { message } => {
write!(f, "the document is not valid GraphQL: {message}")
}
Self::NoOperation => f.write_str("the document declares no operation to count"),
Self::MultipleOperations { names } => write!(
f,
"the document declares {} operations ({}); node_count counts one \
operation and cannot tell which was meant",
names.len(),
names.join(", "),
),
Self::UnboundVariable {
field,
argument,
variable,
position,
} => write!(
f,
"at {position}, field `{field}` passes `{argument}: ${variable}`, \
but no binding for `{variable}` was supplied",
),
Self::PageSizeOutOfRange {
field,
argument,
value,
position,
} => write!(
f,
"at {position}, field `{field}` passes `{argument}: {value}`, \
outside GitHub's page-size range 1..=100",
),
Self::PageSizeNotAnInteger {
field,
argument,
found,
position,
} => write!(
f,
"at {position}, field `{field}` passes `{argument}: {found}`, \
which is neither an integer nor a variable",
),
Self::UndefinedFragment { name, position } => write!(
f,
"at {position}, the spread `...{name}` names a fragment the document \
does not define",
),
Self::FragmentCycle { name, position } => write!(
f,
"at {position}, the spread `...{name}` re-enters a fragment already \
being counted; fragment spreads must not form a cycle",
),
Self::Overflow { field, position } => write!(
f,
"at {position}, the node count for field `{field}` grew past what a \
64-bit integer can express; the document is far above NODE_LIMIT",
),
}
}
}
impl std::error::Error for NodeCountError {}