|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Repository layout utilities. |
| 19 | +//! |
| 20 | +//! This module provides a small helper (`RepoLayout`) that encapsulates |
| 21 | +//! knowledge about the DataFusion repository structure, in particular |
| 22 | +//! where example groups are located relative to the repository root. |
| 23 | +
|
| 24 | +use std::path::{Path, PathBuf}; |
| 25 | + |
| 26 | +use datafusion::error::{DataFusionError, Result}; |
| 27 | + |
| 28 | +/// Describes the layout of a DataFusion repository. |
| 29 | +/// |
| 30 | +/// This type centralizes knowledge about where example-related |
| 31 | +/// directories live relative to the repository root. |
| 32 | +#[derive(Debug, Clone)] |
| 33 | +pub struct RepoLayout { |
| 34 | + root: PathBuf, |
| 35 | +} |
| 36 | + |
| 37 | +impl From<&Path> for RepoLayout { |
| 38 | + fn from(path: &Path) -> Self { |
| 39 | + Self { |
| 40 | + root: path.to_path_buf(), |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl RepoLayout { |
| 46 | + /// Creates a layout from an explicit repository root. |
| 47 | + pub fn from_root(root: PathBuf) -> Self { |
| 48 | + Self { root } |
| 49 | + } |
| 50 | + |
| 51 | + /// Detects the repository root based on `CARGO_MANIFEST_DIR`. |
| 52 | + /// |
| 53 | + /// This is intended for use from binaries inside the workspace. |
| 54 | + pub fn detect() -> Result<Self> { |
| 55 | + let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 56 | + |
| 57 | + let root = manifest_dir.parent().ok_or_else(|| { |
| 58 | + DataFusionError::Execution( |
| 59 | + "CARGO_MANIFEST_DIR does not have a parent".to_string(), |
| 60 | + ) |
| 61 | + })?; |
| 62 | + |
| 63 | + Ok(Self { |
| 64 | + root: root.to_path_buf(), |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + /// Returns the repository root directory. |
| 69 | + pub fn root(&self) -> &Path { |
| 70 | + &self.root |
| 71 | + } |
| 72 | + |
| 73 | + /// Returns the `datafusion-examples/examples` directory. |
| 74 | + pub fn examples_root(&self) -> PathBuf { |
| 75 | + self.root.join("datafusion-examples").join("examples") |
| 76 | + } |
| 77 | + |
| 78 | + /// Returns the directory for a single example group. |
| 79 | + /// |
| 80 | + /// Example: `examples/udf` |
| 81 | + pub fn example_group_dir(&self, group: &str) -> PathBuf { |
| 82 | + self.examples_root().join(group) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod tests { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn detect_sets_non_empty_root() -> Result<()> { |
| 92 | + let layout = RepoLayout::detect()?; |
| 93 | + assert!(!layout.root().as_os_str().is_empty()); |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn examples_root_is_under_repo_root() -> Result<()> { |
| 99 | + let layout = RepoLayout::detect()?; |
| 100 | + let examples_root = layout.examples_root(); |
| 101 | + assert!(examples_root.starts_with(layout.root())); |
| 102 | + assert!(examples_root.ends_with("datafusion-examples/examples")); |
| 103 | + Ok(()) |
| 104 | + } |
| 105 | + |
| 106 | + #[test] |
| 107 | + fn example_group_dir_appends_group_name() -> Result<()> { |
| 108 | + let layout = RepoLayout::detect()?; |
| 109 | + let group_dir = layout.example_group_dir("foo"); |
| 110 | + assert!(group_dir.ends_with("datafusion-examples/examples/foo")); |
| 111 | + Ok(()) |
| 112 | + } |
| 113 | +} |
0 commit comments