Skip to content

Commit 8bea26a

Browse files
committed
Remove thiserror dependency
It is convenient, but not worth adding an extra dependency.
1 parent 7a20ea3 commit 8bea26a

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

graphql_client_codegen/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,3 @@ quote = "^1.0"
1717
serde_json = "1.0"
1818
serde = { version = "^1.0", features = ["derive"] }
1919
syn = "^1.0"
20-
thiserror = "1.0.10"

graphql_client_codegen/src/generated_module.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,23 @@ use crate::{
66
use heck::*;
77
use proc_macro2::{Ident, Span, TokenStream};
88
use quote::quote;
9-
use thiserror::Error;
9+
use std::{error::Error, fmt::Display};
1010

11-
#[derive(Debug, Error)]
12-
#[error(
13-
"Could not find an operation named {} in the query document.",
14-
operation_name
15-
)]
11+
#[derive(Debug)]
1612
struct OperationNotFound {
1713
operation_name: String,
1814
}
1915

16+
impl Display for OperationNotFound {
17+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18+
f.write_str("Could not find an operation named ")?;
19+
f.write_str(&self.operation_name)?;
20+
f.write_str(" in the query document.")
21+
}
22+
}
23+
24+
impl Error for OperationNotFound {}
25+
2026
/// This struct contains the parameters necessary to generate code for a given operation.
2127
pub(crate) struct GeneratedModule<'a> {
2228
pub operation: &'a str,

graphql_client_codegen/src/lib.rs

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,19 @@ mod tests;
2929

3030
pub use crate::codegen_options::{CodegenMode, GraphQLClientCodegenOptions};
3131

32-
use std::{collections::HashMap, io};
33-
use thiserror::Error;
32+
use std::{collections::HashMap, fmt::Display, io};
3433

35-
#[derive(Debug, Error)]
36-
#[error("{0}")]
34+
#[derive(Debug)]
3735
struct GeneralError(String);
3836

37+
impl Display for GeneralError {
38+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39+
f.write_str(&self.0)
40+
}
41+
}
42+
43+
impl std::error::Error for GeneralError {}
44+
3945
type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
4046
type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;
4147

@@ -137,24 +143,34 @@ pub fn generate_module_token_stream(
137143
Ok(modules)
138144
}
139145

140-
#[derive(Debug, Error)]
146+
#[derive(Debug)]
141147
enum ReadFileError {
142-
#[error(
143-
"Could not find file with path: {}\
144-
Hint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = \"src/my_query.graphql\".",
145-
path
146-
)]
147-
FileNotFound {
148-
path: String,
149-
#[source]
150-
io_error: io::Error,
151-
},
152-
#[error("Error reading file at: {}", path)]
153-
ReadError {
154-
path: String,
155-
#[source]
156-
io_error: io::Error,
157-
},
148+
FileNotFound { path: String, io_error: io::Error },
149+
ReadError { path: String, io_error: io::Error },
150+
}
151+
152+
impl Display for ReadFileError {
153+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
154+
match self {
155+
ReadFileError::FileNotFound { path, .. } => {
156+
write!(f, "Could not find file with path: {}\
157+
Hint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = \"src/my_query.graphql\".", path)
158+
}
159+
ReadFileError::ReadError { path, .. } => {
160+
f.write_str("Error reading file at: ")?;
161+
f.write_str(path)
162+
}
163+
}
164+
}
165+
}
166+
167+
impl std::error::Error for ReadFileError {
168+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
169+
match self {
170+
ReadFileError::FileNotFound { path: _, io_error } => Some(io_error),
171+
ReadFileError::ReadError { path: _, io_error } => Some(io_error),
172+
}
173+
}
158174
}
159175

160176
fn read_file(path: &std::path::Path) -> Result<String, ReadFileError> {

graphql_client_codegen/src/query.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,24 @@ use crate::{
1818
StoredInputType, StoredScalar, TypeId, UnionId,
1919
},
2020
};
21-
use std::collections::{HashMap, HashSet};
22-
use thiserror::Error;
21+
use std::{
22+
collections::{HashMap, HashSet},
23+
fmt::Display,
24+
};
2325

24-
#[derive(Debug, Error)]
25-
#[error("{}", message)]
26+
#[derive(Debug)]
2627
pub(crate) struct QueryValidationError {
2728
message: String,
2829
}
2930

31+
impl Display for QueryValidationError {
32+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33+
f.write_str(&self.message)
34+
}
35+
}
36+
37+
impl std::error::Error for QueryValidationError {}
38+
3039
impl QueryValidationError {
3140
pub(crate) fn new(message: String) -> Self {
3241
QueryValidationError { message }

0 commit comments

Comments
 (0)