use std::{collections::BTreeMap, hash::Hash, sync::atomic::Ordering::Relaxed};
use rspack_cacheable::cacheable;
use rspack_util::atom::Atom;
use serde::Serialize;
use super::{ExportInfoData, NEXT_EXPORTS_INFO_UKEY};
use crate::ExportsInfoArtifact;
#[cacheable]
#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd, Serialize)]
pub struct ExportsInfo(u32);
impl ExportsInfo {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self(NEXT_EXPORTS_INFO_UKEY.fetch_add(1, Relaxed))
}
pub fn as_data<'a>(&self, exports_info_artifact: &'a ExportsInfoArtifact) -> &'a ExportsInfoData {
exports_info_artifact.get_exports_info_by_id(self)
}
pub fn as_data_mut<'a>(
&self,
exports_info_artifact: &'a mut ExportsInfoArtifact,
) -> &'a mut ExportsInfoData {
exports_info_artifact.get_exports_info_mut_by_id(self)
}
}
#[derive(Debug, Clone)]
pub struct ExportsInfoData {
exports: BTreeMap<Atom, ExportInfoData>,
other_exports_info: ExportInfoData,
side_effects_only_info: ExportInfoData,
id: ExportsInfo,
}
impl Default for ExportsInfoData {
fn default() -> Self {
let id = ExportsInfo::new();
Self {
exports: BTreeMap::default(),
other_exports_info: ExportInfoData::new(id, None, None),
side_effects_only_info: ExportInfoData::new(id, Some("*side effects only*".into()), None),
id,
}
}
}
impl ExportsInfoData {
pub fn reset(&mut self) {
let id = self.id;
*self = ExportsInfoData::default();
self.id = id;
}
pub fn id(&self) -> ExportsInfo {
self.id
}
pub fn other_exports_info(&self) -> &ExportInfoData {
&self.other_exports_info
}
pub fn other_exports_info_mut(&mut self) -> &mut ExportInfoData {
&mut self.other_exports_info
}
pub fn side_effects_only_info(&self) -> &ExportInfoData {
&self.side_effects_only_info
}
pub fn side_effects_only_info_mut(&mut self) -> &mut ExportInfoData {
&mut self.side_effects_only_info
}
pub fn named_exports(&self, name: &Atom) -> Option<&ExportInfoData> {
self.exports.get(name)
}
pub fn named_exports_mut(&mut self, name: &Atom) -> Option<&mut ExportInfoData> {
self.exports.get_mut(name)
}
pub fn exports(&self) -> &BTreeMap<Atom, ExportInfoData> {
&self.exports
}
pub fn exports_mut(&mut self) -> &mut BTreeMap<Atom, ExportInfoData> {
&mut self.exports
}
}