Repository: typst/typst
File: crates/typst-library/src/loading/mod.rs (Rust)

Code before:
/// Loads data from a path or provided bytes.
pub trait Load {
    /// Bytes or a list of bytes (if there are multiple sources).
    type Output;

    /// Load the bytes.
    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output>;
}

impl Load for Spanned<DataSource> {
    type Output = Loaded;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
        self.as_ref().load(world)
    }
}

impl Load for Spanned<&DataSource> {
    type Output = Loaded;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
        match &self.v {
            DataSource::Path(path) => {
                let file_id = self.span.resolve_path(path).at(self.span)?;
                let data = world.file(file_id).at(self.span).map_err(|mut diags| {
                    if path.starts_with("http://") || path.starts_with("https://") {
                        for diag in diags.make_mut() {
                            diag.hint("network access is not supported");
                        }
                    }
                    diags
                })?;
                let source = Spanned::new(LoadSource::Path(file_id), self.span);
                Ok(Loaded::new(source, data))
            }
            DataSource::Bytes(data) => {
                let source = Spanned::new(LoadSource::Bytes, self.span);
                Ok(Loaded::new(source, data.clone()))
            }
        }
    }
}

impl Load for Spanned<OneOrMultiple<DataSource>> {
    type Output = Vec<Loaded>;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
        self.as_ref().load(world)
    }
}


Review comment (suggestion):
```suggestion
                    const PROTOCOLS: &[&str] = &["http", "https"];
                    if PROTOCOLS.iter().any(|p| path.starts_with(format!("{p}://"))) {
```

Code after:
    /// Bytes or a list of bytes (if there are multiple sources).
    type Output;

    /// Load the bytes.
    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output>;
}

impl Load for Spanned<DataSource> {
    type Output = Loaded;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
        self.as_ref().load(world)
    }
}

impl Load for Spanned<&DataSource> {
    type Output = Loaded;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {
        match self.v {
            DataSource::Path(path) => {
                let resolved =
                    path.resolve_if_some(self.span.id()).at(self.span)?.intern();
                let data = world
                    .file(resolved)
                    .map_err(|error| {
                        let mut hinted = HintedString::from(error);
                        if let PathOrStr::Str(string) = &path
                            && (string.as_str().starts_with("http://")
                                || string.as_str().starts_with("https://"))
                        {
                            hinted.hint("network access is not supported");
                        }
                        hinted
                    })
                    .at(self.span)?;
                let source = Spanned::new(LoadSource::Path(resolved), self.span);
                Ok(Loaded::new(source, data))
            }
            DataSource::Bytes(data) => {
                let source = Spanned::new(LoadSource::Bytes, self.span);
                Ok(Loaded::new(source, data.clone()))
            }
        }
    }
}

impl Load for Spanned<OneOrMultiple<DataSource>> {
    type Output = Vec<Loaded>;

    fn load(&self, world: Tracked<dyn World + '_>) -> SourceResult<Self::Output> {