From 4b7b08b6b3a4e3c195de0bf7f5bdc4b84069396b Mon Sep 17 00:00:00 2001 From: Lucas Pickering Date: Tue, 21 Nov 2023 17:16:39 -0500 Subject: [PATCH] Reformat code The desired formatting changed at somepoint. Maybe during the rust downgrade? --- .rustfmt.toml | 2 +- src/cli.rs | 8 ++- src/collection.rs | 2 +- src/collection/insomnia.rs | 13 ++--- src/http.rs | 32 ++++++------ src/http/parse.rs | 9 ++-- src/http/repository.rs | 2 - src/template.rs | 15 +++--- src/template/parse.rs | 7 ++- src/template/render.rs | 82 +++++++++++++++--------------- src/tui.rs | 33 ++++++------ src/tui/view/component/request.rs | 6 +-- src/tui/view/component/response.rs | 12 ++--- 13 files changed, 111 insertions(+), 112 deletions(-) diff --git a/.rustfmt.toml b/.rustfmt.toml index d069d322..3d87b73b 100644 --- a/.rustfmt.toml +++ b/.rustfmt.toml @@ -1,4 +1,4 @@ max_width = 80 # these two are only available on nightly (F) -merge_imports = true +imports_granularity = "crate" wrap_comments = true diff --git a/src/cli.rs b/src/cli.rs index 63b81cda..242d4ecf 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -134,12 +134,10 @@ impl Subcommand { .truncate(true) .write(true) .open(&output_file) - .with_context(|| { - format!( - "Error opening collection output file \ + .context(format!( + "Error opening collection output file \ {output_file:?}" - ) - })?, + ))?, ), None => Box::new(io::stdout()), }; diff --git a/src/collection.rs b/src/collection.rs index 7bab4e81..e0c9efb5 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -214,7 +214,7 @@ impl RequestCollection { Ok(future .await - .with_context(|| format!("Error loading collection from {path:?}"))? + .context(format!("Error loading collection from {path:?}"))? .with_source(path)) } diff --git a/src/collection/insomnia.rs b/src/collection/insomnia.rs index 398c2739..89004995 100644 --- a/src/collection/insomnia.rs +++ b/src/collection/insomnia.rs @@ -27,17 +27,14 @@ impl RequestCollection<()> { is meant to give you a skeleton for a Slumber collection, and \ nothing more." ); - let file = File::open(insomnia_file).with_context(|| { + let file = File::open(insomnia_file).context( format!("Error opening Insomnia collection file {insomnia_file:?}") - })?; + )?; // The format can be YAML or JSON, so we can just treat it all as YAML let insomnia: Insomnia = - serde_yaml::from_reader(file).with_context(|| { - format!( - "Error deserializing Insomnia collection file\ - {insomnia_file:?}" - ) - })?; + serde_yaml::from_reader(file).context(format!( + "Error deserializing Insomnia collection file {insomnia_file:?}" + ))?; // Convert everything let mut profiles = Vec::new(); diff --git a/src/http.rs b/src/http.rs index 3a42903d..bb9b804a 100644 --- a/src/http.rs +++ b/src/http.rs @@ -134,13 +134,15 @@ impl HttpEngine { let _ = self.repository.insert(&record).await; Ok(record) } - Err(error) => Err(RequestError { - request, - start_time, - end_time, - error, - }) - .traced(), + Err(error) => { + Err(RequestError { + request, + start_time, + end_time, + error, + }) + .traced() + } } }) .await @@ -315,12 +317,12 @@ impl RequestBuilder { // String -> header conversions are fallible, if headers // are invalid Ok::<(HeaderName, HeaderValue), anyhow::Error>(( - header.try_into().with_context(|| { - format!("Error parsing header name `{header}`") - })?, - value.try_into().with_context(|| { + header + .try_into() + .context(format!("Error parsing header name `{header}`"))?, + value.try_into().context( format!("Error parsing value for header `{header}`") - })?, + )?, )) }); Ok(future::try_join_all(iter) @@ -336,9 +338,9 @@ impl RequestBuilder { let iter = query.iter().map(|(k, v)| async move { Ok::<_, anyhow::Error>(( k.clone(), - v.render(template_context).await.context(format!( - "Error rendering query parameter `{k}`" - ))?, + v.render(template_context) + .await + .context(format!("Error rendering query parameter `{k}`"))?, )) }); Ok(future::try_join_all(iter) diff --git a/src/http/parse.rs b/src/http/parse.rs index 41be65a5..ffb8cf25 100644 --- a/src/http/parse.rs +++ b/src/http/parse.rs @@ -138,10 +138,11 @@ mod tests { #[case] body: String, #[case] expected_error: &str, ) { - let headers = match content_type { - Some(content_type) => headers(content_type), - None => HeaderMap::new(), - }; + let headers = + match content_type { + Some(content_type) => headers(content_type), + None => HeaderMap::new(), + }; let response = create!(Response, headers: headers, body: body.into()); assert_err!(parse_body(&response), expected_error); } diff --git a/src/http/repository.rs b/src/http/repository.rs index d563cb87..044abdf9 100644 --- a/src/http/repository.rs +++ b/src/http/repository.rs @@ -47,8 +47,6 @@ pub struct Repository { impl Repository { /// Load the repository database. This will perform first-time setup, so /// this should only be called at the main session entrypoint. - /// - /// Each collection gets its own pub fn load(collection_id: &CollectionId) -> anyhow::Result { let mut connection = Connection::open(Self::path(collection_id))?; // Use WAL for concurrency diff --git a/src/template.rs b/src/template.rs index 7006b74a..235235ff 100644 --- a/src/template.rs +++ b/src/template.rs @@ -166,13 +166,14 @@ mod tests { #[tokio::test] async fn test_override() { let profile = indexmap! {"field1".into() => "field".into()}; - let source = ChainSource::Command( - ["echo", "chain"] - .iter() - .cloned() - .map(String::from) - .collect(), - ); + let source = + ChainSource::Command( + ["echo", "chain"] + .iter() + .cloned() + .map(String::from) + .collect(), + ); let overrides = indexmap! { "field1".into() => "override".into(), "chains.chain1".into() => "override".into(), diff --git a/src/template/parse.rs b/src/template/parse.rs index be3b7c4d..0545614c 100644 --- a/src/template/parse.rs +++ b/src/template/parse.rs @@ -141,10 +141,9 @@ fn take_until_or_eof<'a>( |i| match i.find_substring(tag) { Some(index) => Ok(i.take_split(index)), None if i.input_len() > 0 => Ok(i.take_split(i.input_len())), - None => Err(nom::Err::Error(VerboseError::from_error_kind( - i, - ErrorKind::TakeUntil, - ))), + None => Err(nom::Err::Error( + VerboseError::from_error_kind(i, ErrorKind::TakeUntil) + )), } } diff --git a/src/template/render.rs b/src/template/render.rs index 1c8cd003..de0175a5 100644 --- a/src/template/render.rs +++ b/src/template/render.rs @@ -45,46 +45,47 @@ impl Template { ) -> Vec { // Map over each parsed chunk, and render the keys into strings. The // raw text chunks will be mapped 1:1 - let futures = self.chunks.iter().copied().map(|chunk| async move { - match chunk { - TemplateInputChunk::Raw(span) => TemplateChunk::Raw(span), - TemplateInputChunk::Key(key) => { - // Grab the string corresponding to the span - let key = key.map(|span| self.substring(span)); + let futures = + self.chunks.iter().copied().map(|chunk| async move { + match chunk { + TemplateInputChunk::Raw(span) => TemplateChunk::Raw(span), + TemplateInputChunk::Key(key) => { + // Grab the string corresponding to the span + let key = key.map(|span| self.substring(span)); - // The formatted key should match the source that it was - // parsed from, therefore we can use it to match the - // override key - let raw = key.to_string(); - // If the key is in the overrides, use the given value - // without parsing it - let result = match context.overrides.get(&raw) { - Some(value) => { - trace!( - key = raw, - value, - "Rendered template key from override" - ); - Ok(value.into()) - } - None => { - // Standard case - parse the key and render it - let result = - key.into_source().render(context).await; - if let Ok(value) = &result { + // The formatted key should match the source that it was + // parsed from, therefore we can use it to match the + // override key + let raw = key.to_string(); + // If the key is in the overrides, use the given value + // without parsing it + let result = match context.overrides.get(&raw) { + Some(value) => { trace!( key = raw, value, - "Rendered template key" + "Rendered template key from override" ); + Ok(value.into()) + } + None => { + // Standard case - parse the key and render it + let result = + key.into_source().render(context).await; + if let Ok(value) = &result { + trace!( + key = raw, + value, + "Rendered template key" + ); + } + result } - result - } - }; - result.into() + }; + result.into() + } } - } - }); + }); // Parallelization! join_all(futures).await @@ -270,13 +271,14 @@ impl<'a> ChainTemplateSource<'a> { match command { [] => Err(ChainError::CommandMissing), [program, args @ ..] => { - let output = - Command::new(program).args(args).output().await.map_err( - |error| ChainError::Command { - command: command.to_owned(), - error, - }, - )?; + let output = Command::new(program) + .args(args) + .output() + .await + .map_err(|error| ChainError::Command { + command: command.to_owned(), + error, + })?; info!( ?command, stdout = %String::from_utf8_lossy(&output.stdout), diff --git a/src/tui.rs b/src/tui.rs index f01324da..66685dc5 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -90,19 +90,20 @@ impl Tui { let view = View::new(&collection, messages_tx.clone()); let repository = Repository::load(&collection.id).unwrap(); - let app = Tui { - terminal, - messages_rx, - messages_tx, - http_engine: HttpEngine::new(repository.clone()), - input_engine: InputEngine::new(), - - collection, - should_run: true, - - view, - repository, - }; + let app = + Tui { + terminal, + messages_rx, + messages_tx, + http_engine: HttpEngine::new(repository.clone()), + input_engine: InputEngine::new(), + + collection, + should_run: true, + + view, + repository, + }; // Any error during execution that gets this far is fatal. We expect the // error to already have context attached so we can just unwrap @@ -413,9 +414,9 @@ impl Tui { .profiles .iter() .find(|profile| &profile.id == profile_id) - .ok_or_else(|| { - anyhow!("No profile with ID `{profile_id}`") - })?; + .ok_or_else( + || anyhow!("No profile with ID `{profile_id}`") + )?; profile.data.clone() } None => IndexMap::new(), diff --git a/src/tui/view/component/request.rs b/src/tui/view/component/request.rs index 801368f9..88d5593c 100644 --- a/src/tui/view/component/request.rs +++ b/src/tui/view/component/request.rs @@ -70,9 +70,9 @@ impl Component for RequestPane { action: Some(Action::Fullscreen), .. } => { - context.queue_event(Event::ToggleFullscreen( - FullscreenMode::Request, - )); + context.queue_event( + Event::ToggleFullscreen(FullscreenMode::Request) + ); Update::Consumed } diff --git a/src/tui/view/component/response.rs b/src/tui/view/component/response.rs index 5bd1b9ae..10388d8b 100644 --- a/src/tui/view/component/response.rs +++ b/src/tui/view/component/response.rs @@ -57,9 +57,9 @@ impl Component for ResponsePane { action: Some(Action::Fullscreen), .. } => { - context.queue_event(Event::ToggleFullscreen( - FullscreenMode::Response, - )); + context.queue_event( + Event::ToggleFullscreen(FullscreenMode::Response) + ); Update::Consumed } @@ -200,9 +200,9 @@ impl Component for ResponseContent { action: Some(Action::Fullscreen), .. } => { - context.queue_event(Event::ToggleFullscreen( - FullscreenMode::Response, - )); + context.queue_event( + Event::ToggleFullscreen(FullscreenMode::Response) + ); Update::Consumed }