-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FED-47: Finish porting
compute_best_plan_from_closed_branches
(apol…
…lographql/federation-next#277) - refactor: moved `query_plan::display::State` to `crate::indented_display` module since it's quite reusable across the board.
- Loading branch information
Showing
9 changed files
with
381 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::fmt; | ||
|
||
pub(crate) struct State<'fmt, 'fmt2> { | ||
indent_level: usize, | ||
output: &'fmt mut fmt::Formatter<'fmt2>, | ||
} | ||
|
||
impl<'a, 'b> State<'a, 'b> { | ||
pub(crate) fn new(output: &'a mut fmt::Formatter<'b>) -> State<'a, 'b> { | ||
Self { | ||
indent_level: 0, | ||
output, | ||
} | ||
} | ||
|
||
pub(crate) fn indent_level(&self) -> usize { | ||
self.indent_level | ||
} | ||
|
||
pub(crate) fn write<T: fmt::Display>(&mut self, value: T) -> fmt::Result { | ||
write!(self.output, "{}", value) | ||
} | ||
|
||
pub(crate) fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { | ||
self.output.write_fmt(args) | ||
} | ||
|
||
pub(crate) fn new_line(&mut self) -> fmt::Result { | ||
self.write("\n")?; | ||
for _ in 0..self.indent_level { | ||
self.write(" ")? | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub(crate) fn indent_no_new_line(&mut self) { | ||
self.indent_level += 1; | ||
} | ||
|
||
pub(crate) fn indent(&mut self) -> fmt::Result { | ||
self.indent_no_new_line(); | ||
self.new_line() | ||
} | ||
|
||
pub(crate) fn dedent(&mut self) -> fmt::Result { | ||
self.indent_level -= 1; | ||
self.new_line() | ||
} | ||
} | ||
|
||
pub(crate) fn write_indented_lines<T>( | ||
state: &mut State<'_, '_>, | ||
values: &[T], | ||
mut write_line: impl FnMut(&mut State<'_, '_>, &T) -> fmt::Result, | ||
) -> fmt::Result { | ||
if !values.is_empty() { | ||
state.indent_no_new_line(); | ||
for value in values { | ||
state.new_line()?; | ||
write_line(state, value)?; | ||
} | ||
state.dedent()?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.