miden_assembly_syntax/ast/path/
path.rs1use alloc::{
2 borrow::{Borrow, Cow, ToOwned},
3 string::ToString,
4};
5use core::fmt;
6
7use super::{Iter, Join, PathBuf, PathComponent, PathError, StartsWith};
8use crate::ast::Ident;
9
10#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[repr(transparent)]
13pub struct Path {
14 inner: str,
16}
17
18impl fmt::Debug for Path {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 fmt::Debug::fmt(&self.inner, f)
21 }
22}
23
24#[cfg(feature = "serde")]
25impl serde::Serialize for Path {
26 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
27 where
28 S: serde::Serializer,
29 {
30 serializer.serialize_str(&self.inner)
31 }
32}
33
34#[cfg(feature = "serde")]
35impl<'de> serde::Deserialize<'de> for &'de Path {
36 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
37 where
38 D: serde::Deserializer<'de>,
39 {
40 use serde::de::Visitor;
41
42 struct PathVisitor;
43
44 impl<'de> Visitor<'de> for PathVisitor {
45 type Value = &'de Path;
46
47 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
48 formatter.write_str("a borrowed Path")
49 }
50
51 fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
52 where
53 E: serde::de::Error,
54 {
55 Path::validate(v).map_err(serde::de::Error::custom)
56 }
57 }
58
59 deserializer.deserialize_any(PathVisitor)
60 }
61}
62
63impl ToOwned for Path {
64 type Owned = PathBuf;
65 #[inline]
66 fn to_owned(&self) -> PathBuf {
67 self.to_path_buf()
68 }
69 #[inline]
70 fn clone_into(&self, target: &mut Self::Owned) {
71 self.inner.clone_into(&mut target.inner)
72 }
73}
74
75impl Borrow<Path> for PathBuf {
76 fn borrow(&self) -> &Path {
77 Path::new(self)
78 }
79}
80
81impl AsRef<str> for Path {
82 #[inline]
83 fn as_ref(&self) -> &str {
84 &self.inner
85 }
86}
87
88impl AsRef<Path> for str {
89 #[inline(always)]
90 fn as_ref(&self) -> &Path {
91 unsafe { &*(self as *const str as *const Path) }
92 }
93}
94
95impl AsRef<Path> for Ident {
96 #[inline(always)]
97 fn as_ref(&self) -> &Path {
98 self.as_str().as_ref()
99 }
100}
101
102impl AsRef<Path> for crate::ast::ProcedureName {
103 #[inline(always)]
104 fn as_ref(&self) -> &Path {
105 let ident: &Ident = self.as_ref();
106 ident.as_str().as_ref()
107 }
108}
109
110impl AsRef<Path> for crate::ast::QualifiedProcedureName {
111 #[inline(always)]
112 fn as_ref(&self) -> &Path {
113 self.as_path()
114 }
115}
116
117impl AsRef<Path> for Path {
118 #[inline(always)]
119 fn as_ref(&self) -> &Path {
120 self
121 }
122}
123
124impl From<&Path> for alloc::sync::Arc<Path> {
125 fn from(path: &Path) -> Self {
126 path.to_path_buf().into()
127 }
128}
129
130impl Path {
132 pub const MAX_COMPONENT_LENGTH: usize = (u16::MAX as usize) - 2;
138
139 pub const EMPTY: &Path = unsafe { &*("" as *const str as *const Path) };
141
142 pub const KERNEL_PATH: &str = "$kernel";
144 pub const ABSOLUTE_KERNEL_PATH: &str = "::$kernel";
145 pub const KERNEL: &Path =
146 unsafe { &*(Self::ABSOLUTE_KERNEL_PATH as *const str as *const Path) };
147
148 pub const EXEC_PATH: &str = "$exec";
150 pub const ABSOLUTE_EXEC_PATH: &str = "::$exec";
151 pub const EXEC: &Path = unsafe { &*(Self::ABSOLUTE_EXEC_PATH as *const str as *const Path) };
152
153 pub fn new<S: AsRef<str> + ?Sized>(path: &S) -> &Path {
154 unsafe { &*(path.as_ref() as *const str as *const Path) }
156 }
157
158 pub fn from_mut(path: &mut str) -> &mut Path {
159 unsafe { &mut *(path as *mut str as *mut Path) }
161 }
162
163 pub fn validate(path: &str) -> Result<&Path, PathError> {
165 match path {
166 "" | "\"\"" => return Err(PathError::Empty),
167 "::" => return Err(PathError::EmptyComponent),
168 _ => (),
169 }
170
171 if path.len() > u16::MAX as usize {
172 return Err(PathError::TooLong { max: u16::MAX as usize });
173 }
174
175 for result in Iter::new(path) {
176 result?;
177 }
178
179 Ok(Path::new(path))
180 }
181
182 pub const fn kernel_path() -> &'static Path {
184 Path::KERNEL
185 }
186
187 pub const fn exec_path() -> &'static Path {
189 Path::EXEC
190 }
191
192 #[inline]
193 pub const fn as_str(&self) -> &str {
194 &self.inner
195 }
196
197 #[inline]
198 pub fn as_mut_str(&mut self) -> &mut str {
199 &mut self.inner
200 }
201
202 pub fn as_ident(&self) -> Option<Ident> {
207 let mut components = self.components().filter_map(Result::ok);
208 match components.next()? {
209 component @ PathComponent::Normal(_) => {
210 if components.next().is_none() {
211 component.to_ident()
212 } else {
213 None
214 }
215 },
216 PathComponent::Root => None,
217 }
218 }
219
220 pub fn to_path_buf(&self) -> PathBuf {
222 PathBuf { inner: self.inner.to_string() }
223 }
224
225 pub fn from_ident(ident: &Ident) -> Cow<'_, Path> {
228 let ident = ident.as_str();
229 if Ident::requires_quoting(ident) {
230 let mut buf = PathBuf::with_capacity(ident.len() + 2);
231 buf.push_component(ident);
232 Cow::Owned(buf)
233 } else {
234 Cow::Borrowed(Path::new(ident))
235 }
236 }
237}
238
239impl Path {
241 pub fn is_empty(&self) -> bool {
243 matches!(&self.inner, "" | "::" | "\"\"")
244 }
245
246 pub fn len(&self) -> usize {
248 self.components().count()
249 }
250
251 pub fn char_len(&self) -> usize {
253 self.inner.chars().count()
254 }
255
256 #[inline]
258 pub fn byte_len(&self) -> usize {
259 self.inner.len()
260 }
261
262 pub fn is_absolute(&self) -> bool {
264 matches!(self.components().next(), Some(Ok(PathComponent::Root)))
265 }
266
267 pub fn to_absolute(&self) -> Result<Cow<'_, Path>, PathError> {
276 if self.is_absolute() {
277 for component in self.components() {
278 component?;
279 }
280 if self.byte_len() > u16::MAX as usize {
281 return Err(PathError::TooLong { max: u16::MAX as usize });
282 }
283 Ok(Cow::Borrowed(self))
284 } else {
285 let mut buf = PathBuf::with_capacity(self.byte_len() + 2);
286 buf.push_component("::");
287 buf.extend_with_components(self.components())?;
288 if buf.byte_len() > u16::MAX as usize {
289 return Err(PathError::TooLong { max: u16::MAX as usize });
290 }
291 Ok(Cow::Owned(buf))
292 }
293 }
294
295 pub fn to_relative(&self) -> &Path {
297 match self.inner.strip_prefix("::") {
298 Some(rest) => Path::new(rest),
299 None => self,
300 }
301 }
302
303 pub fn parent(&self) -> Option<&Path> {
309 let mut components = self.components();
310 match components.next_back()?.ok()? {
311 PathComponent::Root => None,
312 _ => Some(components.as_path()),
313 }
314 }
315
316 pub fn components(&self) -> Iter<'_> {
318 Iter::new(&self.inner)
319 }
320
321 pub fn first(&self) -> Option<&str> {
325 self.split_first().map(|(first, _)| first)
326 }
327
328 pub fn last(&self) -> Option<&str> {
332 self.split_last().map(|(last, _)| last)
333 }
334
335 pub fn split_first(&self) -> Option<(&str, &Path)> {
340 let mut components = self.components();
341 match components.next()?.ok()? {
342 PathComponent::Root => {
343 let first = components.next().and_then(Result::ok).map(|c| c.as_str())?;
344 Some((first, components.as_path()))
345 },
346 first @ PathComponent::Normal(_) => Some((first.as_str(), components.as_path())),
347 }
348 }
349
350 pub fn split_last(&self) -> Option<(&str, &Path)> {
355 let mut components = self.components();
356 match components.next_back()?.ok()? {
357 PathComponent::Root => None,
358 last @ PathComponent::Normal(_) => Some((last.as_str(), components.as_path())),
359 }
360 }
361
362 pub fn is_kernel_path(&self) -> bool {
364 match self.inner.strip_prefix("::") {
365 Some(Self::KERNEL_PATH) => true,
366 Some(_) => false,
367 None => &self.inner == Self::KERNEL_PATH,
368 }
369 }
370
371 pub fn is_in_kernel(&self) -> bool {
373 if self.is_kernel_path() {
374 return true;
375 }
376
377 match self.split_last() {
378 Some((_, prefix)) => prefix.is_kernel_path(),
379 None => false,
380 }
381 }
382
383 pub fn is_exec_path(&self) -> bool {
385 match self.inner.strip_prefix("::") {
386 Some(Self::EXEC_PATH) => true,
387 Some(_) => false,
388 None => &self.inner == Self::EXEC_PATH,
389 }
390 }
391
392 pub fn is_in_exec(&self) -> bool {
394 if self.is_exec_path() {
395 return true;
396 }
397
398 match self.split_last() {
399 Some((_, prefix)) => prefix.is_exec_path(),
400 None => false,
401 }
402 }
403
404 #[inline]
412 pub fn starts_with<Prefix>(&self, prefix: &Prefix) -> bool
413 where
414 Prefix: ?Sized,
415 Self: StartsWith<Prefix>,
416 {
417 <Self as StartsWith<Prefix>>::starts_with(self, prefix)
418 }
419
420 #[inline]
428 pub fn starts_with_exactly<Prefix>(&self, prefix: &Prefix) -> bool
429 where
430 Prefix: ?Sized,
431 Self: StartsWith<Prefix>,
432 {
433 <Self as StartsWith<Prefix>>::starts_with_exactly(self, prefix)
434 }
435
436 pub fn strip_prefix<'a>(&'a self, prefix: &Self) -> Option<&'a Self> {
442 let mut components = self.components();
443 for prefix_component in prefix.components() {
444 let prefix_component = prefix_component.expect("invalid prefix path");
453 match (components.next(), prefix_component) {
454 (Some(Ok(PathComponent::Root)), PathComponent::Root) => (),
455 (Some(Ok(c @ PathComponent::Normal(_))), pc @ PathComponent::Normal(_)) => {
456 if c.as_str() != pc.as_str() {
457 return None;
458 }
459 },
460 (Some(Ok(_) | Err(_)) | None, _) => return None,
461 }
462 }
463 Some(components.as_path())
464 }
465
466 #[inline]
479 pub fn join<P>(&self, other: &P) -> PathBuf
480 where
481 P: ?Sized,
482 Path: Join<P>,
483 {
484 <Path as Join<P>>::join(self, other)
485 }
486
487 pub fn canonicalize(&self) -> Result<PathBuf, PathError> {
496 let mut buf = PathBuf::with_capacity(self.byte_len());
497 buf.extend_with_components(self.components())?;
498 if buf.byte_len() > u16::MAX as usize {
499 return Err(PathError::TooLong { max: u16::MAX as usize });
500 }
501 Ok(buf)
502 }
503}
504
505impl StartsWith<str> for Path {
506 fn starts_with(&self, prefix: &str) -> bool {
507 let this = self.to_relative();
508 <Path as StartsWith<str>>::starts_with_exactly(this, prefix)
509 }
510
511 #[inline]
512 fn starts_with_exactly(&self, prefix: &str) -> bool {
513 match prefix {
514 "" => true,
515 "::" => self.is_absolute(),
516 prefix => {
517 let mut components = self.components();
518 let prefix = if let Some(prefix) = prefix.strip_prefix("::") {
519 let is_absolute =
520 components.next().is_some_and(|c| matches!(c, Ok(PathComponent::Root)));
521 if !is_absolute {
522 return false;
523 }
524 prefix
525 } else {
526 prefix
527 };
528 components.next().is_some_and(
529 |c| matches!(c, Ok(c @ PathComponent::Normal(_)) if c.as_str() == prefix),
530 )
531 },
532 }
533 }
534}
535
536impl StartsWith<Path> for Path {
537 fn starts_with(&self, prefix: &Path) -> bool {
538 let this = self.to_relative();
539 let prefix = prefix.to_relative();
540 <Path as StartsWith<Path>>::starts_with_exactly(this, prefix)
541 }
542
543 #[inline]
544 fn starts_with_exactly(&self, prefix: &Path) -> bool {
545 let mut components = self.components();
546 for prefix_component in prefix.components() {
547 let prefix_component = prefix_component.expect("invalid prefix path");
556 match (components.next(), prefix_component) {
557 (Some(Ok(PathComponent::Root)), PathComponent::Root) => {},
558 (Some(Ok(c @ PathComponent::Normal(_))), pc @ PathComponent::Normal(_)) => {
559 if c.as_str() != pc.as_str() {
560 return false;
561 }
562 },
563 (Some(Ok(_) | Err(_)) | None, _) => return false,
564 }
565 }
566 true
567 }
568}
569
570impl PartialEq<str> for Path {
571 fn eq(&self, other: &str) -> bool {
572 &self.inner == other
573 }
574}
575
576impl PartialEq<PathBuf> for Path {
577 fn eq(&self, other: &PathBuf) -> bool {
578 &self.inner == other.inner.as_str()
579 }
580}
581
582impl PartialEq<&PathBuf> for Path {
583 fn eq(&self, other: &&PathBuf) -> bool {
584 &self.inner == other.inner.as_str()
585 }
586}
587
588impl PartialEq<Path> for PathBuf {
589 fn eq(&self, other: &Path) -> bool {
590 self.inner.as_str() == &other.inner
591 }
592}
593
594impl PartialEq<&Path> for Path {
595 fn eq(&self, other: &&Path) -> bool {
596 self.inner == other.inner
597 }
598}
599
600impl PartialEq<alloc::boxed::Box<Path>> for Path {
601 fn eq(&self, other: &alloc::boxed::Box<Path>) -> bool {
602 self.inner == other.inner
603 }
604}
605
606impl PartialEq<alloc::rc::Rc<Path>> for Path {
607 fn eq(&self, other: &alloc::rc::Rc<Path>) -> bool {
608 self.inner == other.inner
609 }
610}
611
612impl PartialEq<alloc::sync::Arc<Path>> for Path {
613 fn eq(&self, other: &alloc::sync::Arc<Path>) -> bool {
614 self.inner == other.inner
615 }
616}
617
618impl PartialEq<Cow<'_, Path>> for Path {
619 fn eq(&self, other: &Cow<'_, Path>) -> bool {
620 self.inner == other.as_ref().inner
621 }
622}
623
624impl fmt::Display for Path {
625 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
626 f.write_str(&self.inner)
627 }
628}
629
630#[cfg(test)]
631mod tests {
632 use super::*;
633
634 #[test]
635 fn test_path_split_with_quotes_and_large_components() -> Result<(), PathError> {
636 let path = Path::new(
637 "::\"miden:counter-contract/counter-contract@0.1.0\"::counter_contract::_RNvXsg_NtNtCseaniv3neBPi_10miden_base5types7storageINtB5_10StorageMapNtNtCscxjsWiPtnzN_11miden_field4word4WordNtNtB19_10wasm_miden4FeltEINtNtCs3NSMmx5ugmE_4core7convert4FromNtNtNtCs52sTGLXFK3W_14miden_base_sys8bindings5types13StorageSlotIdE4fromCsanZ5gV8dMQ0_16counter_contract",
638 );
639 let canonicalized = path.canonicalize()?;
640
641 assert_eq!(canonicalized.as_path(), path);
642 Ok(())
643 }
644
645 #[test]
646 fn test_canonicalize_path_identity() -> Result<(), PathError> {
647 let path = Path::new("foo::bar");
648 let canonicalized = path.canonicalize()?;
649
650 assert_eq!(canonicalized.as_path(), path);
651 Ok(())
652 }
653
654 #[test]
655 fn test_canonicalize_path_kernel_is_absolute() -> Result<(), PathError> {
656 let path = Path::new("$kernel::bar");
657 let canonicalized = path.canonicalize()?;
658
659 let expected = Path::new("::$kernel::bar");
660 assert_eq!(canonicalized.as_path(), expected);
661 Ok(())
662 }
663
664 #[test]
665 fn test_canonicalize_path_exec_is_absolute() -> Result<(), PathError> {
666 let path = Path::new("$exec::$main");
667 let canonicalized = path.canonicalize()?;
668
669 let expected = Path::new("::$exec::$main");
670 assert_eq!(canonicalized.as_path(), expected);
671 Ok(())
672 }
673
674 #[test]
675 fn test_to_absolute_rejects_invalid_absolute_path_component() {
676 let source = alloc::format!("::{}", "a".repeat(Path::MAX_COMPONENT_LENGTH + 1));
677 let err = Path::new(&source)
678 .to_absolute()
679 .expect_err("absolute paths must still validate their components");
680
681 assert!(matches!(
682 err,
683 PathError::InvalidComponent(crate::ast::IdentError::InvalidLength { .. })
684 ));
685 }
686
687 #[test]
688 fn test_to_absolute_rejects_oversized_absolute_result() {
689 let source = alloc::format!("{}aa", "a::".repeat(21_844));
690 assert_eq!(source.len(), (u16::MAX as usize) - 1);
691
692 let err = Path::new(&source)
693 .to_absolute()
694 .expect_err("adding the absolute path prefix must preserve the path length bound");
695
696 assert!(matches!(err, PathError::TooLong { max } if max == u16::MAX as usize));
697 }
698
699 #[test]
700 fn test_canonicalize_path_remove_unnecessary_quoting() -> Result<(), PathError> {
701 let path = Path::new("foo::\"bar\"");
702 let canonicalized = path.canonicalize()?;
703
704 let expected = Path::new("foo::bar");
705 assert_eq!(canonicalized.as_path(), expected);
706 Ok(())
707 }
708
709 #[test]
710 fn test_canonicalize_path_preserve_necessary_quoting() -> Result<(), PathError> {
711 let path = Path::new("foo::\"bar::baz\"");
712 let canonicalized = path.canonicalize()?;
713
714 assert_eq!(canonicalized.as_path(), path);
715 Ok(())
716 }
717
718 #[test]
719 fn test_canonicalize_path_add_required_quoting_to_components_without_delimiter()
720 -> Result<(), PathError> {
721 let path = Path::new("foo::$bar");
722 let canonicalized = path.canonicalize()?;
723
724 let expected = Path::new("foo::\"$bar\"");
725 assert_eq!(canonicalized.as_path(), expected);
726 Ok(())
727 }
728
729 #[test]
730 fn test_canonicalize_path_rejects_canonical_result_longer_than_u16_max() {
731 let component = alloc::format!("{}-", "a".repeat(254));
732 let mut source = alloc::string::String::new();
733 for i in 0..255 {
734 if i > 0 {
735 source.push_str("::");
736 }
737 source.push_str(&component);
738 }
739
740 let path = Path::validate(&source).expect("source path must be pre-canonicalization valid");
741 let err = path.canonicalize().expect_err(
742 "canonicalization must reject paths that exceed the serialization length bound",
743 );
744
745 assert!(matches!(err, PathError::TooLong { max } if max == u16::MAX as usize));
746 }
747}