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

Refactor CLI code #286

Merged
merged 7 commits into from
Aug 7, 2024
Merged

Conversation

cmleinz
Copy link
Contributor

@cmleinz cmleinz commented Aug 3, 2024

Summary

This is a draft PR for addressing #240. So far I've made one change to the CLI main function. I'm looking for feedback as to if this is a good path forward for the refactor, if it is I can proceed with making changes to the rest of the actions in a similar way.

Architectural Changes

Adjust the CLI code to be more generic and reduce duplication

@@ -1,6 +1,6 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use clap::{Args as ArgsT, Parser, Subcommand};
Copy link
Member

Choose a reason for hiding this comment

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

I typically prefer to rename the repo's structs to not conflict with imports, instead of the other way around. So in this case, you could rename the pub struct Args on line 8 to something else (CliArgs?), and still import clap's Args as is.

@@ -66,3 +55,17 @@ pub enum Actions {
id: i32,
},
}

#[derive(Debug, PartialEq, Eq, PartialOrd, ArgsT)]
pub(crate) struct TruncateById {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add an example of how the command changes now that TruncateById derives clap's Args?

@@ -50,7 +53,9 @@ pub enum CliErrors {

fn main() -> Result<(), CliErrors> {
if var(LOG_VAR).is_err() {
set_var(LOG_VAR, "INFO");
unsafe {
set_var(LOG_VAR, "INFO");
Copy link
Member

Choose a reason for hiding this comment

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

Is set_var unsafe now?

Copy link
Contributor Author

@cmleinz cmleinz Aug 4, 2024

Choose a reason for hiding this comment

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

Yes it's been marked deprecated safe, I think this will officially apply in rust 2024?

anise-cli/src/main.rs Show resolved Hide resolved
@@ -45,6 +45,7 @@ pub trait NAIFRecord: AsBytes + FromBytes + Sized + Default + Debug {

pub trait NAIFSummaryRecord: NAIFRecord + Copy {
fn start_index(&self) -> usize;
fn data_type_i(&self) -> i32;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of adding the data_type_i, I recommend making the data_type() function of SPKSummaryRecord and BPCSummaryRecord generic.

The way you could do this is by adding a new type parameter to the trait as follows:

us core::error::Error;

pub trait NAIFSummaryRecord: NAIFRecord + Copy {
    type ErrorType: Error;
    /// (...)
    fn data_type(&self) -> Result<DafDataType, Self::ErrorType>;
}

Then in the BPCSummaryType, move the data_type function from this struct's own implementation to the struct's implementation of the NAIFSummaryRecord trait.

So around line 160 of naif/spk/summary.rs, you'd need to add the type Error definition.

impl NAIFSummaryRecord for SPKSummaryRecord {
    const NAME: &'static str = "SPKSummaryRecord";
    type ErrorType = EphemerisError;
    // (...)
   fn data_type(&self) -> Result<DafDataType, EphemerisError> {
        DafDataType::try_from(self.data_type_i).map_err(|source| EphemerisError::SPK {
            action: "converting data type from i32",
            source,
        })
    }

Repeat that for the BPC Summary structure.

Then in the CLI code, you can just use the data_type() function of the NAIFSummary directly, and remove the need to export the integer representing the data_type_i or even rebuilding the data type enum from that integer in the code.

Let me know if you'd like more clarifications here.

@ChristopherRabotin
Copy link
Member

Thanks for your work, @cmleinz ! This is a great start, I've just made a recommendation on the change to NAIFSummary which I think will make it more generic.

@cmleinz
Copy link
Contributor Author

cmleinz commented Aug 6, 2024

I addressed the comments thus far, still haven't made the rest of the changes though. Let me know what you think! As for your comment on how the CLI will change with Args, I believe it will remain the same. I compared the outputs of the help commands for trunc-daf-by-id between this branch and master and couldn't find any differences.

Additionally for another CLI I maintain I derive Args and I'm pretty sure everything automagically works the same, I'll look a little deeper though to be sure


fn truncate_daf_by_id<R>(
args::TruncateById {
input,
Copy link
Member

Choose a reason for hiding this comment

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

I've not seen this syntax before. Does this unpack the struct on call? I read about this idea, but I didn't know that it was stabilized.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Essentially yes, it's pattern I like for this sort of thing where the function consumes it's argument and the struct is defined primarily as a helper struct, all of whose fields you intend to use within the function's body. It's pretty common in the axum crate!

}
);

let segment = fmt.nth_data::<Type2ChebyshevSet>(idx).unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

These unwrap calls should probably bubble up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep sounds good I can add to the error list

@cmleinz cmleinz changed the title DRAFT Refactor CLI code Refactor CLI code Aug 7, 2024
@ChristopherRabotin
Copy link
Member

Great work! I especially like the changes to file2heap and NAIFPrettyPrint. which I hadn't considered. The failing tests are because there is an ephem that's behind a Github secret that only the maintainers can use in CI.

@ChristopherRabotin ChristopherRabotin merged commit 3eab6eb into nyx-space:master Aug 7, 2024
14 of 20 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.

2 participants