-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Refactor CLI code #286
Conversation
anise-cli/src/args.rs
Outdated
@@ -1,6 +1,6 @@ | |||
use std::path::PathBuf; | |||
|
|||
use clap::{Parser, Subcommand}; | |||
use clap::{Args as ArgsT, Parser, Subcommand}; |
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 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 { |
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.
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"); |
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.
Is set_var
unsafe now?
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.
Yes it's been marked deprecated safe, I think this will officially apply in rust 2024?
anise/src/naif/daf/mod.rs
Outdated
@@ -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; |
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.
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.
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. |
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, |
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'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.
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.
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!
anise-cli/src/main.rs
Outdated
} | ||
); | ||
|
||
let segment = fmt.nth_data::<Type2ChebyshevSet>(idx).unwrap(); |
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.
These unwrap calls should probably bubble up.
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.
Yep sounds good I can add to the error list
Great work! I especially like the changes to |
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