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

Introduce TypeName Crate with Standardized Naming Conventions #177

Open
20 tasks
evomimic opened this issue Dec 7, 2024 · 0 comments
Open
20 tasks

Introduce TypeName Crate with Standardized Naming Conventions #177

evomimic opened this issue Dec 7, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@evomimic
Copy link
Owner

evomimic commented Dec 7, 2024

Introduce a type_names crate to enhance consistency, readability, and maintainability of type names and implementing specialized naming conventions for TypeName, PropertyName, and RelationshipName. These changes ensure that each naming type enforces its own standard convention while centralizing logic for easier extensibility and reuse.

Dependencies

Crate Structure:

type_names crate

  • Create a type_names crate to encapsulate related types and logic. Include the following files:
    • type_name.rs: Core TypeName implementation.
    • property_name.rs: Specialized TypeName for property identifiers.
    • relationship_name.rs: Specialized TypeName for relationship identifiers.
    • naming_convention.rs: Enum and logic for naming conventions.

TypeName

TypeName is a specialized newtype wrapper around MapString that used as the key for all MAP Types.

    ```
    #[hdk_entry_helper]
    #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct TypeName(pub MapString);
    ```

Implement the following traits:

  • Deref-- so TypeName can act like a MapString (avoiding the need to use .0)
  • AsRef -- to allow seamless borrowing of the inner MapString
  • Borrow -- For usage in collections like HashMap or BTreeMap
  • From -- convenience for creating a TypeName from a MapString:

Implement a to_string method that returns a clone of the inner MapString

    - [ ] `pub fn to_string(&self) -> MapString`

Naming Conventions:

Leverage the Inflector crate to detect, convert, and assert string naming conventions.

  • Define a NamingConvention enum
    pub enum NamingConvention { ClassCase, SnakeCase, ScreamingSnakeCase, }

  • Implement methods on NamingConvention for converting, asserting, and detecting cases
    - [ ] pub fn to_case(&self, input: &str) -> String // converts to this NamingConvention
    - [ ] pub fn is_case(&self, input: &str) -> bool // asserts the supplied str conforms to this NamingConvention
    - [ ] pub fn detect_case(input: &str) -> NamingConvention // Detects the naming convention of a given string. It uses precedence rules to resolve ambiguities.

Specialized TypeNames:

The following are all TypeNames. They are introduced to enhance type safety and to allow different naming conventions for different types

  • PropertyName -- specializes TypeName and enforces snake_case naming convention
use super::type_name::TypeName;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PropertyName(TypeName);

impl PropertyName {
    /// Constructor enforcing snake_case naming convention.
    pub fn new(raw: &str) -> Self {
        PropertyName(TypeName::with_convention(raw, NamingConvention::SnakeCase))
    }

    /// downcast method for treating a PropertyName as a TypeName
    pub fn as_typename(&self) -> &TypeName {
        &self.0
    }
}

impl fmt::Display for PropertyName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

  • RelationshipName -- Specializes TypeName for RelationshipName with SCREAMING_SNAKE_CASE convention
use super::type_name::TypeName;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationshipName(TypeName);

impl RelationshipName {
    /// Constructor enforcing SCREAMING_SNAKE_CASE naming convention.
    pub fn new(raw: &str) -> Self {
        RelationshipName(TypeName::with_convention(raw, NamingConvention::ScreamingSnakeCase))
    }

    /// downcast method for treating a RelationshipName as a TypeName
    pub fn as_typename(&self) -> &TypeName {
        &self.0
    }
}

impl fmt::Display for RelationshipName {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

•	TypeName:
•	Default to ClassCase convention.
•	Provide methods for creating TypeName instances with different conventions.
•	

Public API

  • Export all relevant types (TypeName, PropertyName, RelationshipName, NamingConvention) through the type_names/mod.rs file for external use.

Handle Ripple Effects

  • Adjust the remainder of the MAP code to work with these revisions to the MAP Type System

Testing

  • Unit Tests should confirm the behavior of the NamingConvention methods
  • A new "naming_conventions_test" test case should exercise both valid and invalid PropertyName, RelationshipName and TypeName names

Definition of Done

  • Regression tests: All existing test cases pass
  • The naming convention unit and sweetest case all pass
@evomimic evomimic added the enhancement New feature or request label Dec 7, 2024
@evomimic evomimic self-assigned this Dec 7, 2024
@evomimic evomimic changed the title Introduce TypeName Module with Standardized Naming Conventions Introduce TypeName Crate with Standardized Naming Conventions Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant