manifest: add Link::to_file

Summary:
Removing the contructor for File that takes dependency on
tree implementation details.

Reviewed By: kulshrax

Differential Revision: D18822858

fbshipit-source-id: aed33b146dfdbdb23721a0f6e96977025d71a627
This commit is contained in:
Stefan Filip 2019-12-06 11:47:59 -08:00 committed by Facebook Github Bot
parent b21d605d74
commit 4074083986
3 changed files with 15 additions and 17 deletions

View File

@ -16,8 +16,6 @@ use anyhow::Result;
use types::{HgId, RepoPath, RepoPathBuf};
use crate::tree::Link;
/// Manifest describes a mapping between file path ([`String`]) and file metadata ([`FileMetadata`]).
/// Fundamentally it is just a Map<file_path, file_metadata>.
///
@ -82,15 +80,6 @@ impl File {
pub fn new(path: RepoPathBuf, meta: FileMetadata) -> Self {
Self { path, meta }
}
/// Create a file record for a `Link`, failing if the link
/// refers to a directory rather than a file.
pub fn from_link(link: &Link, path: RepoPathBuf) -> Option<Self> {
match link {
Link::Leaf(meta) => Some(Self::new(path, *meta)),
_ => None,
}
}
}
impl From<(RepoPathBuf, FileMetadata)> for File {

View File

@ -10,10 +10,10 @@ use std::{collections::BTreeMap, sync::Arc};
use anyhow::{bail, format_err, Result};
use once_cell::sync::OnceCell;
use types::{HgId, PathComponentBuf, RepoPath};
use types::{HgId, PathComponentBuf, RepoPath, RepoPathBuf};
use crate::tree::{store, store::InnerStore};
use crate::{FileMetadata, FsNode};
use crate::{File, FileMetadata, FsNode};
/// `Link` describes the type of nodes that tree manifest operates on.
#[derive(Clone, Debug)]
@ -83,6 +83,15 @@ impl Link {
Link::Durable(durable) => FsNode::Directory(Some(durable.hgid)),
}
}
/// Create a file record for a `Link`, failing if the link
/// refers to a directory rather than a file.
pub fn to_file(&self, path: RepoPathBuf) -> Option<File> {
match self {
Leaf(metadata) => Some(File::new(path, *metadata)),
_ => None,
}
}
}
impl DurableEntry {

View File

@ -715,7 +715,7 @@ impl<'a> Directory<'a> {
path.push(name.as_ref());
match link {
Link::Leaf(_) => {
files.push(File::from_link(link, path).expect("leaf node must be a valid file"))
files.push(link.to_file(path).expect("leaf node must be a valid file"))
}
Link::Ephemeral(_) | Link::Durable(_) => dirs.push(
Directory::from_link(link, path).expect("inner node must be a valid directory"),
@ -1573,7 +1573,7 @@ mod tests {
let path = repo_path_buf("test/leaf");
let leaf = Link::Leaf(meta.clone());
let file = File::from_link(&leaf, path.clone()).unwrap();
let file = leaf.to_file(path.clone()).unwrap();
let expected = File {
path: path.clone(),
@ -1583,11 +1583,11 @@ mod tests {
// Attempting to use a directory link should fail.
let ephemeral = Link::ephemeral();
let _file = File::from_link(&ephemeral, path.clone());
let _file = ephemeral.to_file(path.clone());
// Durable link should result in a directory.
let durable = Link::durable(hgid("a"));
let file = File::from_link(&durable, path.clone());
let file = durable.to_file(path.clone());
assert!(file.is_none());
}