|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is dual-licensed under either the MIT license found in the |
| 5 | + * LICENSE-MIT file in the root directory of this source tree or the Apache |
| 6 | + * License, Version 2.0 found in the LICENSE-APACHE file in the root directory |
| 7 | + * of this source tree. You may select, at your option, one of the |
| 8 | + * above-listed licenses. |
| 9 | + */ |
| 10 | + |
| 11 | +use std::{fs, path::Path}; |
| 12 | + |
| 13 | +use anyhow::Context as _; |
| 14 | +use thiserror::Error; |
| 15 | +use utoipa::ToSchema; |
| 16 | + |
| 17 | +pub use crate::buck2::types::ProjectRelativePath; |
| 18 | + |
| 19 | +#[derive(Debug, PartialEq, Eq, Hash, Clone, serde::Serialize, serde::Deserialize, ToSchema)] |
| 20 | +pub enum Status<Path> { |
| 21 | + Modified(Path), |
| 22 | + Added(Path), |
| 23 | + Removed(Path), |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Error, Debug)] |
| 27 | +enum StatusParseError { |
| 28 | + #[error("Unexpected line format: {0}")] |
| 29 | + UnexpectedFormat(String), |
| 30 | + #[error("Unknown line prefix: {0}")] |
| 31 | + UnknownPrefix(String), |
| 32 | +} |
| 33 | + |
| 34 | +impl Status<ProjectRelativePath> { |
| 35 | + /// Creates a new Modified status from a file path string |
| 36 | + pub fn modified(path: &str) -> Self { |
| 37 | + Self::Modified(ProjectRelativePath::new(path)) |
| 38 | + } |
| 39 | + |
| 40 | + /// Creates a new Added status from a file path string |
| 41 | + pub fn added(path: &str) -> Self { |
| 42 | + Self::Added(ProjectRelativePath::new(path)) |
| 43 | + } |
| 44 | + |
| 45 | + /// Creates a new Removed status from a file path string |
| 46 | + pub fn removed(path: &str) -> Self { |
| 47 | + Self::Removed(ProjectRelativePath::new(path)) |
| 48 | + } |
| 49 | + |
| 50 | + fn from_str(value: &str) -> anyhow::Result<Self> { |
| 51 | + let mut it = value.chars(); |
| 52 | + let typ = it.next(); |
| 53 | + if it.next() != Some(' ') { |
| 54 | + return Err(StatusParseError::UnexpectedFormat(value.to_owned()).into()); |
| 55 | + } |
| 56 | + let path = ProjectRelativePath::new(it.as_str()); |
| 57 | + match typ { |
| 58 | + Some('A') => Ok(Self::Added(path)), |
| 59 | + Some('M') => Ok(Self::Modified(path)), |
| 60 | + Some('R') => Ok(Self::Removed(path)), |
| 61 | + Some('D') => Ok(Self::Removed(path)), // used by jujutsu |
| 62 | + _ => Err(StatusParseError::UnknownPrefix(value.to_owned()).into()), |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl<Path> Status<Path> { |
| 68 | + pub fn get(&self) -> &Path { |
| 69 | + match self { |
| 70 | + Status::Modified(x) => x, |
| 71 | + Status::Added(x) => x, |
| 72 | + Status::Removed(x) => x, |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub fn map<'a, T: 'a>(&'a self, f: impl FnOnce(&'a Path) -> T) -> Status<T> { |
| 77 | + match self { |
| 78 | + Status::Modified(x) => Status::Modified(f(x)), |
| 79 | + Status::Added(x) => Status::Added(f(x)), |
| 80 | + Status::Removed(x) => Status::Removed(f(x)), |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pub fn try_map<T, E>(&self, f: impl FnOnce(&Path) -> Result<T, E>) -> Result<Status<T>, E> { |
| 85 | + Ok(match self { |
| 86 | + Status::Modified(x) => Status::Modified(f(x)?), |
| 87 | + Status::Added(x) => Status::Added(f(x)?), |
| 88 | + Status::Removed(x) => Status::Removed(f(x)?), |
| 89 | + }) |
| 90 | + } |
| 91 | + |
| 92 | + pub fn into_map<T>(self, f: impl FnOnce(Path) -> T) -> Status<T> { |
| 93 | + match self { |
| 94 | + Status::Modified(x) => Status::Modified(f(x)), |
| 95 | + Status::Added(x) => Status::Added(f(x)), |
| 96 | + Status::Removed(x) => Status::Removed(f(x)), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + pub fn into_try_map<T, E>(self, f: impl FnOnce(Path) -> Result<T, E>) -> Result<Status<T>, E> { |
| 101 | + Ok(match self { |
| 102 | + Status::Modified(x) => Status::Modified(f(x)?), |
| 103 | + Status::Added(x) => Status::Added(f(x)?), |
| 104 | + Status::Removed(x) => Status::Removed(f(x)?), |
| 105 | + }) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +pub fn read_status(path: &Path) -> anyhow::Result<Vec<Status<ProjectRelativePath>>> { |
| 110 | + parse_status( |
| 111 | + &fs::read_to_string(path).with_context(|| format!("When reading `{}`", path.display()))?, |
| 112 | + ) |
| 113 | +} |
| 114 | + |
| 115 | +fn parse_status(data: &str) -> anyhow::Result<Vec<Status<ProjectRelativePath>>> { |
| 116 | + data.lines() |
| 117 | + .map(Status::from_str) |
| 118 | + .collect::<anyhow::Result<Vec<_>>>() |
| 119 | +} |
| 120 | + |
| 121 | +#[cfg(test)] |
| 122 | +mod tests { |
| 123 | + use super::*; |
| 124 | + |
| 125 | + #[test] |
| 126 | + fn test_status() { |
| 127 | + let src = r#" |
| 128 | +M proj/foo.rs |
| 129 | +M bar.rs |
| 130 | +A baz/file.txt |
| 131 | +R quux.js |
| 132 | +"#; |
| 133 | + assert_eq!( |
| 134 | + parse_status(&src[1..]).unwrap(), |
| 135 | + vec![ |
| 136 | + Status::Modified(ProjectRelativePath::new("proj/foo.rs")), |
| 137 | + Status::Modified(ProjectRelativePath::new("bar.rs")), |
| 138 | + Status::Added(ProjectRelativePath::new("baz/file.txt")), |
| 139 | + Status::Removed(ProjectRelativePath::new("quux.js")) |
| 140 | + ] |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + #[test] |
| 145 | + fn test_status_error() { |
| 146 | + assert!(parse_status("X quux.js").is_err()); |
| 147 | + assert!(parse_status("notaline").is_err()); |
| 148 | + assert!(parse_status("not a line").is_err()); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn test_status_constructors() { |
| 153 | + let modified = Status::modified("foo/modified.rs"); |
| 154 | + let modified_parsed = Status::from_str("M foo/modified.rs").unwrap(); |
| 155 | + assert!(matches!(modified, Status::Modified(_))); |
| 156 | + assert_eq!(modified, modified_parsed); |
| 157 | + assert_eq!(modified.get().as_str(), "foo/modified.rs"); |
| 158 | + |
| 159 | + let added = Status::added("foo/added.rs"); |
| 160 | + let added_parsed = Status::from_str("A foo/added.rs").unwrap(); |
| 161 | + assert!(matches!(added, Status::Added(_))); |
| 162 | + assert_eq!(added, added_parsed); |
| 163 | + assert_eq!(added.get().as_str(), "foo/added.rs"); |
| 164 | + |
| 165 | + let removed = Status::removed("foo/removed.rs"); |
| 166 | + let removed_parsed = Status::from_str("R foo/removed.rs").unwrap(); |
| 167 | + assert!(matches!(removed, Status::Removed(_))); |
| 168 | + assert_eq!(removed, removed_parsed); |
| 169 | + assert_eq!(removed.get().as_str(), "foo/removed.rs"); |
| 170 | + } |
| 171 | +} |
0 commit comments