-
Notifications
You must be signed in to change notification settings - Fork 456
diff_util: Move diff renderer format length check to constructor #6253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -287,23 +287,28 @@ pub enum DiffRenderError { | |
/// Configuration and environment to render textual diff. | ||
pub struct DiffRenderer<'a> { | ||
repo: &'a dyn Repo, | ||
formats: Vec<DiffFormat>, | ||
path_converter: &'a RepoPathUiConverter, | ||
conflict_marker_style: ConflictMarkerStyle, | ||
formats: Vec<DiffFormat>, | ||
} | ||
|
||
impl<'a> DiffRenderer<'a> { | ||
pub fn new( | ||
/// Create a new textual diff renderer if the formats vector is non-empty. | ||
pub fn new_if_non_empty( | ||
repo: &'a dyn Repo, | ||
formats: Vec<DiffFormat>, | ||
path_converter: &'a RepoPathUiConverter, | ||
conflict_marker_style: ConflictMarkerStyle, | ||
formats: Vec<DiffFormat>, | ||
) -> Self { | ||
DiffRenderer { | ||
repo, | ||
path_converter, | ||
conflict_marker_style, | ||
formats, | ||
) -> Option<Self> { | ||
if formats.is_empty() { | ||
None | ||
} else { | ||
Comment on lines
+303
to
+305
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would move this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that it's part of the type's constructor. I think this should stay with the type, because it really is nonsensical to have a diff renderer that outputs nothing. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not wrong to construct a diff function that does nothing. I think this could be a contract at cli_util layer, but as I said, I assumed it would be easier to test the condition by caller. FWIW, we'll need to reorganize the current diff format configuration as "short" and "long" formats pair . (see |
||
Some(Self { | ||
repo, | ||
formats, | ||
path_converter, | ||
conflict_marker_style, | ||
}) | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps, it's simpler to handle
.is_empty()
here instead of addingmaybe_*()
and wrapping the result?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
diff_renderer
only takes a single format, not a vector of formats.You should try rewriting this if you want to see the pitfalls. I don't think a simpler solution really exists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean you can revert the change to accept multiple formats.