Skip to content
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

Libify tlparse, first steps #3

Merged
merged 1 commit into from
Mar 25, 2024
Merged

Libify tlparse, first steps #3

merged 1 commit into from
Mar 25, 2024

Conversation

jamesjwu
Copy link
Collaborator

  • Ran clippy on the files for a quick lint
  • Decoupled some of the CLI from the library. The only part that's still a bit tangled up is the spinner, but I'll deal with that later
  • Defined a lib.rs, deleted the Cargo.lock file that's generated and added to gitignore. Building the binary (with cargo run or cargo build --bin tlparse) still results in an executable binary, but the library is now the main crate.

TODO: need to test what happens with pip and cargo install

@jamesjwu jamesjwu requested a review from ezyang March 22, 2024 13:48
let file = File::open(path)?;
let metadata = file.metadata()?;
let file_size = metadata.len();

// TODO: abstract out this spinner to not be part of the library
// Instead, add a callback trait for CLIs to implement
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@aorenste what do you think the best way to handle this is? The spinner gets updated on every loop of the log parse, but I don't want this in the library. It's also called on various errors with multi.suspend().

Maybe I could implement a trait like StatLogger and put the spinner in the callback in the CLI:


trait StatLogger {
    fn init(&self) -> ();
    fn log_stats(&mut self, bytes_read: u64, stat: &Stats) -> ();
    // how to do **args in rust?
    fn error(&self, e : Exception, **args) -> (); 
}

pub fn parse_path_with_logger(path: &PathBuf, config: ParseConfig, logger: impl StatLogger) ...

// uses default no-op logger
pub fn parse_path(path: &PathBuf, config: ParseConfig) ...

And then call it later in the code when updating and exiting? Or is there a more rust-like way to do it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes - I think a trait is the right approach.

I would make sure to either make a trivial null implementation that callers can instantiate or have the call take Option<> so that callers don't have to build their own if they don't care.

Also you should take &mut impl StatLogger so it's borrowed instead of owned.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh - I missed that you had two different APIs for it (one with and one without). I would just have one API that took Option<&mut impl StatLogger>.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'll do this in a separate PR: there's some complexities with the fact that you can't actually condition statically on impl StatLogger and also the spinner's side effects need to happen in the function initially. Going to land this change first

@jamesjwu jamesjwu marked this pull request as draft March 22, 2024 14:30
@jamesjwu
Copy link
Collaborator Author

Talked with @aorenste offline, gonna rename a few things and test out cargo a bit more first


[[bin]]
name = "tlparse"
path = "src/cli.rs"
Copy link
Owner

Choose a reason for hiding this comment

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

don't forget to git add this

@jamesjwu jamesjwu marked this pull request as ready for review March 25, 2024 05:01
@jamesjwu jamesjwu requested review from ezyang and aorenste March 25, 2024 05:01
@jamesjwu
Copy link
Collaborator Author

Based on https://stackoverflow.com/questions/62861623/should-cargo-lock-be-committed-when-the-crate-is-both-a-rust-library-and-an-exec, I think we can keep the Cargo.lock as long as there's a lib.rs file for us to internally import. Crates that depend on the library will ignore the lock.

@jamesjwu jamesjwu merged commit 661eccf into main Mar 25, 2024
12 checks passed
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.

3 participants