Skip to content

Conversation

@MichaReiser
Copy link
Contributor

@MichaReiser MichaReiser commented May 12, 2023

This PR moves the format and cformat modules from RustPython into the new format crate. I'm not sure if this is the right crate to add. Please let me know if I should move it to another crate and/or add it behind a feature flag.

The format implementation has an optimized path for BorrowedStr. I kept that path by introducing a new CharLen trait that BorrowedStr can implement in RustPython.

The goal of this is that format and cformat are the last two modules from RustPython that ruff depends on. Moving them into this repository would allow us to cut that dependency.

Comment on lines 659 to 685
pub trait CharLen {
/// Returns the number of characters in the text
fn char_len(&self) -> usize;
}

struct AsciiStr<'a> {
inner: &'a str,
}

impl<'a> AsciiStr<'a> {
fn new(inner: &'a str) -> Self {
Self { inner }
}
}

impl CharLen for AsciiStr<'_> {
fn char_len(&self) -> usize {
self.inner.len()
}
}

impl Deref for AsciiStr<'_> {
type Target = str;
fn deref(&self) -> &Self::Target {
self.inner
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is new

@MichaReiser MichaReiser force-pushed the add-format-and-cformat branch from b26bb1e to d967b34 Compare May 12, 2023 08:44
@youknowone
Copy link
Member

I feel a bit awkward to put format functions to parser directory. But that will be fine if it helps to maintaining Ruff in practice.
rustpython-common has too many dependencies to handling platform/threading stuff.
To make future contributors not being confused, could you add comments to format.rs and cformat.rs that it doesn't look like parser component but added to support common parser user scenario?

@MichaReiser
Copy link
Contributor Author

I feel a bit awkward to put format functions to parser directory. But that will be fine if it helps to maintaining Ruff in practice. rustpython-common has too many dependencies to handling platform/threading stuff. To make future contributors not being confused, could you add comments to format.rs and cformat.rs that it doesn't look like parser component but added to support common parser user scenario?

I understand. Would it help if we move the logic into a new rustpython-format crate?

@MichaReiser MichaReiser marked this pull request as ready for review May 12, 2023 08:58
@youknowone
Copy link
Member

Either way will be good to me.

@MichaReiser
Copy link
Contributor Author

I added a new crate and updated the repository URLs.

@youknowone youknowone merged commit a983f43 into RustPython:main May 12, 2023
@youknowone
Copy link
Member

Thank you!

MichaReiser

This comment was marked as resolved.

@MichaReiser MichaReiser deleted the add-format-and-cformat branch May 12, 2023 09:28
charliermarsh referenced this pull request in astral-sh/RustPython-Parser May 15, 2023
* Add `format` and `cformat` modules from `RustPython` * Introduce `rustpython-format` crate * Remove unused dependencies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants