File size: 5,043 Bytes
1e92f2d | 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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | use std::{
any::Any,
fmt::{Display, Formatter},
hash::Hash,
};
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexSet, InvalidationReason, InvalidationReasonKind, util::StaticOrArc};
/// Invalidation was caused by a file change detected by the file watcher
#[derive(PartialEq, Eq, Hash)]
pub(crate) struct WatchChange {
pub path: String,
}
impl InvalidationReason for WatchChange {
fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
Some(StaticOrArc::Static(&WATCH_CHANGE_KIND))
}
}
impl Display for WatchChange {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} changed", self.path)
}
}
/// Invalidation kind for [WatchChange]
#[derive(PartialEq, Eq, Hash)]
struct WatchChangeKind;
static WATCH_CHANGE_KIND: WatchChangeKind = WatchChangeKind;
impl InvalidationReasonKind for WatchChangeKind {
fn fmt(
&self,
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
write!(
f,
"{} files changed ({}, ...)",
reasons.len(),
(first_reason as &dyn Any)
.downcast_ref::<WatchChange>()
.unwrap()
.path
)
}
}
/// Invalidation was caused by a directory starting to watch from which was read
/// before.
#[derive(PartialEq, Eq, Hash, Clone)]
pub(crate) struct WatchStart {
pub name: RcStr,
pub path: RcStr,
}
impl InvalidationReason for WatchStart {
fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
Some(StaticOrArc::Static(&WATCH_START_KIND))
}
}
impl Display for WatchStart {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "started watching {} in {}", self.path, self.name)
}
}
/// Invalidation kind for [WatchStart]
#[derive(PartialEq, Eq, Hash)]
struct WatchStartKind;
static WATCH_START_KIND: WatchStartKind = WatchStartKind;
impl InvalidationReasonKind for WatchStartKind {
fn fmt(
&self,
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
let example = (first_reason as &dyn Any)
.downcast_ref::<WatchStart>()
.unwrap();
write!(
f,
"{} items started watching (e.g. {} in {})",
reasons.len(),
example.path,
example.name
)
}
}
/// Invalidation was caused by initialization of a project or filesystem.
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct Initialize {
pub path: RcStr,
}
impl InvalidationReason for Initialize {
fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
Some(StaticOrArc::Static(&INITIALIZE_KIND))
}
}
impl Display for Initialize {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(
f,
"initialized project or filesystem with path {}",
self.path
)
}
}
/// [Invalidation kind][InvalidationReasonKind] for [`Initialize`].
#[derive(PartialEq, Eq, Hash)]
struct InitializeKind;
static INITIALIZE_KIND: InitializeKind = InitializeKind;
impl InvalidationReasonKind for InitializeKind {
fn fmt(
&self,
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
let example = (first_reason as &dyn Any)
.downcast_ref::<Initialize>()
.unwrap();
write!(
f,
"{} items invalidated as part of project or filesystem initialization ({}, ...)",
reasons.len(),
example.path,
)
}
}
/// Invalidation was caused by a write operation on the filesystem
#[derive(PartialEq, Eq, Hash)]
pub(crate) struct Write {
pub path: String,
}
impl InvalidationReason for Write {
fn kind(&self) -> Option<StaticOrArc<dyn InvalidationReasonKind>> {
Some(StaticOrArc::Static(&WRITE_KIND))
}
}
impl Display for Write {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{} written", self.path)
}
}
/// Invalidation kind for [Write]
#[derive(PartialEq, Eq, Hash)]
struct WriteKind;
static WRITE_KIND: WriteKind = WriteKind;
impl InvalidationReasonKind for WriteKind {
fn fmt(
&self,
reasons: &FxIndexSet<StaticOrArc<dyn InvalidationReason>>,
f: &mut Formatter<'_>,
) -> std::fmt::Result {
let first_reason: &dyn InvalidationReason = &*reasons[0];
write!(
f,
"{} files written ({}, ...)",
reasons.len(),
(first_reason as &dyn Any)
.downcast_ref::<Write>()
.unwrap()
.path
)
}
}
|