Skip to content

Commit

Permalink
chore: taillcall prettier anyhow cleanup (#2454)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 authored Jul 27, 2024
1 parent dea5527 commit b0b55d7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tailcall-prettier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
anyhow = "1.0.82"
derive_more = { workspace = true }
lazy_static = "1.4.0"
strum_macros = "0.26.2"
tokio.workspace = true
56 changes: 56 additions & 0 deletions tailcall-prettier/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fmt::Display;
use std::string::FromUtf8Error;
use std::sync::Arc;

use derive_more::{DebugCustom, From};
use tokio::task::JoinError;

#[derive(From, DebugCustom)]
pub enum Error {
#[debug(fmt = "Std IO Error: {}", _0)]
IO(std::io::Error),

#[debug(fmt = "Join Error: {}", _0)]
Join(JoinError),

#[debug(fmt = "From Utf8 Error: {}", _0)]
FromUtf8(FromUtf8Error),

#[debug(fmt = "Prettier formatting failed: {}", _0)]
PrettierFormattingFailed(String),

#[debug(fmt = "No file extension found")]
FileExtensionNotFound,

#[debug(fmt = "Unsupported file type")]
UnsupportedFiletype,

#[debug(fmt = "{}\n\nCaused by:\n {}", context, source)]
Context { source: Arc<Error>, context: String },
}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::IO(error) => write!(f, "Std IO Error: {}", error),
Error::Join(error) => write!(f, "Join Error: {}", error),
Error::FromUtf8(error) => write!(f, "From Utf8 Error: {}", error),
Error::PrettierFormattingFailed(msg) => {
write!(f, "Prettier formatting failed: {}", msg)
}
Error::FileExtensionNotFound => write!(f, "No file extension found"),
Error::UnsupportedFiletype => write!(f, "Unsupported file type"),
Error::Context { source, context } => {
write!(f, "{}\n\nCaused by:\n {}", context, source)
}
}
}
}

impl Error {
pub fn with_context(self, context: String) -> Self {
Error::Context { source: Arc::new(self), context }
}
}

pub type Result<A> = std::result::Result<A, Error>;
6 changes: 4 additions & 2 deletions tailcall-prettier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::sync::Arc;
mod error;
mod parser;
mod prettier;
use anyhow::Result;
pub use error::{Error, Result};
pub use parser::Parser;
use prettier::Prettier;

Expand All @@ -15,10 +16,11 @@ pub async fn format<T: AsRef<str>>(source: T, parser: &Parser) -> Result<String>

#[cfg(test)]
mod tests {
use super::Result;
use crate::{format, Parser};

#[tokio::test]
async fn test_js() -> anyhow::Result<()> {
async fn test_js() -> Result<()> {
let prettier = format("const x={a:3};", &Parser::Js).await?;
assert_eq!("const x = {a: 3}\n", prettier);
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions tailcall-prettier/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::{anyhow, Result};
use super::{Error, Result};

#[derive(strum_macros::Display, Clone)]
pub enum Parser {
Expand All @@ -15,7 +15,7 @@ impl Parser {
let ext = path
.split('.')
.last()
.ok_or(anyhow!("No file extension found"))?
.ok_or(Error::FileExtensionNotFound)?
.to_lowercase();
match ext.as_str() {
"gql" | "graphql" => Ok(Parser::Gql),
Expand All @@ -24,7 +24,7 @@ impl Parser {
"md" => Ok(Parser::Md),
"ts" => Ok(Parser::Ts),
"js" => Ok(Parser::Js),
_ => Err(anyhow!("Unsupported file type")),
_ => Err(Error::UnsupportedFiletype),
}
}
}
7 changes: 3 additions & 4 deletions tailcall-prettier/src/prettier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::io::Write;
use std::path::Path;
use std::process::{Command, Stdio};

use anyhow::{anyhow, Result};
use tokio::runtime::Runtime;

use super::{Error, Result};
use crate::Parser;

/// Struct representing a Prettier formatter.
Expand Down Expand Up @@ -81,9 +81,8 @@ impl Prettier {
if output.status.success() {
Ok(String::from_utf8(output.stdout)?)
} else {
Err(anyhow!(
"Prettier formatting failed: {}",
String::from_utf8(output.stderr).unwrap()
Err(Error::PrettierFormattingFailed(
String::from_utf8(output.stderr).unwrap(),
))
}
})
Expand Down
4 changes: 2 additions & 2 deletions tests/core/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ async fn check_server_config(spec: ExecutionSpec) -> Vec<Config> {

let actual = tailcall_prettier::format(actual, &tailcall_prettier::Parser::Gql)
.await
.context(context.clone())
.map_err(|e| e.with_context(context.clone()))
.unwrap();

let expected = tailcall_prettier::format(content, &tailcall_prettier::Parser::Gql)
.await
.context(context)
.map_err(|e| e.with_context(context.clone()))
.unwrap();

pretty_assertions::assert_eq!(
Expand Down

0 comments on commit b0b55d7

Please sign in to comment.