use serde::{Deserialize, Serialize};
use crate::paths::{ModulePath, Name, PathSyntax};
use crate::text::Span;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportRef {
pub span: Span,
pub path: ModulePath,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub group: Option<ImportGroup>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub glob: bool,
#[serde(default = "yes", skip_serializing_if = "std::ops::Not::not")]
pub declares: bool,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub reexport: bool,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alias: Option<Name>,
}
fn yes() -> bool {
true
}
impl ImportRef {
pub fn new(span: Span, path: ModulePath) -> Self {
Self {
span,
path,
group: None,
glob: false,
declares: true,
reexport: false,
alias: None,
}
}
pub fn glob(mut self) -> Self {
self.glob = true;
self
}
pub fn reexporting(mut self) -> Self {
self.reexport = true;
self
}
pub fn aliased(mut self, alias: impl Into<Name>) -> Self {
self.alias = Some(alias.into());
self
}
pub fn binding(&self) -> Option<&Name> {
if self.glob {
return None;
}
self.alias.as_ref().or_else(|| self.path.last())
}
pub fn is_standalone(&self) -> bool {
self.group.is_none()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ImportGroup {
pub prefix: ModulePath,
pub item: Span,
pub list: Span,
pub items: usize,
pub statement: Span,
pub top_level: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ImportRule {
Node {
kind: &'static str,
field: Option<&'static str>,
under: Option<&'static str>,
},
Pattern {
pattern: &'static str,
capture: &'static str,
},
}
impl ImportRule {
pub const fn node(kind: &'static str) -> Self {
Self::Node {
kind,
field: None,
under: None,
}
}
pub const fn field(kind: &'static str, field: &'static str) -> Self {
Self::Node {
kind,
field: Some(field),
under: None,
}
}
pub const fn under(self, parent: &'static str) -> Self {
match self {
Self::Node { kind, field, .. } => Self::Node {
kind,
field,
under: Some(parent),
},
other => other,
}
}
pub const fn pattern(pattern: &'static str, capture: &'static str) -> Self {
Self::Pattern { pattern, capture }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImportNesting {
pub list: &'static str,
pub scope: &'static str,
pub prefix_field: &'static str,
pub statement: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AliasRule {
pub under: &'static str,
pub field: &'static str,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ReExportRule {
#[default]
Never,
Modifier(&'static str),
Statement(&'static str),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImportGrammar {
pub syntax: PathSyntax,
pub rules: &'static [ImportRule],
pub reexports: ReExportRule,
pub nesting: Option<ImportNesting>,
pub glob_under: Option<&'static str>,
pub glob_marker: Option<&'static str>,
pub alias: Option<AliasRule>,
pub statements: &'static [&'static str],
}
impl ImportGrammar {
pub const EMPTY: Self = Self {
syntax: PathSyntax::Scoped,
rules: &[],
reexports: ReExportRule::Never,
nesting: None,
glob_under: None,
glob_marker: None,
alias: None,
statements: &[],
};
pub const fn new(syntax: PathSyntax, rules: &'static [ImportRule]) -> Self {
Self {
syntax,
rules,
reexports: ReExportRule::Never,
nesting: None,
glob_under: None,
glob_marker: None,
alias: None,
statements: &[],
}
}
pub const fn aliased_by(mut self, parent: &'static str, field: &'static str) -> Self {
self.alias = Some(AliasRule {
under: parent,
field,
});
self
}
pub const fn statements(mut self, kinds: &'static [&'static str]) -> Self {
self.statements = kinds;
self
}
pub const fn nested(mut self, nesting: ImportNesting) -> Self {
self.nesting = Some(nesting);
self
}
pub const fn reexports(mut self, rule: ReExportRule) -> Self {
self.reexports = rule;
self
}
pub const fn globs_under(mut self, parent: &'static str, marker: &'static str) -> Self {
self.glob_under = Some(parent);
self.glob_marker = Some(marker);
self
}
}