-
Notifications
You must be signed in to change notification settings - Fork 188
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
Sozo: split hash command into 'hash compute' and 'hash find' #2892
base: main
Are you sure you want to change the base?
Conversation
WalkthroughOhayo, sensei! The pull request introduces a significant refactoring of the hash command in the Sozo CLI. The changes transform the Changes
Sequence DiagramsequenceDiagram
participant User
participant HashCommand
participant Config
User->>HashCommand: Execute with command
alt Compute Hash
HashCommand->>HashCommand: compute(input)
HashCommand-->>User: Return hash result
else Find Hash
HashCommand->>Config: Retrieve namespaces/resources
HashCommand->>HashCommand: find(hash, namespaces, resources)
HashCommand-->>User: Return matching results
end
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (4)
bin/sozo/src/commands/hash.rs (4)
32-32
: Ohayo! Add description for Find command, sensei!The Find command is missing its about string which should describe its functionality.
- #[command(about = "")] + #[command(about = "Search for a hash among namespaces and resource names/tags")]
65-68
: Enhance error message clarity, sensei!The error message could be more descriptive about the 32-character limit for Starknet selectors.
- return Err(anyhow::anyhow!("Input is too long for a starknet selector")); + return Err(anyhow::anyhow!( + "Input exceeds the 32-character limit for a Starknet selector" + ));
98-104
: Consider performance optimization and error handling improvements, sensei!The find method could benefit from:
- Early validation of input parameters
- Capacity hints for collections to avoid reallocations
pub fn find( &self, config: &Config, hash: &String, namespaces: Option<Vec<String>>, resources: Option<Vec<String>>, ) -> Result<()> { + // Early validation of input parameters + if namespaces.as_ref().map_or(false, |n| n.is_empty()) + && resources.as_ref().map_or(false, |r| r.is_empty()) + { + return Err(anyhow::anyhow!( + "At least one namespace or resource must be provided" + )); + }
112-147
: Optimize collection initialization, sensei!Consider providing capacity hints for HashSet to avoid reallocations.
- let mut ns_from_config = HashSet::new(); + let mut ns_from_config = HashSet::with_capacity(10); // Adjust capacity based on expected size
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
bin/sozo/src/commands/hash.rs
(3 hunks)bin/sozo/src/commands/mod.rs
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: fmt
🔇 Additional comments (2)
bin/sozo/src/commands/mod.rs (1)
115-115
: Ohayo! LGTM, sensei!The modification to pass the config to the hash command's run method is necessary to support the new find subcommand's functionality.
bin/sozo/src/commands/hash.rs (1)
Line range hint
223-281
: Add tests for find functionality, sensei!While the compute functionality is well-tested, there are no tests for the new find functionality. Consider adding tests for:
- Finding namespace matches
- Finding resource name matches
- Finding tag matches
- Invalid hash handling
- Empty configuration scenarios
Would you like me to help generate these test cases?
80a9963
to
9bbff84
Compare
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.
Actionable comments posted: 0
🔭 Outside diff range comments (1)
bin/sozo/src/commands/hash.rs (1)
Line range hint
226-283
: Let's enhance test coverage, sensei!While the existing tests are good, we're missing tests for the new
find
command functionality.Consider adding these test cases:
#[test] fn test_hash_find_namespace() { let config = Config::default(); let hash = "0x123"; // Use an actual computed namespace hash let args = HashArgs { command: HashCommand::Find { hash: hash.to_string(), namespaces: Some(vec!["test_namespace".to_string()]), resources: None, } }; let result = args.find(&config, &hash.to_string(), None, None); assert!(result.is_ok()); } #[test] fn test_hash_find_with_config() { let config = Config::default(); let args = HashArgs { command: HashCommand::Find { hash: "0x123".to_string(), namespaces: None, resources: None, } }; let result = args.find(&config, &"0x123".to_string(), None, None); assert!(result.is_ok()); }
🧹 Nitpick comments (3)
bin/sozo/src/commands/hash.rs (3)
23-49
: Well-structured command interface, sensei!The HashCommand enum is well-designed with clear documentation. Consider adding examples in the help messages to make it even more user-friendly.
#[command(about = "Compute the hash of the provided input.")] Compute { - #[arg(help = "Input to hash. It can be a comma separated list of inputs or a single \ - input. The single input can be a dojo tag or a felt.")] + #[arg(help = "Input to hash. It can be a comma separated list of inputs or a single \ + input. The single input can be a dojo tag or a felt. \ + Examples: 'dojo_examples-actions', '0x1,0x2,0x3'")]
Line range hint
52-97
: Consider enhancing error handling and readability, sensei!The compute method handles multiple input types well, but could benefit from:
- More descriptive error messages
- Extraction of the hash computation logic into separate functions
pub fn compute(&self, input: &str) -> Result<()> { if input.is_empty() { - return Err(anyhow::anyhow!("Input is empty")); + return Err(anyhow::anyhow!("Input cannot be empty. Please provide a value to hash.")); } + + self.compute_impl(input) + } + + fn compute_impl(&self, input: &str) -> Result<()> { if input.contains('-') { return self.compute_tag(input); }
100-209
: Ohayo! Let's improve the find method's organization, sensei!The find method is comprehensive but could be more maintainable if split into smaller functions.
Consider extracting the namespace and resource collection logic into separate functions:
+ fn collect_namespaces_from_config(&self, profile_config: &ProfileConfig, manifest: &Option<Manifest>) -> Vec<String> { + let mut ns_from_config = HashSet::new(); + ns_from_config.insert(profile_config.namespace.default); + // ... rest of namespace collection logic + Vec::from_iter(ns_from_config) + } + + fn collect_resources_from_config(&self, profile_config: &ProfileConfig, manifest: &Option<Manifest>) -> Vec<String> { + let mut res_from_config = HashSet::new(); + // ... rest of resource collection logic + Vec::from_iter(res_from_config) + }Also, consider adding error handling for cases where no matches are found:
// --- find the hash --- + let mut matches_found = false; // could be a namespace hash for ns in &namespaces { if hash == compute_bytearray_hash(ns) { println!("Namespace found: {ns}"); + matches_found = true; } } + + if !matches_found { + println!("No matches found for hash: {:#066x}", hash); + }
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
bin/sozo/src/commands/hash.rs
(3 hunks)bin/sozo/src/commands/mod.rs
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- bin/sozo/src/commands/mod.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
- GitHub Check: fmt
🔇 Additional comments (2)
bin/sozo/src/commands/hash.rs (2)
1-20
: Ohayo sensei! Clean command structure implementation!The command structure follows clap's best practices for subcommands, and the imports are well-organized.
212-221
: Clean command dispatch implementation!The run method is well-structured with proper tracing.
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2892 +/- ##
==========================================
- Coverage 55.77% 55.71% -0.06%
==========================================
Files 446 449 +3
Lines 57794 57772 -22
==========================================
- Hits 32234 32188 -46
- Misses 25560 25584 +24 ☔ View full report in Codecov by Sentry. |
Description
This PR splits the existing
sozo hash
command into 2 subcommands:sozo hash compute
which does the same thing than the actualsozo hash
command,sozo hash find
which:--namespaces
and--resources
options,Related issue
#2850
Tests
Added to documentation?
Checklist
scripts/prettier.sh
,scripts/rust_fmt.sh
,scripts/cairo_fmt.sh
)scripts/clippy.sh
,scripts/docs.sh
)Summary by CodeRabbit
New Features
Refactor