Skip to content

Commit

Permalink
Resolve mounted exports (#28621)
Browse files Browse the repository at this point in the history
GitOrigin-RevId: eef704a1cbb1508861ff37c607d56368d2059c49
  • Loading branch information
sujayakar authored and Convex, Inc. committed Aug 5, 2024
1 parent 47e1b06 commit d09293f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
22 changes: 13 additions & 9 deletions sync_types/src/module_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use std::{
str::FromStr,
};

use crate::path::check_valid_path_component;
use crate::path::{
check_valid_path_component,
PathComponent,
};

pub const SYSTEM_UDF_DIR: &str = "_system";
pub const DEPS_DIR: &str = "_deps";
Expand Down Expand Up @@ -38,14 +41,15 @@ impl ModulePath {
&self.path
}

pub fn components(&self) -> impl Iterator<Item = &str> {
self.path
.components()
.map(|component| match component {
Component::Normal(c) => c,
c => panic!("Unexpected component {c:?}"),
})
.map(|c| c.to_str().expect("Non-unicode data in module path?"))
pub fn components(&self) -> impl Iterator<Item = PathComponent> + '_ {
self.path.components().map(|component| match component {
Component::Normal(c) => c
.to_str()
.expect("Non-unicode data in module path?")
.parse()
.expect("Invalid path component"),
c => panic!("Unexpected component {c:?}"),
})
}

/// Does a module live within the `_system/` directory?
Expand Down
57 changes: 56 additions & 1 deletion sync_types/src/path.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use crate::identifier::MAX_IDENTIFIER_LEN;
use std::{
ops::Deref,
str::FromStr,
};

use crate::{
identifier::MAX_IDENTIFIER_LEN,
FunctionName,
};

pub fn check_valid_path_component(s: &str) -> anyhow::Result<()> {
if s.len() > MAX_IDENTIFIER_LEN {
Expand All @@ -21,3 +29,50 @@ pub fn check_valid_path_component(s: &str) -> anyhow::Result<()> {
}
Ok(())
}

#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct PathComponent(String);

impl FromStr for PathComponent {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
check_valid_path_component(s)?;
Ok(Self(s.to_owned()))
}
}

impl Deref for PathComponent {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl From<PathComponent> for String {
fn from(p: PathComponent) -> Self {
p.0
}
}

impl From<FunctionName> for PathComponent {
fn from(function_name: FunctionName) -> Self {
function_name
.parse()
.expect("FunctionName isn't a valid PathComponent")
}
}

#[cfg(any(test, feature = "testing"))]
impl proptest::arbitrary::Arbitrary for PathComponent {
type Parameters = ();
type Strategy = proptest::strategy::BoxedStrategy<Self>;

fn arbitrary_with((): Self::Parameters) -> Self::Strategy {
use proptest::prelude::*;
"_?[a-zA-Z0-9_]{1,60}(\\.js)?"
.prop_filter_map("Invalid path component", |s| s.parse().ok())
.boxed()
}
}

0 comments on commit d09293f

Please sign in to comment.