Skip to content

Commit

Permalink
chore: fixes & anyhow removal from main code
Browse files Browse the repository at this point in the history
  • Loading branch information
mehulmathur16 committed Jun 26, 2024
1 parent b1b501f commit c3d3158
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 30 deletions.
11 changes: 6 additions & 5 deletions src/cli/generator/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize};
use url::Url;

use crate::core::config::{self};
use crate::core::error::Error;

#[derive(Deserialize, Serialize, Debug, Default, Setters)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -124,7 +125,7 @@ pub struct Schema {
}

impl Output<UnResolved> {
pub fn resolve(self, parent_dir: Option<&Path>) -> anyhow::Result<Output<Resolved>> {
pub fn resolve(self, parent_dir: Option<&Path>) -> Result<Output<Resolved>, Error> {

Check warning on line 128 in src/cli/generator/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/config.rs#L128

Added line #L128 was not covered by tests
Ok(Output {
format: self.format,
path: self.path.into_resolved(parent_dir),
Expand All @@ -133,7 +134,7 @@ impl Output<UnResolved> {
}

impl Source<UnResolved> {
pub fn resolve(self, parent_dir: Option<&Path>) -> anyhow::Result<Source<Resolved>> {
pub fn resolve(self, parent_dir: Option<&Path>) -> Result<Source<Resolved>, Error> {

Check warning on line 137 in src/cli/generator/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/config.rs#L137

Added line #L137 was not covered by tests
match self {
Source::Curl { src, field_name } => {
let resolved_path = src.into_resolved(parent_dir);
Expand All @@ -152,22 +153,22 @@ impl Source<UnResolved> {
}

impl Input<UnResolved> {
pub fn resolve(self, parent_dir: Option<&Path>) -> anyhow::Result<Input<Resolved>> {
pub fn resolve(self, parent_dir: Option<&Path>) -> Result<Input<Resolved>, Error> {

Check warning on line 156 in src/cli/generator/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/config.rs#L156

Added line #L156 was not covered by tests
let resolved_source = self.source.resolve(parent_dir)?;
Ok(Input { source: resolved_source })
}
}

impl Config {
/// Resolves all the relative paths present inside the GeneratorConfig.
pub fn into_resolved(self, config_path: &str) -> anyhow::Result<Config<Resolved>> {
pub fn into_resolved(self, config_path: &str) -> Result<Config<Resolved>, Error> {

Check warning on line 164 in src/cli/generator/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/config.rs#L164

Added line #L164 was not covered by tests
let parent_dir = Some(Path::new(config_path).parent().unwrap_or(Path::new("")));

let inputs = self
.inputs
.into_iter()
.map(|input| input.resolve(parent_dir))
.collect::<anyhow::Result<Vec<Input<Resolved>>>>()?;
.collect::<Result<Vec<Input<Resolved>>, Error>>()?;

Check warning on line 171 in src/cli/generator/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/config.rs#L171

Added line #L171 was not covered by tests

let output = self.output.resolve(parent_dir)?;

Expand Down
11 changes: 6 additions & 5 deletions src/cli/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use pathdiff::diff_paths;
use super::config::{Config, Resolved, Source};
use super::source::ConfigSource;
use crate::core::config::{self, ConfigModule};
use crate::core::error::Error;
use crate::core::generator::{Generator as ConfigGenerator, Input};
use crate::core::proto_reader::ProtoReader;
use crate::core::resource_reader::ResourceReader;
Expand All @@ -26,7 +27,7 @@ impl Generator {
}

/// Writes the configuration to the output file if allowed.
async fn write(self, graphql_config: &ConfigModule, output_path: &str) -> anyhow::Result<()> {
async fn write(self, graphql_config: &ConfigModule, output_path: &str) -> Result<(), Error> {

Check warning on line 30 in src/cli/generator/generator.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/generator.rs#L30

Added line #L30 was not covered by tests
let output_source = config::Source::detect(output_path)?;
let config = match output_source {
config::Source::Json => graphql_config.to_json(true)?,
Expand All @@ -48,7 +49,7 @@ impl Generator {

/// Checks if the output file already exists and prompts for overwrite
/// confirmation.
fn should_overwrite(&self, output_path: &str) -> anyhow::Result<bool> {
fn should_overwrite(&self, output_path: &str) -> Result<bool, Error> {

Check warning on line 52 in src/cli/generator/generator.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/generator.rs#L52

Added line #L52 was not covered by tests
if is_exists(output_path) {
let should_overwrite = Confirm::new(
format!(
Expand All @@ -66,7 +67,7 @@ impl Generator {
Ok(true)
}

async fn read(&self) -> anyhow::Result<Config<Resolved>> {
async fn read(&self) -> Result<Config<Resolved>, Error> {

Check warning on line 70 in src/cli/generator/generator.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/generator.rs#L70

Added line #L70 was not covered by tests
let config_path = &self.config_path;
let source = ConfigSource::detect(config_path)?;
let config_content = self.runtime.file.read(config_path).await?;
Expand All @@ -82,7 +83,7 @@ impl Generator {

/// performs all the i/o's required in the config file and generates
/// concrete vec containing data for generator.
async fn resolve_io(&self, config: Config<Resolved>) -> anyhow::Result<Vec<Input>> {
async fn resolve_io(&self, config: Config<Resolved>) -> Result<Vec<Input>, Error> {

Check warning on line 86 in src/cli/generator/generator.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/generator.rs#L86

Added line #L86 was not covered by tests
let mut input_samples = vec![];

let reader = ResourceReader::cached(self.runtime.clone());
Expand Down Expand Up @@ -123,7 +124,7 @@ impl Generator {
}

/// generates the final configuration.
pub async fn generate(self) -> anyhow::Result<ConfigModule> {
pub async fn generate(self) -> Result<ConfigModule, Error> {

Check warning on line 127 in src/cli/generator/generator.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/generator/generator.rs#L127

Added line #L127 was not covered by tests
let config = self.read().await?;
let path = config.output.path.0.to_owned();
let query_type = config.schema.query.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/cli/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

pub mod config;
mod generator;
mod source;
pub mod source;

pub use generator::Generator;
1 change: 0 additions & 1 deletion src/cli/tc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use crate::cli::server::Server;
use crate::core::blueprint::Blueprint;
use crate::core::config::reader::ConfigReader;
use crate::core::error::Error;
use crate::core::generator::Generator;
use crate::core::http::API_URL_PREFIX;
use crate::core::print_schema;
use crate::core::rest::{EndpointSet, Unchecked};
Expand Down
1 change: 0 additions & 1 deletion src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::fmt::{self, Display};
use std::num::NonZeroU64;

use async_graphql::parser::types::ServiceDocument;
use derive_setters::Setters;
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand Down
4 changes: 2 additions & 2 deletions src/core/config/transformer/ambiguous_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ mod tests {

use crate::core::config::transformer::AmbiguousType;
use crate::core::config::{Config, Type};
use crate::core::error::Error;
use crate::core::generator::{Generator, Input};
use crate::core::proto_reader::ProtoMetadata;
use crate::core::transform::Transform;
use crate::core::error::Error;
use crate::core::valid::Validator;

fn build_qry(mut config: Config) -> Config {
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
assert_snapshot!(config.to_sdl());
}

fn compile_protobuf(files: &[&str]) -> anyhow::Result<FileDescriptorSet> {
fn compile_protobuf(files: &[&str]) -> Result<FileDescriptorSet, Error> {
Ok(protox::compile(files, [protobuf::SELF])?)
}

Expand Down
5 changes: 3 additions & 2 deletions src/core/config/transformer/merge_types/type_merger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ mod test {

use super::TypeMerger;
use crate::core::config::{Config, Field, Type};
use crate::core::error::Error;
use crate::core::transform::Transform;
use crate::core::valid::Validator;

Expand All @@ -233,7 +234,7 @@ mod test {
}

#[test]
fn test_cyclic_merge_case() -> anyhow::Result<()> {
fn test_cyclic_merge_case() -> Result<(), Error> {
let str_field = Field { type_of: "String".to_owned(), ..Default::default() };
let int_field = Field { type_of: "Int".to_owned(), ..Default::default() };
let bool_field = Field { type_of: "Boolean".to_owned(), ..Default::default() };
Expand Down Expand Up @@ -281,7 +282,7 @@ mod test {
}

#[test]
fn test_type_merger() -> anyhow::Result<()> {
fn test_type_merger() -> Result<(), Error> {
let str_field = Field { type_of: "String".to_owned(), ..Default::default() };
let int_field = Field { type_of: "Int".to_owned(), ..Default::default() };
let bool_field = Field { type_of: "Boolean".to_owned(), ..Default::default() };
Expand Down
4 changes: 4 additions & 0 deletions src/core/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::ir;
use super::rest::error::Error as RestError;
use super::valid::ValidationError;
use crate::cli::error::Error as CLIError;

Check failure on line 16 in src/core/error.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

failed to resolve: could not find `cli` in the crate root

Check failure on line 16 in src/core/error.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

failed to resolve: could not find `cli` in the crate root
use crate::cli::generator::source::UnsupportedFileFormat;

Check failure on line 17 in src/core/error.rs

View workflow job for this annotation

GitHub Actions / Run Tests (WASM)

failed to resolve: could not find `cli` in the crate root

Check failure on line 17 in src/core/error.rs

View workflow job for this annotation

GitHub Actions / Test AWS Lambda Build

failed to resolve: could not find `cli` in the crate root

#[derive(From, thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -166,6 +167,9 @@ pub enum Error {

#[error("Headers Error")]
Headers(headers::Error),

#[error("Unsupported File Format")]
UnsupportedFileFormat(UnsupportedFileFormat),
}

pub mod file {
Expand Down
4 changes: 2 additions & 2 deletions src/core/generator/from_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::json::{self, TypesGenerator};
use super::NameGenerator;
use crate::core::config::Config;
use crate::core::merge_right::MergeRight;
use crate::core::error::Error;
use crate::core::transform::{Transform, TransformerOps};
use crate::core::valid::{Valid, Validator};

Expand Down Expand Up @@ -77,6 +76,7 @@ mod tests {
use serde::Deserialize;

use crate::core::config::transformer::Preset;
use crate::core::error::Error;
use crate::core::generator::{FromJsonGenerator, NameGenerator, RequestSample};
use crate::core::transform::TransformerOps;
use crate::core::valid::Validator;
Expand All @@ -93,7 +93,7 @@ mod tests {
}

#[test]
fn generate_config_from_json() -> anyhow::Result<()> {
fn generate_config_from_json() -> Result<(), Error> {
let mut request_samples = vec![];
let fixtures = [
"src/core/generator/tests/fixtures/json/incompatible_properties.json",
Expand Down
19 changes: 10 additions & 9 deletions src/core/generator/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Generator {
&self,
type_name_generator: &NameGenerator,
json_samples: &[RequestSample],
) -> anyhow::Result<Config> {
) -> Result<Config, Error> {
Ok(
FromJsonGenerator::new(json_samples, type_name_generator, &self.operation_name)
.generate()
Expand All @@ -73,7 +73,7 @@ impl Generator {
&self,
metadata: &ProtoMetadata,
operation_name: &str,
) -> anyhow::Result<Config> {
) -> Result<Config, Error> {
let descriptor_set = resolve_file_descriptor_set(metadata.descriptor_set.clone())?;
let mut config = from_proto(&[descriptor_set], operation_name)?;
config.links.push(Link {
Expand All @@ -85,7 +85,7 @@ impl Generator {
}

/// Generated the actual configuratio from provided samples.
pub fn generate(&self, use_transformers: bool) -> anyhow::Result<ConfigModule> {
pub fn generate(&self, use_transformers: bool) -> Result<ConfigModule, Error> {
let mut config: Config = Config::default();
let type_name_generator = NameGenerator::new(&self.type_name_prefix);

Expand Down Expand Up @@ -143,11 +143,12 @@ mod test {

use super::Generator;
use crate::core::config::transformer::Preset;
use crate::core::error::Error;
use crate::core::generator::generator::Input;
use crate::core::generator::NameGenerator;
use crate::core::proto_reader::ProtoMetadata;

fn compile_protobuf(files: &[&str]) -> anyhow::Result<FileDescriptorSet> {
fn compile_protobuf(files: &[&str]) -> Result<FileDescriptorSet, Error> {
Ok(protox::compile(files, [tailcall_fixtures::protobuf::SELF])?)
}

Expand All @@ -163,7 +164,7 @@ mod test {
}

#[test]
fn should_generate_config_from_proto() -> anyhow::Result<()> {
fn should_generate_config_from_proto() -> Result<(), Error> {
let news_proto = tailcall_fixtures::protobuf::NEWS;
let set = compile_protobuf(&[news_proto])?;

Expand All @@ -179,7 +180,7 @@ mod test {
}

#[test]
fn should_generate_config_from_configs() -> anyhow::Result<()> {
fn should_generate_config_from_configs() -> Result<(), Error> {
let cfg_module = Generator::default()
.inputs(vec![Input::Config {
schema: std::fs::read_to_string(tailcall_fixtures::configs::USER_POSTS)?,
Expand All @@ -192,7 +193,7 @@ mod test {
}

#[test]
fn should_generate_config_from_json() -> anyhow::Result<()> {
fn should_generate_config_from_json() -> Result<(), Error> {
let parsed_content =
parse_json("src/core/generator/tests/fixtures/json/incompatible_properties.json");
let cfg_module = Generator::default()
Expand All @@ -208,7 +209,7 @@ mod test {
}

#[test]
fn should_generate_combined_config() -> anyhow::Result<()> {
fn should_generate_combined_config() -> Result<(), Error> {
// Proto input
let news_proto = tailcall_fixtures::protobuf::NEWS;
let proto_set = compile_protobuf(&[news_proto])?;
Expand Down Expand Up @@ -244,7 +245,7 @@ mod test {
}

#[test]
fn generate_from_config_from_multiple_jsons() -> anyhow::Result<()> {
fn generate_from_config_from_multiple_jsons() -> Result<(), Error> {
let mut inputs = vec![];
let json_fixtures = [
"src/core/generator/tests/fixtures/json/incompatible_properties.json",
Expand Down
2 changes: 1 addition & 1 deletion src/core/generator/tests/json_to_config_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::Path;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use tailcall::core::generator::{Generator, Input};
use tailcall::core::error::Error;
use tailcall::core::generator::{Generator, Input};
use url::Url;

#[derive(Serialize, Deserialize)]
Expand Down
6 changes: 6 additions & 0 deletions src/core/ir/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum Error {

AuthError(String),

ExprEvalError(String),

WorkerError(String),

HttpError(String),
Expand Down Expand Up @@ -68,6 +70,10 @@ impl From<Error> for crate::core::Errata {
CoreError::new("Authentication Error").description(message)
}

Error::ExprEvalError(message) => {
CoreError::new("Expression Evaluation Error").description(message)

Check warning on line 74 in src/core/ir/error.rs

View check run for this annotation

Codecov / codecov/patch

src/core/ir/error.rs#L73-L74

Added lines #L73 - L74 were not covered by tests
}

Error::WorkerError(message) => CoreError::new("Worker Error").description(message),

Error::HttpError(message) => CoreError::new("HTTP Error").description(message),
Expand Down
3 changes: 2 additions & 1 deletion src/core/mustache/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use nom::sequence::delimited;
use nom::{Finish, IResult};

use super::*;
use crate::core::error::Error;

impl Mustache {
// TODO: infallible function, no need to return Result
pub fn parse(str: &str) -> anyhow::Result<Mustache> {
pub fn parse(str: &str) -> Result<Mustache, Error> {
let result = parse_mustache(str).finish();
match result {
Ok((_, mustache)) => Ok(mustache),
Expand Down

1 comment on commit c3d3158

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 6.74ms 3.52ms 134.87ms 78.30%
Req/Sec 3.78k 214.61 4.13k 92.00%

451110 requests in 30.01s, 2.26GB read

Requests/sec: 15030.77

Transfer/sec: 77.15MB

Please sign in to comment.