rust: derive TextFormat's name and ChangeChannel's variant list with strum
TextFormat::as_str re-spelled the serde names by hand; it is now strum's IntoStaticStr under the same snake_case rule, with a test pinning both spellings. ChangeChannel::ALL was a hand-kept list of all five variants, which strum's VariantArray gives as VARIANTS without the chance of a new channel being left off it. Skipped from S14 on purpose: ContactProtocol's slug (frozen into derived ids) and ArtifactKind-from-ArtifactValue (renames stored strings). The store-local enums were done with their stores. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
65ed79d55f
commit
d2bb2eeb79
3 files changed
+33
-21
No files matched your search
@@ -882,8 +882,11 @@ impl ArtifactKind {
|
||||
}
|
||||
|
||||
/// The text markup format of a [`Note`](super::DocumentType::Note) body.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, uniffi::Enum)]
|
||||
#[derive(
|
||||
Debug, Clone, Serialize, Deserialize, Default, PartialEq, Eq, uniffi::Enum, IntoStaticStr,
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
#[strum(serialize_all = "snake_case")]
|
||||
pub enum TextFormat {
|
||||
#[default]
|
||||
Markdown,
|
||||
@@ -892,12 +895,9 @@ pub enum TextFormat {
|
||||
}
|
||||
|
||||
impl TextFormat {
|
||||
/// The format's name, as serde spells it too.
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
TextFormat::Markdown => "markdown",
|
||||
TextFormat::Typst => "typst",
|
||||
TextFormat::PlainText => "plain_text",
|
||||
}
|
||||
self.into()
|
||||
}
|
||||
|
||||
/// The filename suffix a note in this format is shown with — how a tree
|
||||
@@ -926,3 +926,19 @@ impl TextFormat {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use rstest::rstest;
|
||||
|
||||
/// `as_str` must keep agreeing with the serde spelling.
|
||||
#[rstest]
|
||||
#[case(TextFormat::Markdown, "markdown")]
|
||||
#[case(TextFormat::Typst, "typst")]
|
||||
#[case(TextFormat::PlainText, "plain_text")]
|
||||
fn text_format_names(#[case] format: TextFormat, #[case] name: &str) {
|
||||
assert_eq!(format.as_str(), name);
|
||||
assert_eq!(serde_json::to_string(&format).unwrap(), format!("\"{name}\""));
|
||||
}
|
||||
}
|
||||
@@ -38,6 +38,7 @@ use std::sync::{Arc, Condvar, Mutex, Weak};
|
||||
use std::time::Duration;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use strum::VariantArray;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
use crate::documents::DocumentID;
|
||||
@@ -58,7 +59,13 @@ use crate::documents::DocumentID;
|
||||
/// silently shows something that has stopped being true. Which is what the
|
||||
/// Settings channel was — a remote node can set this node's sync policy
|
||||
/// (`p2p::node_admin`), and the tab showing that policy had no way to hear it.
|
||||
#[derive(uniffi::Enum, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
///
|
||||
/// [`VARIANTS`](VariantArray::VARIANTS) is every channel, for a client that
|
||||
/// wants to hear about all of them — which is what a whole UI process wants,
|
||||
/// as opposed to one screen.
|
||||
#[derive(
|
||||
uniffi::Enum, Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, VariantArray,
|
||||
)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum ChangeChannel {
|
||||
/// Documents and their artifacts — the one bus
|
||||
@@ -80,18 +87,6 @@ pub enum ChangeChannel {
|
||||
Settings,
|
||||
}
|
||||
|
||||
impl ChangeChannel {
|
||||
/// Every channel, for a client that wants to hear about all of them —
|
||||
/// which is what a whole UI process wants, as opposed to one screen.
|
||||
pub const ALL: [ChangeChannel; 5] = [
|
||||
ChangeChannel::Documents,
|
||||
ChangeChannel::Jobs,
|
||||
ChangeChannel::Linkages,
|
||||
ChangeChannel::Nodes,
|
||||
ChangeChannel::Settings,
|
||||
];
|
||||
}
|
||||
|
||||
/// Where a client is up to on every channel.
|
||||
///
|
||||
/// Passed back to [`ChangeLedger::wait`] as the `since` it should wake from,
|
||||
@@ -149,7 +144,7 @@ impl ChangeCursor {
|
||||
/// Every channel that moved between `previous` and this cursor — what a
|
||||
/// client turns into "and so these screens re-read".
|
||||
pub fn moved_channels(&self, previous: &Self) -> Vec<ChangeChannel> {
|
||||
ChangeChannel::ALL.into_iter().filter(|c| self.moved(previous, *c)).collect()
|
||||
ChangeChannel::VARIANTS.iter().copied().filter(|c| self.moved(previous, *c)).collect()
|
||||
}
|
||||
|
||||
/// Whether any of `channels` has moved past `since`.
|
||||
|
||||
@@ -29,6 +29,7 @@ use std::{
|
||||
fs,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use strum::VariantArray;
|
||||
use tokio::sync::broadcast;
|
||||
|
||||
/// How long a connection will wait for a write lock held by another
|
||||
@@ -426,7 +427,7 @@ impl Store {
|
||||
/// asking on a timer. **Blocks the calling thread**; from async code,
|
||||
/// reach it through `spawn_blocking`.
|
||||
pub fn await_change(&self, since: ChangeCursor, timeout: Duration) -> ChangeCursor {
|
||||
self.changes.wait(since, &ChangeChannel::ALL, timeout)
|
||||
self.changes.wait(since, ChangeChannel::VARIANTS, timeout)
|
||||
}
|
||||
|
||||
/// Open an existing store directory.
|
||||
|
||||
Reference in new issue
Block a user