1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use std::path::{Path, PathBuf};

use crate::Manifest;
#[cfg(all(feature = "toml", feature = "serde", feature = "thiserror"))]
use crate::TomlReadError;

/// A struct contains package manifest and its root path.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Package {
    /// Package manifest.
    manifest: Manifest,
    /// Package root path.
    path: PathBuf,
}

impl Package {
    /// Creates a package from the specified path.
    #[cfg(all(feature = "toml", feature = "serde", feature = "thiserror"))]
    pub fn from_path(path: PathBuf) -> Result<Self, TomlReadError> {
        Ok(Self {
            manifest: Manifest::from_package_path(&path)?,
            path,
        })
    }

    /// Creates a package from the manifest and package path.
    pub fn from_manifest_and_path(manifest: Manifest, path: PathBuf) -> Self {
        Self { manifest, path }
    }

    /// Returns a package manifest.
    pub fn manifest(&self) -> &Manifest {
        &self.manifest
    }

    /// Returns a package path.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns package relative readme path.
    pub fn relative_readme_path(&self) -> Option<&Path> {
        self.manifest.relative_readme_path(&self.path)
    }

    /// Returns package relative default readme path.
    pub fn default_relative_readme_path(&self) -> Option<&Path> {
        Manifest::default_readme_filename(&self.path)
    }
}