I'm trying to make my rust project more modular, where I have different integrations that should implement certain functions and have certain fields, along with additional ones specific to that integration. How can I make it so that a function parameter or struct field can accept any integration that implements the required functions?
Currently I'm using enums to do this, but the code is a bit sloppy.
enum SocialIntegration {
Facebook(FacebookIntegration),
Twitter(TwitterIntegration),
}
struct FacebookIntegration {
pub username: String,
pub url: String,
}
impl FacebookIntegration {
pub fn search(&self, query: &str) -> Vec<SearchResult> {
todo!()
}
}
struct TwitterIntegration {
pub username: String,
pub url: String,
following_list: Vec<String>,
}
impl TwitterIntegration {
pub fn search(&self, query: &str) -> Vec<SearchResult> {
todo!()
}
}
struct IntegrationHelper {
integrations: Vec<SocialIntegration>,
}
impl IntegrationHelper {
pub fn search(&self, query: &str) -> Vec<SearchResult> {
for integration in self.integrations {
match integration {
SocialIntegration::Facebook(integration) => integration.search(query),
SocialIntegration::Twitter(integration) => integration.search(query),
}
}
}
}
Coming from typescript, and previously used just an interface, and then creating a class implementing the interface. I couldn't find anything quite similar in Rust, or figure out a combination of things to use to achieve a similar result in Rust.