Hone logo
Hone
Problems

Building a Simple Module System in Rust

This challenge asks you to implement a rudimentary module system in Rust, mimicking some core functionalities of Rust's built-in module system. Understanding how modules work is fundamental to organizing larger Rust projects, and this exercise will provide hands-on experience with the underlying concepts of namespaces, visibility, and organization. You'll be creating a system that allows you to define and use modules and their contents.

Problem Description

You are tasked with creating a simplified module system. This system should allow you to define modules, declare items (functions and structs) within those modules, and then access those items from other modules or the main program. The system should enforce visibility rules: items declared as pub should be accessible from outside the module, while those without pub should be private to the module.

What needs to be achieved:

  1. Module Definition: A mechanism to define a module. This will be represented as a string (module name).
  2. Item Declaration: A way to declare items (functions and structs) within a module. Each item will have a name, type (function or struct), and visibility (public or private).
  3. Item Storage: A data structure to store the declared items for each module.
  4. Item Access: A mechanism to access items from a module, respecting visibility rules. You should be able to retrieve an item by its name and the module it belongs to.
  5. Error Handling: Handle cases where a module doesn't exist or an item is not found within a module.

Key Requirements:

  • The system should support both functions and structs.
  • Visibility should be strictly enforced (public items are accessible, private items are not).
  • The system should be extensible to support more item types in the future (though you don't need to implement this extensibility in this challenge).
  • The module system should be implemented using structs and enums to represent the module structure.

Expected Behavior:

The system should allow you to:

  • Create a new module.
  • Add a public function to a module.
  • Add a private struct to a module.
  • Access a public function from another module or the main program.
  • Attempt to access a private struct from outside its module and receive an appropriate error.
  • Attempt to access a non-existent item and receive an appropriate error.

Edge Cases to Consider:

  • Module names are not unique. (For simplicity, assume module names are unique in this challenge.)
  • Item names are not unique within a module. (For simplicity, assume item names are unique within a module.)
  • Attempting to add an item to a non-existent module.
  • Attempting to access an item from a module that doesn't exist.

Examples

Example 1:

Input:
Module Definitions:
  - Module: "math"
    - Item: "add" (Function, Public)
    - Item: "private_struct" (Struct, Private)

  - Module: "main"

Item Access:
  - Access "math::add"
Output:
  - Success: Function found.

Explanation: The "add" function is public and accessible from the "main" module (or the main program).

Example 2:

Input:
Module Definitions:
  - Module: "utils"
    - Item: "helper_function" (Function, Private)

Item Access:
  - Access "utils::helper_function"
Output:
  - Error: Item not accessible (private).

Explanation: The "helper_function" is private and cannot be accessed from outside the "utils" module.

Example 3:

Input:
Module Definitions:
  - Module: "graphics"
    - Item: "draw_circle" (Function, Public)

Item Access:
  - Access "physics::draw_circle"
Output:
  - Error: Module not found.

Explanation: The "physics" module does not exist, so accessing "physics::draw_circle" results in a module not found error.

Constraints

  • The module system should be implemented using Rust's standard library only. No external crates are allowed.
  • Error handling should be done using Result and custom error types.
  • The code should be well-structured and readable.
  • The solution should compile and run without errors.
  • The solution should handle at least the three example cases correctly.

Notes

  • Consider using enums to represent the different item types (Function, Struct).
  • Think about how to represent the module structure in a data structure. A HashMap might be useful.
  • Focus on the core functionality of module definition, item declaration, and item access. You don't need to implement advanced features like re-exporting or module paths.
  • Error messages should be informative and helpful for debugging.
  • This is a simplified module system; it doesn't need to be a complete replacement for Rust's built-in module system. The goal is to understand the underlying principles.
Loading editor...
rust