Add the ability to read the dependencies of a crate

This commit is contained in:
Gwilym Kuiper 2022-08-04 21:53:18 +01:00
parent 4277f07ec0
commit c3f803206a

View file

@ -20,6 +20,8 @@ pub enum Error {
PublishCrate,
Poll,
CrateVersion,
ReadingDependencies,
CargoToml,
}
pub fn command() -> clap::Command<'static> {
@ -117,11 +119,7 @@ fn get_url_to_poll(crate_name: &str) -> String {
}
fn read_cargo_toml_version(folder: &Path) -> Result<String, Error> {
let cargo_toml_contents =
fs::read_to_string(folder.join("Cargo.toml")).map_err(|_| Error::CrateVersion)?;
let cargo_toml: Document = cargo_toml_contents
.parse()
.map_err(|_| Error::CrateVersion)?;
let cargo_toml = read_cargo_toml(folder)?;
let version_value = cargo_toml["package"]["version"]
.as_value()
@ -132,6 +130,33 @@ fn read_cargo_toml_version(folder: &Path) -> Result<String, Error> {
Ok(version_value.to_owned())
}
fn get_agb_dependencies(folder: &Path) -> Result<Vec<String>, Error> {
let cargo_toml = read_cargo_toml(folder)?;
let dependencies = cargo_toml["dependencies"]
.as_table()
.ok_or(Error::ReadingDependencies)?
.get_values();
let mut result = vec![];
for (key, _) in dependencies {
let dep = key[0].get();
if dep.starts_with("agb") {
result.push(dep.replace('_', "-"))
}
}
Ok(result)
}
fn read_cargo_toml(folder: &Path) -> Result<Document, Error> {
let cargo_toml_contents =
fs::read_to_string(folder.join("Cargo.toml")).map_err(|_| Error::CargoToml)?;
let cargo_toml: Document = cargo_toml_contents.parse().map_err(|_| Error::CargoToml)?;
Ok(cargo_toml)
}
#[cfg(test)]
mod test {
use super::*;
@ -170,4 +195,21 @@ mod test {
assert_eq!(my_version, "0.1.0");
Ok(())
}
#[test]
fn should_detect_dependencies() -> Result<(), Error> {
let root_directory = find_agb_root_directory()?;
let deps = get_agb_dependencies(&root_directory.join("agb"))?;
assert_eq!(
deps,
&[
"agb-image-converter",
"agb-sound-converter",
"agb-macros",
"agb-fixnum"
]
);
Ok(())
}
}