Skip to content

Commit

Permalink
feat: make spec.yaml files optional (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
holtgrewe authored Jul 22, 2024
1 parent 625c2a4 commit c768822
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,9 @@ struct Server {
#[derive(Debug, Subcommand, Clone)]
enum ServerCommands {
/// "run" sub command.
Run(server::run::Args),
Run(Box<server::run::Args>),
/// Dump the schema.
Schema(crate::server::schema::Args),
Schema(Box<crate::server::schema::Args>),
}

pub fn main() -> Result<(), anyhow::Error> {
Expand Down
25 changes: 19 additions & 6 deletions src/server/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,20 +264,23 @@ pub struct WithVersionSpec<T: std::fmt::Debug> {
/// The actual data.
pub data: T,
/// Version specification.
pub version_spec: pbs::common::versions::VersionSpec,
pub version_spec: Option<pbs::common::versions::VersionSpec>,
}

impl<T> WithVersionSpec<T>
where
T: std::fmt::Debug,
{
/// Construct with the given data and path to specification YAML file.
pub fn from_data_and_path<P>(data: T, path: P) -> Result<Self, anyhow::Error>
pub fn from_data_and_path<P>(data: T, path: &Option<P>) -> Result<Self, anyhow::Error>
where
P: AsRef<Path>,
{
let version_spec: pbs::common::versions::VersionSpec =
versions::schema::VersionSpec::from_path(path)?.into();
let version_spec: Option<pbs::common::versions::VersionSpec> = path
.as_ref()
.map(versions::schema::VersionSpec::from_path)
.transpose()?
.map(|version_spec| version_spec.into());
Ok(Self { data, version_spec })
}
}
Expand Down Expand Up @@ -483,11 +486,16 @@ pub fn run(args_common: &common::cli::Args, args: &Args) -> Result<(), anyhow::E
.parent()
.ok_or_else(|| anyhow::anyhow!("cannot get parent directory of path {}", path_genes))?
.join("spec.yaml");
let path_buf = path_buf.exists().then_some(path_buf);
data.genes = Some(
WithVersionSpec::from_data_and_path(gene_info_db, &path_buf).map_err(|e| {
anyhow::anyhow!(
"problem loading gene info spec from {}: {}",
path_buf.display(),
if let Some(path_buf) = path_buf.as_ref() {
format!("{}", path_buf.display())
} else {
"None".to_string()
},
e
)
})?,
Expand Down Expand Up @@ -564,13 +572,18 @@ pub fn run(args_common: &common::cli::Args, args: &Args) -> Result<(), anyhow::E
anyhow::anyhow!("cannot get parent directory of path {}", path_rocksdb)
})?
.join("spec.yaml");
let spec_path = spec_path.exists().then_some(spec_path);
let name = db_info.name;
data.db_infos[genome_release][name] = Some(db_info);
data.annos[genome_release][name] = Some(
WithVersionSpec::from_data_and_path(db, &spec_path).map_err(|e| {
anyhow::anyhow!(
"problem loading gene info spec from {}: {}",
spec_path.display(),
if let Some(spec_path) = spec_path.as_ref() {
format!("{}", spec_path.display())
} else {
"None".to_string()
},
e
)
})?,
Expand Down
26 changes: 13 additions & 13 deletions src/server/run/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub mod schema {
pub version: String,
}

impl Into<pbs::common::versions::CreatedFrom> for CreatedFrom {
fn into(self) -> pbs::common::versions::CreatedFrom {
let Self { name, version } = self;
impl From<CreatedFrom> for pbs::common::versions::CreatedFrom {
fn from(val: CreatedFrom) -> Self {
let CreatedFrom { name, version } = val;
pbs::common::versions::CreatedFrom { name, version }
}
}
Expand Down Expand Up @@ -81,16 +81,16 @@ pub mod schema {
let full_path = p.as_ref().to_str().ok_or_else(|| {
anyhow::anyhow!("problem converting path to string: {:?}", p.as_ref())
})?;
let yaml_str = std::fs::read_to_string(&full_path)
let yaml_str = std::fs::read_to_string(full_path)
.map_err(|e| anyhow::anyhow!("problem reading file {}: {}", &full_path, e))?;
serde_yaml::from_str(&yaml_str)
.map_err(|e| anyhow::anyhow!("problem deserializing {}: {}", full_path, e))
}
}

impl Into<pbs::common::versions::VersionSpec> for VersionSpec {
fn into(self) -> pbs::common::versions::VersionSpec {
let Self {
impl From<VersionSpec> for pbs::common::versions::VersionSpec {
fn from(val: VersionSpec) -> Self {
let VersionSpec {
identifier,
title,
creator,
Expand All @@ -102,7 +102,7 @@ pub mod schema {
description,
source,
created_from,
} = self;
} = val;
pbs::common::versions::VersionSpec {
identifier,
title,
Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct AnnoVersionInfo {
/// Database name.
pub database: AnnoDb,
/// Version information of the database.
pub version_spec: VersionSpec,
pub version_spec: Option<VersionSpec>,
}

/// Version information for databases in a given release.
Expand Down Expand Up @@ -177,8 +177,8 @@ async fn handle(
for (anno_db, with_version) in anno_dbs {
if let Some(with_version) = with_version.as_ref() {
version_infos.push(AnnoVersionInfo {
database: anno_db.clone(),
version_spec: with_version.version_spec.clone().into(),
database: anno_db,
version_spec: with_version.version_spec.clone(),
});
}
}
Expand All @@ -193,7 +193,7 @@ async fn handle(
.as_ref()
.genes
.as_ref()
.map(|genes| genes.version_spec.clone()),
.and_then(|genes| genes.version_spec.clone()),
annos,
};

Expand All @@ -216,7 +216,7 @@ pub mod test {
crate::common::set_snapshot_suffix!("{}", &name);

let full_path = format!("tests/server/annonars/{}/spec.yaml", &name);
let spec = super::schema::VersionSpec::from_path(&full_path)?;
let spec = super::schema::VersionSpec::from_path(full_path)?;
insta::assert_yaml_snapshot!(&spec);

let proto_spec: crate::pbs::common::versions::VersionSpec = spec.into();
Expand Down

0 comments on commit c768822

Please sign in to comment.