Skip to main content

flm_core/progress/
kind.rs

1//! The kind of a row and the shared texts of a row.
2
3use std::borrow::Borrow;
4use std::fmt;
5use std::hash::{Hash, Hasher};
6use std::ops::Deref;
7use std::sync::Arc;
8
9/// A text of a row (its kind, title or label), shared and never copied when
10/// a row is read.
11///
12/// A `SharedText` holds either a `&'static str` (a kind or a title declared as
13/// a constant) or an `Arc<str>` (a label, or a name the caller already holds).
14/// Cloning one copies no text, so the [`RowSnapshot`](super::RowSnapshot)
15/// given to the observer is built without allocating. It dereferences to
16/// `str`, and it compares, hashes, orders and serializes as its text.
17#[derive(Clone)]
18pub struct SharedText(Repr);
19
20#[derive(Clone)]
21enum Repr {
22    Static(&'static str),
23    Shared(Arc<str>),
24}
25
26impl SharedText {
27    /// A text that refers to `text`, a constant: nothing is allocated.
28    pub const fn from_static(text: &'static str) -> SharedText {
29        SharedText(Repr::Static(text))
30    }
31
32    /// The text.
33    pub fn as_str(&self) -> &str {
34        match &self.0 {
35            Repr::Static(text) => text,
36            Repr::Shared(text) => text,
37        }
38    }
39}
40
41impl Deref for SharedText {
42    type Target = str;
43
44    fn deref(&self) -> &str {
45        self.as_str()
46    }
47}
48
49impl AsRef<str> for SharedText {
50    fn as_ref(&self) -> &str {
51        self.as_str()
52    }
53}
54
55impl Borrow<str> for SharedText {
56    fn borrow(&self) -> &str {
57        self.as_str()
58    }
59}
60
61impl From<&'static str> for SharedText {
62    fn from(text: &'static str) -> SharedText {
63        SharedText::from_static(text)
64    }
65}
66
67impl From<String> for SharedText {
68    fn from(text: String) -> SharedText {
69        SharedText(Repr::Shared(text.into()))
70    }
71}
72
73impl From<Arc<str>> for SharedText {
74    fn from(text: Arc<str>) -> SharedText {
75        SharedText(Repr::Shared(text))
76    }
77}
78
79impl PartialEq for SharedText {
80    fn eq(&self, other: &SharedText) -> bool {
81        self.as_str() == other.as_str()
82    }
83}
84
85impl Eq for SharedText {}
86
87impl PartialEq<str> for SharedText {
88    fn eq(&self, other: &str) -> bool {
89        self.as_str() == other
90    }
91}
92
93impl PartialEq<&str> for SharedText {
94    fn eq(&self, other: &&str) -> bool {
95        self.as_str() == *other
96    }
97}
98
99impl PartialOrd for SharedText {
100    fn partial_cmp(&self, other: &SharedText) -> Option<std::cmp::Ordering> {
101        Some(self.cmp(other))
102    }
103}
104
105impl Ord for SharedText {
106    fn cmp(&self, other: &SharedText) -> std::cmp::Ordering {
107        self.as_str().cmp(other.as_str())
108    }
109}
110
111impl Hash for SharedText {
112    fn hash<H: Hasher>(&self, state: &mut H) {
113        self.as_str().hash(state)
114    }
115}
116
117impl fmt::Display for SharedText {
118    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
119        f.write_str(self.as_str())
120    }
121}
122
123impl fmt::Debug for SharedText {
124    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125        fmt::Debug::fmt(self.as_str(), f)
126    }
127}
128
129impl serde::Serialize for SharedText {
130    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
131        serializer.serialize_str(self.as_str())
132    }
133}
134
135impl<'de> serde::Deserialize<'de> for SharedText {
136    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<SharedText, D::Error> {
137        String::deserialize(deserializer).map(SharedText::from)
138    }
139}
140
141/// The sort of work a row stands for: a dotted name and, optionally, a title.
142///
143/// The name is the same for every row of that sort, such as
144/// `flm.graphics.convert`; the run gives the row of a stage the stage's name.
145/// The feature that owns the work declares its kind as a constant where the
146/// work is done, and passes it by reference to [`Scope::child`](super::Scope::child):
147///
148/// ```
149/// use flm_core::progress::Kind;
150///
151/// const CONVERT: Kind = Kind::titled("flm.graphics.convert", "Converting graphics");
152/// assert_eq!(CONVERT.name(), "flm.graphics.convert");
153/// ```
154///
155/// Kinds are not registered. A display identifies a sort of row by the name,
156/// so renaming a kind is a change that a host's stored data about that kind
157/// (such as its measured durations) does not follow.
158///
159/// **The title** is the fixed text that a display shows for the rows of the
160/// kind, before the label of the row: "Converting graphics figure-3.pdf". A
161/// title says what is being done: it begins with a capital and a verb in
162/// "-ing", has no article and no final period, and names nothing of the
163/// library's internals ("Reading document", "Fetching citation data"). A kind
164/// whose rows a person need not see has no title; a display then shows the
165/// nearest row above that has one.
166#[derive(Clone, Debug, PartialEq, Eq)]
167pub struct Kind {
168    name: SharedText,
169    title: Option<SharedText>,
170}
171
172impl Kind {
173    /// A kind named `name`, with no title.
174    pub const fn new(name: &'static str) -> Kind {
175        Kind { name: SharedText::from_static(name), title: None }
176    }
177
178    /// A kind named `name`, with the title `title`.
179    pub const fn titled(name: &'static str, title: &'static str) -> Kind {
180        Kind { name: SharedText::from_static(name), title: Some(SharedText::from_static(title)) }
181    }
182
183    /// A kind with no title whose name is a text the caller already holds,
184    /// such as the name of a stage; the text is shared, not copied.
185    pub fn shared(name: impl Into<Arc<str>>) -> Kind {
186        Kind { name: SharedText::from(name.into()), title: None }
187    }
188
189    /// The same kind, with the title `title`, a text the caller already
190    /// holds, such as a title an extension gives; the text is shared, not
191    /// copied.
192    pub fn with_title(self, title: impl Into<Arc<str>>) -> Kind {
193        Kind { title: Some(SharedText::from(title.into())), ..self }
194    }
195
196    /// The kind's dotted name.
197    pub fn name(&self) -> &str {
198        self.name.as_str()
199    }
200
201    /// The kind's title, if it has one.
202    pub fn title(&self) -> Option<&str> {
203        self.title.as_deref()
204    }
205
206    /// The name, as the shared text that a row holds.
207    pub(super) fn shared_name(&self) -> &SharedText {
208        &self.name
209    }
210
211    /// The title, as the shared text that a row holds.
212    pub(super) fn shared_title(&self) -> Option<&SharedText> {
213        self.title.as_ref()
214    }
215}