Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
The desired formatting changed at somepoint. Maybe during the rust downgrade?
  • Loading branch information
LucasPickering committed Nov 21, 2023
1 parent 1fd1ddb commit 4b7b08b
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 112 deletions.
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
max_width = 80
# these two are only available on nightly (F)
merge_imports = true
imports_granularity = "crate"
wrap_comments = true
8 changes: 3 additions & 5 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
};
Expand Down
2 changes: 1 addition & 1 deletion src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ impl RequestCollection<PathBuf> {

Ok(future
.await
.with_context(|| format!("Error loading collection from {path:?}"))?
.context(format!("Error loading collection from {path:?}"))?
.with_source(path))
}

Expand Down
13 changes: 5 additions & 8 deletions src/collection/insomnia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
32 changes: 17 additions & 15 deletions src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions src/http/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 0 additions & 2 deletions src/http/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
let mut connection = Connection::open(Self::path(collection_id))?;
// Use WAL for concurrency
Expand Down
15 changes: 8 additions & 7 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
7 changes: 3 additions & 4 deletions src/template/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)),
}
}

Expand Down
82 changes: 42 additions & 40 deletions src/template/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,46 +45,47 @@ impl Template {
) -> Vec<TemplateChunk> {
// 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
Expand Down Expand Up @@ -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),
Expand Down
33 changes: 17 additions & 16 deletions src/tui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions src/tui/view/component/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions src/tui/view/component/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 4b7b08b

Please sign in to comment.