From 1b80617c1b9fb5d1c7f54fa33362d05b3a4c41a7 Mon Sep 17 00:00:00 2001 From: Arsenii Kulikov Date: Tue, 10 Sep 2024 20:19:20 +0400 Subject: [PATCH] wip --- crates/compilers/src/resolver/profiles.rs | 59 +++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 crates/compilers/src/resolver/profiles.rs diff --git a/crates/compilers/src/resolver/profiles.rs b/crates/compilers/src/resolver/profiles.rs new file mode 100644 index 00000000..b8342692 --- /dev/null +++ b/crates/compilers/src/resolver/profiles.rs @@ -0,0 +1,59 @@ +use crate::{ArtifactOutput, Compiler, CompilerSettings, Project}; +use alloy_primitives::hex; +use md5::Digest; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Default)] +pub struct CompilationProfiles { + profiles: Vec>, +} + +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct CompilationProfile { + pub settings: S, + pub id: String, +} + +impl CompilationProfile { + pub fn new(settings: S) -> Self { + let mut hasher = md5::Md5::new(); + let ser = serde_json::to_string(&settings).unwrap(); + hasher.update(ser.as_bytes()); + let id = hex::encode(hasher.finalize()); + + Self { settings, id } + } +} + +impl CompilationProfiles { + pub fn new, T: ArtifactOutput>(project: &Project) -> Self { + let mut profiles = Self::default(); + + profiles.add(project.settings.clone()); + + profiles + } + + pub fn add(&mut self, settings: S) -> usize { + self.profiles.push(CompilationProfile::new(settings)); + + self.profiles.len() - 1 + } + + pub fn find_or_create(&mut self, restrictions: &S::Restrictions) -> usize { + if let Some((idx, _)) = self + .profiles + .iter() + .enumerate() + .find(|(_, profile)| profile.settings.satisfies_restrictions(restrictions)) + { + return idx; + } + + self.add(self.profiles[0].settings.apply_restrictions(restrictions)) + } + + pub fn get(&self, idx: usize) -> &CompilationProfile { + self.profiles.get(idx).unwrap() + } +}