Repository: typst/typst
File: crates/typst-kit/src/package.rs (Rust)

Code before:
    fn lazy_deser<T>(self) -> LazyDeser<T, Self> {
        LazyDeser { inner: self, _phantom: PhantomData }
    }
}

impl<'a, T, I> Iterator for LazyDeser<T, I>
where
    T: DeserializeOwned,
    I: Iterator<Item = &'a serde_json::Value>,
{
    type Item = Result<T, serde_json::Error>;

    fn next(&mut self) -> Option<Self::Item> {
        let inner = self.inner.next()?;
        Some(serde_json::from_value(inner.clone()))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lazy_deser_index() {
        let storage = PackageStorage::with_index(
            None,
            None,
            Downloader::new("typst/test"),
            OnceCell::with_value(vec![
                serde_json::json!({
                    "name": "charged-ieee",
                    "version": "0.1.0",
                    "entrypoint": "lib.typ",
                }),
                serde_json::json!({
                    "name": "unequivocal-ams",
                    // This version number is currently not valid, so this package
                    // can't be parsed.
                    "version": "0.2.0-dev",
                    "entrypoint": "lib.typ",
                }),
            ]),
        );

        let ieee_version = storage.determine_latest_version(&VersionlessPackageSpec {
            namespace: "preview".into(),
            name: "charged-ieee".into(),
        });
        assert_eq!(ieee_version, Ok(PackageVersion { major: 0, minor: 1, patch: 0 }));

        let ams_version = storage.determine_latest_version(&VersionlessPackageSpec {

Review comment (performance):
I left the error message as is for now to not introduce too much complexity, but maybe it would be nice to have a more specific one in this scenario, such as "a more recent version of Typst is required to use @preview/unequivocal-ams"? This would require the assumption that the `name` field will always be a string in the index.

I don't know what should be shown in case a more recent version maybe exists but the compiler can't parse it so it can't tell. Should it show a warning? An error? Silently ignore this version as it currently does?

Code after:
                    "version": "0.1.0",
                    "entrypoint": "lib.typ",
                }),
                serde_json::json!({
                    "name": "unequivocal-ams",
                    // This version number is currently not valid, so this package
                    // can't be parsed.
                    "version": "0.2.0-dev",
                    "entrypoint": "lib.typ",
                }),
            ]),
        );

        let ieee_version = storage.determine_latest_version(&VersionlessPackageSpec {
            namespace: "preview".into(),
            name: "charged-ieee".into(),
        });
        assert_eq!(ieee_version, Ok(PackageVersion { major: 0, minor: 1, patch: 0 }));

        let ams_version = storage.determine_latest_version(&VersionlessPackageSpec {
            namespace: "preview".into(),
            name: "unequivocal-ams".into(),
        });
        assert_eq!(
            ams_version,
            Err("failed to find package @preview/unequivocal-ams".into())
        )
    }
}