use std::{fmt, str::FromStr};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Level {
Workspace,
App,
Kind,
Resource,
Item,
Field,
Action,
}
impl Level {
pub const ALL: [Self; 7] = [
Self::Workspace,
Self::App,
Self::Kind,
Self::Resource,
Self::Item,
Self::Field,
Self::Action,
];
#[must_use]
pub const fn tag(self) -> &'static str {
match self {
Self::Workspace => "workspace",
Self::App => "app",
Self::Kind => "kind",
Self::Resource => "resource",
Self::Item => "item",
Self::Field => "field",
Self::Action => "action",
}
}
#[must_use]
pub const fn noun(self) -> &'static str {
match self {
Self::Workspace => "workspace",
Self::App => "app",
Self::Kind => "kind",
Self::Resource => "resource",
Self::Item => "item",
Self::Field => "field",
Self::Action => "action",
}
}
fn from_tag(tag: &str) -> Option<Self> {
Self::ALL.into_iter().find(|level| level.tag() == tag)
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Segment {
Name(Level, String),
Any(Level),
Rest,
}
impl Segment {
const fn weight(&self) -> u8 {
match self {
Self::Name(..) => 2,
Self::Any(_) => 1,
Self::Rest => 0,
}
}
fn covers(&self, other: &Self) -> bool {
match (self, other) {
(Self::Name(level, id), Self::Name(other_level, other_id)) => {
level == other_level && id == other_id
}
(Self::Any(level), Self::Name(other_level, _) | Self::Any(other_level)) => {
level == other_level
}
_ => false,
}
}
#[must_use]
pub const fn level(&self) -> Option<Level> {
match self {
Self::Name(level, _) | Self::Any(level) => Some(*level),
Self::Rest => None,
}
}
#[must_use]
pub fn id(&self) -> Option<&str> {
match self {
Self::Name(_, id) => Some(id),
_ => None,
}
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TreePath(Vec<Segment>);
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Specificity(Vec<u8>);
impl TreePath {
#[must_use]
pub const fn root() -> Self {
Self(Vec::new())
}
#[must_use]
pub fn workspace(id: impl Into<String>) -> Self {
Self::root().push(Level::Workspace, id)
}
#[must_use]
pub fn push(mut self, level: Level, id: impl Into<String>) -> Self {
self.0.push(Segment::Name(level, id.into()));
self
}
#[must_use]
pub fn app(self, id: impl Into<String>) -> Self {
self.push(Level::App, id)
}
#[must_use]
pub fn kind(self, id: impl Into<String>) -> Self {
self.push(Level::Kind, id)
}
#[must_use]
pub fn resource(self, id: impl Into<String>) -> Self {
self.push(Level::Resource, id)
}
#[must_use]
pub fn item(self, id: impl Into<String>) -> Self {
self.push(Level::Item, id)
}
#[must_use]
pub fn field(mut self, path: &str) -> Self {
for part in path.split('.').filter(|part| !part.is_empty()) {
self.0.push(Segment::Name(Level::Field, part.to_owned()));
}
self
}
#[must_use]
pub fn action(self, id: impl Into<String>) -> Self {
self.push(Level::Action, id)
}
#[must_use]
pub fn any(mut self, level: Level) -> Self {
self.0.push(Segment::Any(level));
self
}
#[must_use]
pub fn rest(mut self) -> Self {
if self.0.last() != Some(&Segment::Rest) {
self.0.push(Segment::Rest);
}
self
}
#[must_use]
pub fn segments(&self) -> &[Segment] {
&self.0
}
#[must_use]
pub const fn is_root(&self) -> bool {
self.0.is_empty()
}
#[must_use]
pub fn within_workspace(&self) -> Self {
let workspace = matches!(
self.0.first(),
Some(Segment::Name(Level::Workspace, _) | Segment::Any(Level::Workspace))
);
Self(
self.0
.iter()
.skip(usize::from(workspace))
.cloned()
.collect(),
)
}
#[must_use]
pub fn parent(&self) -> Option<Self> {
let (_, parent) = self.0.split_last()?;
Some(Self(parent.to_vec()))
}
#[must_use]
pub fn id_at(&self, level: Level) -> Option<&str> {
self.0.iter().find_map(|segment| match segment {
Segment::Name(at, id) if *at == level => Some(id.as_str()),
_ => None,
})
}
#[must_use]
pub fn join(&self, rest: &Self) -> Self {
self.clone().joined(rest)
}
#[must_use]
pub fn joined(self, rest: &Self) -> Self {
let mut path = self;
path.0.reserve(rest.0.len());
for segment in &rest.0 {
path = match segment {
Segment::Rest => path.rest(),
segment => {
path.0.push(segment.clone());
path
}
};
}
path
}
#[must_use]
pub fn with_workspace(&self, from: &str, to: &str) -> Self {
let mut path = self.clone();
if let Some(Segment::Name(Level::Workspace, id)) = path.0.first_mut()
&& id == from
{
to.clone_into(id);
}
path
}
#[must_use]
pub fn workspace_id(&self) -> Option<&str> {
self.id_at(Level::Workspace)
}
#[must_use]
pub fn covers(&self, path: &Self) -> bool {
covers(self.trimmed(), &path.0)
}
#[must_use]
pub fn overlaps(&self, other: &Self) -> bool {
overlaps(self.trimmed(), other.trimmed())
}
#[must_use]
pub fn toward(&self, pattern: &Self) -> Option<Self> {
let (mine, theirs) = (self.trimmed(), pattern.trimmed());
if !overlaps(mine, theirs)
|| mine.contains(&Segment::Rest)
|| theirs.contains(&Segment::Rest)
{
return None;
}
let at = |segments: &[Segment], level| {
segments
.iter()
.find(|segment| segment.level() == Some(level))
.cloned()
};
let node = Self(
Level::ALL
.into_iter()
.filter_map(|level| match (at(mine, level), at(theirs, level)) {
(Some(named @ Segment::Name(..)), _) | (_, Some(named @ Segment::Name(..))) => {
Some(named)
}
(mine, theirs) => mine.or(theirs),
})
.collect(),
);
self.covers(&node).then_some(node)
}
#[must_use]
pub fn is_within(&self, ancestor: &Self) -> bool {
ancestor.covers(self)
}
#[must_use]
pub fn specificity(&self) -> Specificity {
Specificity(self.trimmed().iter().map(Segment::weight).collect())
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn truncate(&mut self, len: usize) {
self.0.truncate(len);
}
pub fn push_name(&mut self, level: Level, id: impl Into<String>) {
self.0.push(Segment::Name(level, id.into()));
}
pub(crate) fn cmp_specificity(&self, other: &Self) -> std::cmp::Ordering {
let theirs = other.trimmed().iter().map(Segment::weight);
self.trimmed().iter().map(Segment::weight).cmp(theirs)
}
fn write_with(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.0.is_empty() {
return f.write_str("/");
}
for (index, segment) in self.0.iter().enumerate() {
if index > 0 {
f.write_str("/")?;
}
match segment {
Segment::Name(level, id) => {
write!(f, "{}:", level.tag())?;
if id == "*" {
f.write_str("%2A")?;
} else {
for ch in id.chars() {
match ch {
'%' => f.write_str("%25")?,
'/' => f.write_str("%2F")?,
_ => fmt::Write::write_char(f, ch)?,
}
}
}
}
Segment::Any(level) => write!(f, "{}:*", level.tag())?,
Segment::Rest => f.write_str("**")?,
}
}
Ok(())
}
fn trimmed(&self) -> &[Segment] {
let mut segments = self.0.as_slice();
while let [init @ .., Segment::Rest] = segments {
segments = init;
}
segments
}
}
fn covers(pattern: &[Segment], path: &[Segment]) -> bool {
let Some((first, rest)) = pattern.split_first() else {
return true;
};
if first.level() == Some(Level::Resource)
&& let Some((kind, resource)) = path.split_first()
&& kind.level() == Some(Level::Kind)
{
return covers(pattern, resource);
}
match first {
Segment::Rest => {
let mut tail = path;
loop {
if covers(rest, tail) {
return true;
}
let Some((_, shorter)) = tail.split_first() else {
return false;
};
tail = shorter;
}
}
_ => path
.split_first()
.is_some_and(|(head, tail)| first.covers(head) && covers(rest, tail)),
}
}
fn overlaps(left: &[Segment], right: &[Segment]) -> bool {
let (Some((first, left_rest)), Some((second, right_rest))) =
(left.split_first(), right.split_first())
else {
return true;
};
match (first.level(), second.level()) {
(Some(Level::Resource), Some(Level::Kind)) => return overlaps(left, right_rest),
(Some(Level::Kind), Some(Level::Resource)) => return overlaps(left_rest, right),
_ => {}
}
match (first, second) {
(Segment::Rest, _) | (_, Segment::Rest) => {
overlaps(left_rest, right) || overlaps(left, right_rest)
}
_ => compatible(first, second) && overlaps(left_rest, right_rest),
}
}
fn compatible(left: &Segment, right: &Segment) -> bool {
match (left, right) {
(Segment::Name(level, id), Segment::Name(other_level, other_id)) => {
level == other_level && id == other_id
}
(Segment::Name(level, _) | Segment::Any(level), Segment::Any(other_level))
| (Segment::Any(level), Segment::Name(other_level, _)) => level == other_level,
_ => false,
}
}
impl fmt::Display for TreePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.write_with(f)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PathError(pub String);
impl fmt::Display for PathError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "invalid tree path: {}", self.0)
}
}
impl std::error::Error for PathError {}
impl FromStr for TreePath {
type Err = PathError;
fn from_str(text: &str) -> Result<Self, Self::Err> {
if text == "/" || text.is_empty() {
return Ok(Self::root());
}
let mut path = Self::root();
for part in text.split('/') {
if part == "**" {
path = path.rest();
continue;
}
let (tag, id) = part
.split_once(':')
.ok_or_else(|| PathError(format!("segment `{part}` has no level")))?;
let level =
Level::from_tag(tag).ok_or_else(|| PathError(format!("unknown level `{tag}`")))?;
if id == "*" {
path = path.any(level);
} else if id.is_empty() && level != Level::Resource {
return Err(PathError(format!("segment `{part}` has no id")));
} else {
path = path.push(level, unescape(id)?);
}
}
Ok(path)
}
}
fn unescape(id: &str) -> Result<String, PathError> {
let mut out = String::with_capacity(id.len());
let mut chars = id.chars();
while let Some(ch) = chars.next() {
if ch != '%' {
out.push(ch);
continue;
}
let code: String = chars.by_ref().take(2).collect();
out.push(match code.as_str() {
"25" => '%',
"2F" | "2f" => '/',
"2A" | "2a" => '*',
_ => return Err(PathError(format!("bad escape in `{id}`"))),
});
}
Ok(out)
}
impl Serialize for TreePath {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.collect_str(self)
}
}
impl<'de> Deserialize<'de> for TreePath {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
String::deserialize(deserializer)?
.parse()
.map_err(serde::de::Error::custom)
}
}