Crates.io | ai-descriptor-trait |
lib.rs | ai-descriptor-trait |
version | 0.3.0 |
source | src |
created_at | 2024-11-22 01:14:32.461577 |
updated_at | 2024-11-22 02:24:41.761267 |
description | Provides an interface for the proc macro we can use to annotate enums for facilitating itemization and description using an AI model |
homepage | https://github.com/klebs6/klebs-general |
repository | https://github.com/klebs6/klebs-general |
max_upload_size | |
id | 1456868 |
size | 7,588 |
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.
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.
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
Add the following to your Cargo.toml
:
[dependencies]
ai-descriptor-trait = "0.1.0"
Unit tests are provided to ensure the functionality of the AIDescriptor
implementation. Run the tests using:
cargo test
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>;
}
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);
}
}
This crate is licensed under the MIT License.
Contributions are welcome! Feel free to fork the repository and submit pull requests.