ai-descriptor-trait

Crates.ioai-descriptor-trait
lib.rsai-descriptor-trait
version0.3.0
sourcesrc
created_at2024-11-22 01:14:32.461577
updated_at2024-11-22 02:24:41.761267
descriptionProvides an interface for the proc macro we can use to annotate enums for facilitating itemization and description using an AI model
homepagehttps://github.com/klebs6/klebs-general
repositoryhttps://github.com/klebs6/klebs-general
max_upload_size
id1456868
size7,588
(klebs6)

documentation

https://docs.rs/ai-descriptor

README

ai-descriptor-trait

ai-descriptor-trait is a Rust crate that provides a simple, structured way to describe items with features in a human-readable format using AI-style descriptors. It defines reusable traits to represent items and their features, then generates a formatted description for these items.

Features

  • Modular Traits:

    • ItemFeature: Represents an individual feature with a text description.
    • ItemWithFeatures: Represents an item containing a header and a collection of features.
    • AIDescriptor: Automatically formats an item's header and its features into a human-readable string.
  • Convenience: Automatically composes structured descriptions for any type implementing ItemWithFeatures.

  • Flexibility: Uses Cow<'_, str> for efficient memory management, allowing seamless use of owned and borrowed strings.

Example Usage

Below is an example of how to implement and use the provided traits:

use std::borrow::Cow;
use ai_descriptor_trait::{ItemWithFeatures, AIDescriptor};

struct MyItem {
    header: String,
    features: Vec<Cow<'static, str>>,
}

impl ItemWithFeatures for MyItem {
    fn header(&self) -> Cow<'_, str> {
        Cow::Borrowed(&self.header)
    }

    fn features(&self) -> &[Cow<'_, str>] {
        &self.features
    }
}

fn main() {
    let item = MyItem {
        header: "My Cool Item".to_string(),
        features: vec![
            Cow::Borrowed("Lightweight"),
            Cow::Borrowed("Durable"),
            Cow::Borrowed("Eco-friendly"),
        ],
    };

    println!("{}", item.ai());
}

Output:

My Cool Item
It has the following features:
- Lightweight
- Durable
- Eco-friendly

Integration with Your Project

Add the following to your Cargo.toml:

[dependencies]
ai-descriptor-trait = "0.1.0"

Running Tests

Unit tests are provided to ensure the functionality of the AIDescriptor implementation. Run the tests using:

cargo test

Key Concepts

Traits Overview

  • ItemFeature: Defines a single feature for an item.

    pub trait ItemFeature {
        fn text(&self) -> Cow<'_, str>;
    }
    
  • ItemWithFeatures: Represents an item with a header and a collection of features.

    pub trait ItemWithFeatures {
        fn header(&self) -> Cow<'_, str>;
        fn features(&self) -> &[Cow<'_, str>];
    }
    
  • AIDescriptor: Generates a formatted description of any item implementing ItemWithFeatures.

    pub trait AIDescriptor {
        fn ai(&self) -> Cow<'_, str>;
    }
    

Test Example

A test is provided in the module to ensure the AIDescriptor works as expected:

#[cfg(test)]
mod tests {
    use super::*;

    struct TestItem {
        header: String,
        features: Vec<Cow<'static, str>>,
    }

    impl ItemWithFeatures for TestItem {
        fn header(&self) -> Cow<'_, str> {
            Cow::Borrowed(&self.header)
        }

        fn features(&self) -> &[Cow<'_, str>] {
            &self.features
        }
    }

    #[test]
    fn test_ai_descriptor() {
        let item = TestItem {
            header: "An Item.".to_string(),
            features: vec![
                Cow::Borrowed("Feature 1"),
                Cow::Borrowed("Feature 2"),
                Cow::Borrowed("Feature 3"),
            ],
        };

        let expected_output = "\
An Item.
It has the following features:
- Feature 1
- Feature 2
- Feature 3";

        assert_eq!(item.ai(), expected_output);
    }
}

License

This crate is licensed under the MIT License.


Contributions are welcome! Feel free to fork the repository and submit pull requests.

Commit count: 248

cargo fmt