Skip to content

refactor: Use Origin for Snippet::origin #221

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

Closed
wants to merge 1 commit into from

Conversation

Muscraft
Copy link
Member

This partially addresses #208 by making Snippet::origin use Origin internally. This makes the distinction a little clearer: you should use Snippet::origin if you are using a Snippet and Origin on its own if you need the file details.

Copy link
Contributor

Choose a reason for hiding this comment

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

you should use Snippet::origin if you are using a Snippet and Origin on its own if you need the file details.

maybe that should be put in some docs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let me know what you think of the docs I added

Copy link
Contributor

Choose a reason for hiding this comment

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

But when would you use Snippet::origin(Origin')?

Comment on lines +210 to 217
pub fn origin(mut self, origin: impl Into<Origin<'a>>) -> Self {
self.origin = Some(origin.into());
self
Copy link
Contributor

Choose a reason for hiding this comment

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

Not too sure how this makes it clearer.

Also, wht does uit mean to specify line, char_column, or primary on an Origin in a Snippet?

Copy link
Member Author

Choose a reason for hiding this comment

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

is_primary is respected unless the Origin is the "primary origin" for a Group (I think).

line is respected if it is set.

char_column is respected if line is set; if line is not set, it is overridden.

src/snippet.rs Outdated
Comment on lines 387 to 400
impl<'a> From<&'a str> for Origin<'a> {
fn from(origin: &'a str) -> Self {
Self::new(origin)
}
}

impl<'a> From<&'a String> for Origin<'a> {
fn from(origin: &'a String) -> Self {
Self::new(origin)
}
}

impl<'a> Origin<'a> {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I tend to put trait impls after the regular functions as they tend to be more of the focus for a type

src/snippet.rs Outdated
Comment on lines 203 to 207
/// If just a location is passed (i.e. a string) in the line and column
/// numbers are automatically inferred. If you want to explicitly set the
/// line and column you can create an [`Origin`] and pass it in.
///
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// If just a location is passed (i.e. a string) in the line and column
/// numbers are automatically inferred. If you want to explicitly set the
/// line and column you can create an [`Origin`] and pass it in.
///
/// If only a location is provided (i.e. a `String`) then the rest of the [`Origin`] is inferred (e.g. line and column
/// numbers). To adjust line numbers, consider using [`Snippet::line_start`] instead as it will also adjust line numbers for the [`Snippet::source`]

Comment on lines +388 to +391
/// Note: `line` is always respected if set, but `char_column` is only
/// respected if `line` has been set. `primary` is respected unless the origin
/// is the first one in a [`Group`], in which case it is ignored.
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems like good context to put on Origin::char_column

/// <div class="warning">
///
/// This will only be respected if [`Origin::line]` is also set
///
/// </div>

src/snippet.rs Outdated
Comment on lines 384 to 386
/// This should be used if you want to display the file details by
/// itself, or you want to set the line number and column explicitly.
/// Otherwise, it is better to use [`Snippet::origin`].
Copy link
Contributor

Choose a reason for hiding this comment

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

This should be clarified that you mean that this is a parallel item to Snippet (can't remember all of the terms and not digging in to provide a suggestion atm)

Copy link
Contributor

Choose a reason for hiding this comment

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

I was referring to [Element::Origin]

@Muscraft Muscraft force-pushed the use-origin branch 3 times, most recently from 30d349a to 960ef89 Compare June 25, 2025 23:22
Comment on lines +428 to +434
///
/// <div class="warning">
///
/// When [`Origin`] is passed into [`Snippet::origin`], `char_column` is
/// only be respected if [`Origin::line`] is also set.
///
/// </div>
Copy link
Contributor

Choose a reason for hiding this comment

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

We should keep this comment

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait,. isn't this always applicable, not just Snippet::origin?

Comment on lines +383 to +387
/// The location of the [`Snippet`] (e.g. a path).
///
/// This should be used if you want to set the line number and column
/// explicitly for a [`Snippet`], or if you need to render a location without
/// an accompanying [`Snippet`].
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// The location of the [`Snippet`] (e.g. a path).
///
/// This should be used if you want to set the line number and column
/// explicitly for a [`Snippet`], or if you need to render a location without
/// an accompanying [`Snippet`].
/// The referenced location (e.g. a path)
///
/// If you have source available, see instead [`Snippet`]

Comment on lines 164 to 166
/// A source view [`Element`] in a [`Group`]
#[derive(Clone, Debug)]
pub struct Snippet<'a, T> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// A source view [`Element`] in a [`Group`]
#[derive(Clone, Debug)]
pub struct Snippet<'a, T> {
/// A source view [`Element`] in a [`Group`]
///
/// If you do not have [source][Snippet::source] available, see instead [`Origin`]
#[derive(Clone, Debug)]
pub struct Snippet<'a, T> {

@Muscraft
Copy link
Member Author

Closing in favor of #227, as it does a better job of addressing #208.

@Muscraft Muscraft closed this Jun 26, 2025
@Muscraft Muscraft deleted the use-origin branch June 26, 2025 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants