use std::{fmt::Display, path::Path};
use crate::ResolveError;
#[allow(clippy::missing_errors_doc)] pub trait PackageJson: Sized {
#[must_use]
fn path(&self) -> &Path;
#[must_use]
fn realpath(&self) -> &Path;
#[must_use]
fn directory(&self) -> &Path;
fn name(&self) -> Option<&str>;
fn r#type(&self) -> Option<PackageType>;
#[must_use]
fn main_fields<'a>(&'a self, main_fields: &'a [String]) -> impl Iterator<Item = &'a str> + 'a;
#[must_use]
fn exports_fields<'a>(
&'a self,
exports_fields: &'a [Vec<String>],
) -> impl Iterator<Item = impl ImportsExportsEntry<'a>> + 'a;
#[must_use]
fn imports_fields<'a>(
&'a self,
imports_fields: &'a [Vec<String>],
) -> impl Iterator<Item = impl ImportsExportsMap<'a>> + 'a;
fn resolve_browser_field<'a>(
&'a self,
path: &Path,
request: Option<&str>,
alias_fields: &'a [Vec<String>],
) -> Result<Option<&'a str>, ResolveError>;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "fs_cache", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "fs_cache", serde(rename_all = "lowercase"))]
pub enum PackageType {
CommonJs,
Module,
}
impl Display for PackageType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CommonJs => f.write_str("commonjs"),
Self::Module => f.write_str("module"),
}
}
}
pub trait ImportsExportsEntry<'a>: Clone + Sized {
type Array: ImportsExportsArray<'a, Entry = Self>;
type Map: ImportsExportsMap<'a, Entry = Self>;
fn kind(&self) -> ImportsExportsKind;
fn as_string(&self) -> Option<&'a str>;
fn as_array(&self) -> Option<Self::Array>;
fn as_map(&self) -> Option<Self::Map>;
}
pub trait ImportsExportsArray<'a>: Clone + Sized {
type Entry: ImportsExportsEntry<'a, Array = Self>;
fn is_empty(&self) -> bool {
self.len() == 0
}
fn len(&self) -> usize;
fn iter(&self) -> impl Iterator<Item = Self::Entry>;
}
pub trait ImportsExportsMap<'a>: Clone + Sized {
type Entry: ImportsExportsEntry<'a, Map = Self>;
fn get(&self, key: &str) -> Option<Self::Entry>;
fn keys(&self) -> impl Iterator<Item = &'a str>;
fn iter(&self) -> impl Iterator<Item = (&'a str, Self::Entry)>;
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ImportsExportsKind {
String,
Array,
Map,
Invalid,
}