3 releases (breaking)
new 0.3.0 | Nov 22, 2024 |
---|---|
0.2.0 | Nov 22, 2024 |
0.1.0 | Nov 22, 2024 |
#37 in #ergonomics
25 downloads per month
Used in 3 crates
(2 directly)
8KB
66 lines
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 implementingItemWithFeatures
.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.