use super::SourcePos;
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct SourceName {
name: String,
pub(crate) imported: SourceKind,
}
impl SourceName {
pub fn root<T: ToString>(name: T) -> Self {
Self {
name: name.to_string(),
imported: SourceKind::Root,
}
}
pub fn called<T: ToString>(name: T, from: SourcePos) -> Self {
Self {
name: from.file_url().into(),
imported: SourceKind::Call(name.to_string(), from),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn is_builtin(&self) -> bool {
self.name.starts_with("sass:") || self.name.is_empty()
}
}
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub enum SourceKind {
Root,
Import(SourcePos),
Use(SourcePos),
Forward(SourcePos),
Call(String, SourcePos),
}
impl SourceKind {
pub fn load_css(pos: &SourcePos) -> Self {
Self::Call("load-css".into(), pos.clone())
}
pub(crate) fn url(self, url: &str) -> SourceName {
SourceName {
name: url.to_string(),
imported: self,
}
}
pub(crate) fn next(&self) -> Option<&SourcePos> {
match self {
Self::Root => None,
Self::Import(pos) => Some(pos),
Self::Use(pos) => Some(pos),
Self::Forward(pos) => Some(pos),
Self::Call(_, pos) => Some(pos),
}
}
pub(crate) fn is_import(&self) -> bool {
matches!(self, Self::Import(_))
}
}
impl fmt::Display for SourceKind {
fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Root => out.write_str("root stylesheet"),
Self::Import(_) => out.write_str("@import"),
Self::Use(_) => out.write_str("@use"),
Self::Forward(_) => out.write_str("@forward"),
Self::Call(name, _) => write!(out, "{name}()"),
}
}
}