use std::collections::BTreeMap;
use boxferry_model::{Identifier, ModelError, SourceId};
use compose_lens::{merge::MergedProject, profiles::ProfileSelection, source::SourceId as ComposeSourceId};
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ComposeSource {
project: MergedProject,
fallback_application_name: Identifier,
source_ids: BTreeMap<ComposeSourceId, SourceId>,
profile_selection: Option<ProfileSelection>,
}
impl ComposeSource {
pub fn new(project: MergedProject, fallback_application_name: Identifier) -> Result<Self, ModelError> {
let source_ids = project
.source_ids()
.iter()
.copied()
.map(|source_id| {
SourceId::new(format!("compose-source-{}", source_id.get())).map(|neutral| (source_id, neutral))
})
.collect::<Result<_, _>>()?;
Ok(Self {
project,
fallback_application_name,
source_ids,
profile_selection: None,
})
}
#[must_use]
pub fn with_source_id(mut self, compose: ComposeSourceId, neutral: SourceId) -> Self {
self.source_ids.insert(compose, neutral);
self
}
#[must_use]
pub fn with_profile_selection(mut self, selection: ProfileSelection) -> Self {
self.profile_selection = Some(selection);
self
}
#[must_use]
pub const fn project(&self) -> &MergedProject {
&self.project
}
#[must_use]
pub const fn fallback_application_name(&self) -> &Identifier {
&self.fallback_application_name
}
#[must_use]
pub fn source_id(&self, compose: ComposeSourceId) -> Option<&SourceId> {
self.source_ids.get(&compose)
}
#[must_use]
pub const fn profile_selection(&self) -> Option<&ProfileSelection> {
self.profile_selection.as_ref()
}
}