Skip to main content

nibkeep_node/
error.rs

1//! Why the engine could not do something.
2
3use std::io;
4use std::path::{Path, PathBuf};
5
6use nibkeep_base::{EntityId, SpaceId};
7use nibkeep_keys::KeysError;
8use nibkeep_log::LogError;
9use nibkeep_model::{DefinitionError, FieldName, TypeName};
10use nibkeep_net::NetError;
11use nibkeep_store::StoreError;
12use nibkeep_sync::SyncError;
13
14/// Why the engine could not do something.
15#[derive(Debug, thiserror::Error)]
16#[non_exhaustive]
17pub enum NodeError {
18	/// The directory holds no library.
19	#[error("there is no library here")]
20	NoLibrary,
21	/// The directory holds a library already.
22	#[error("there is a library here already")]
23	Exists,
24	/// A name is empty, and a name is written in full.
25	#[error("a name cannot be empty")]
26	EmptyName,
27	/// A library was started in the directory, but starting it was cut short.
28	#[error("a library was started here, but not finished; remove its files to start again")]
29	Unfinished,
30	/// The directory holds only copies of replicas: made on another computer or drive, or in
31	/// another folder, and brought here by a backup, a program that syncs folders, or by hand.
32	/// A copy is never used as the device it was, but helps this computer become a device of
33	/// its own.
34	#[error(
35		"this library is a copy of one on another computer or drive, or in another folder; make this computer a device of its own"
36	)]
37	Copied,
38	/// Two or more replicas in the directory are this computer's, in these folders.
39	#[error(
40		"{} replicas here are all this computer's, in {}; move away all but the one you use",
41		.0.len(),
42		.0.iter().map(|folder| folder.display().to_string()).collect::<Vec<_>>().join(", ")
43	)]
44	SeveralHere(Vec<std::path::PathBuf>),
45	/// The library's replica, or one of its files, is of a later version than this program's
46	/// engine reads, which a newer program that shares it wrote: nothing was opened or
47	/// written ([storage](../../../docs/spec/storage.md#16-engines-of-different-versions)).
48	#[error("a newer version of NibKeep changed this library; update this program to open it")]
49	NewerFormats,
50	/// A drive whose file system keeps no journal, as FAT and exFAT, where a removal without
51	/// unmounting can damage a library later (technical design: drives without a journal).
52	#[error(
53		"this drive keeps no journal, as FAT and exFAT do: a library on it can be damaged after the drive is removed without ejecting it, or loses its power; keep it on another drive, or allow it on this one"
54	)]
55	NoJournal,
56	/// The drive of a replica without a journal was removed, or lost its power, while the
57	/// replica was open, so that its file system may be damaged until it is checked; or a
58	/// program that had it open was stopped then, which leaves the same mark.
59	#[error(
60		"the library on this drive was not closed: the drive was removed without ejecting it, or lost its power, while the library was open, or a program that had it open was stopped; check the drive for errors first (Scan and fix on Windows, First Aid on a Mac, fsck on Linux), then say that it was checked, and the library is verified"
61	)]
62	UncleanRemoval,
63	/// An app's own folder on a phone, whose ID the app could not read: a replica made there
64	/// would never open again.
65	#[error("this phone gives the app no ID to keep its library by")]
66	NoPhoneId,
67	/// The directory is on a network drive, on which SQLite's write-ahead log does not work.
68	#[error(
69		"a library cannot be kept on a network drive, where its databases are not safe; keep it on a drive of this computer's"
70	)]
71	RemoteDrive,
72	/// The directory is in a folder whose files a program that syncs with the cloud may take
73	/// away until they are opened.
74	#[error(
75		"a library cannot be kept in a folder that a program syncs with the cloud, which may take its files away; keep it elsewhere on this computer, and NibKeep syncs it with your other devices"
76	)]
77	CloudFolder,
78	/// The library has no such space.
79	#[error("this library has no space {0}")]
80	NoSpace(SpaceId),
81	/// No space has this name, this ID or this start of an ID.
82	#[error("there is no space {0}")]
83	NoSpaceNamed(String),
84	/// Several spaces have this name or this start of an ID.
85	#[error("{0} could be several spaces: give its ID")]
86	SpacesNamed(String),
87	/// A secret value in a shared space whose secrets another device holds, and this one
88	/// does not yet.
89	#[error(
90		"another device holds this space's secrets: your devices pass them on as they sync, \
91		 or ask one that holds them"
92	)]
93	SpaceSecretsElsewhere,
94	/// Secrets of a shared space, on a device whose own secrets are not set up yet.
95	#[error("set up the secrets on this device first")]
96	SetUpSecretsFirst,
97	/// A move to the space the entity is in.
98	#[error("the entity is in that space already")]
99	SameSpace,
100	/// An entity moved to another space already, which moves no further from here.
101	#[error("the entity was moved to another space already")]
102	MovedAlready,
103	/// A move of what may hold secret values, while the secrets of either space are locked,
104	/// or this device holds none of them.
105	#[error(
106		"what moves holds secret values, which move only with the secrets of both spaces \
107		 unlocked: unlock them, or leave the secret values behind"
108	)]
109	MoveNeedsSecrets,
110	/// A move of a secret value whose field is secret where it is, and would be plain in
111	/// the other space, whose admins alone make it secret.
112	#[error(
113		"{0} is secret here, and would not be in the other space, where only its admins make \
114		 it secret: leave the secret values behind, or ask an admin to move it"
115	)]
116	NotSecretThere(FieldName),
117	/// A tree, or what is in one, which moves only whole, once trees are shared: an entity of
118	/// a move, or of a folder or a list that a move takes.
119	#[error("{0} is a tree or in one, which moves only whole, once trees are shared")]
120	TreeStays(String),
121	/// Content that this device does not hold yet.
122	#[error("the content {0} is neither held on this device nor where it was found here")]
123	NotHere(String),
124	/// What the personal space does not do, as being renamed.
125	#[error("the personal space keeps its name")]
126	PersonalSpace,
127	/// A choice of the personal space, which every device of the user holds
128	/// ([technical design](../../../docs/Technical-design.md#1310-devices-that-choose-their-spaces)).
129	#[error("every device holds the personal space")]
130	PersonalSpaceHeld,
131	/// A device that dropped a space and has not asked for it again, as listing it there
132	/// again needs its own newer choice to hold the space.
133	#[error("that device has not asked for the space again")]
134	NoNewerChoice,
135	/// A device that asks for no such space here, as approving a request needs
136	/// ([technical design](../../../docs/Technical-design.md#1310-devices-that-choose-their-spaces)).
137	#[error("that device asks for no such space here")]
138	NoRequest,
139	/// A drop of a space that this device may not make now, with each reason
140	/// ([technical design](../../../docs/Technical-design.md#1310-devices-that-choose-their-spaces)).
141	#[error("the space cannot be dropped now: {0}")]
142	DropRefused(crate::DropRefusals),
143	/// No entity has this ID, this prefix of an ID, or this name.
144	#[error("there is no entity {0}")]
145	NoEntity(String),
146	/// Several entities have this prefix of an ID, or this name.
147	#[error("{reference} could be any of {} entities", candidates.len())]
148	Ambiguous {
149		/// What was looked for.
150		reference: String,
151		/// The entities it could be.
152		candidates: Vec<EntityId>,
153	},
154	/// The type is one of NibKeep's own, such as those that define types, whose entities only
155	/// its own commands make.
156	#[error("{0} is defined by NibKeep, and only its own commands make entities of it")]
157	OwnType(TypeName),
158	/// A type to take away from an entity that does not have it.
159	#[error("the entity has no type {0}")]
160	NotOfType(TypeName),
161	/// The only type of an entity, to take away: an entity keeps at least one.
162	#[error("{0} is the entity's only type; give it another before taking this one away")]
163	OnlyType(TypeName),
164	/// A type to delete or restore that is not the space's own: a standard one, which the
165	/// vocabulary installs again, or one that NibKeep or an app defines in code.
166	#[error("{0} is not a type of the space's own, so it is neither deleted nor restored")]
167	NotOwnType(TypeName),
168	/// A type to delete that no definition of the space makes, or to restore that no deleted
169	/// one does.
170	#[error("the space has no definition of {0} to delete or restore")]
171	NoTypeDefined(TypeName),
172	/// A type to delete that entities still have.
173	#[error(
174		"{entities} {} {kind}; take it away from them, or delete them, before deleting the type",
175		if *entities == 1 { "entity has" } else { "entities have" }
176	)]
177	TypeInUse {
178		/// The type.
179		kind: TypeName,
180		/// How many entities that are not deleted have it.
181		entities: usize,
182	},
183	/// The field is one that NibKeep reserves for its own structure.
184	#[error("{0} is one of NibKeep's own fields, which only its own commands set")]
185	OwnField(FieldName),
186	/// A field with one value was given several.
187	#[error("{0} takes one value")]
188	OneValue(FieldName),
189	/// A value to remove, or to add a form to, is not one of the field's.
190	#[error("{0} has no such value")]
191	NoValue(FieldName),
192	/// A form is to be added to a field that shows several values, without saying which.
193	#[error("{0} shows several values; say which one the new form is of")]
194	WhichValue(FieldName),
195	/// Entities to merge that are one already, merged or not.
196	#[error("{0} and the entity to merge it into are one already")]
197	SameEntity(String),
198	/// An entity to merge that defines a type or a field.
199	#[error("{0} defines a type or a field, and is not merged")]
200	DefinesTypes(String),
201	/// An entity to merge that is one of NibKeep's own records of who uses the library.
202	#[error("{0} is a record of a user, device, replica or space, which is not merged")]
203	OwnRecord(String),
204	/// An entity to merge, or a secret file to open or save, that is deleted.
205	#[error("{0} is deleted; restore it first")]
206	Deleted(String),
207	/// A merge to undo of an entity that was not merged into another.
208	#[error("{0} was not merged into another entity")]
209	NotMerged(String),
210	/// An export into a directory that holds something already.
211	#[error("{0} holds files already; an export goes into an empty directory or a new one")]
212	ExportExists(String),
213	/// An export to import that is damaged, or none that this version reads.
214	#[error("the export cannot be imported: {0}")]
215	ExportDamaged(String),
216	/// A file to add that is not one, such as a folder, or that cannot be read.
217	#[error("{path} cannot be added: {reason}")]
218	Unreadable {
219		/// The path, as it was given.
220		path: String,
221		/// Why it cannot be added.
222		reason: String,
223	},
224	/// An entity to pin that is no file.
225	#[error("{0} is not a file")]
226	NotAFile(String),
227	/// A file to pin to a device of the person's that does not hold the file's space
228	/// ([technical design](../../../docs/Technical-design.md#1310-devices-that-choose-their-spaces)).
229	#[error("that device does not hold the file's space; take the space there first")]
230	DeviceLacksSpace,
231	/// A file to check out that has no content, or is no file.
232	#[error("{0} has no content to check out")]
233	NoContent(String),
234	/// A secret file to check out: NibKeep records no plain copy of one.
235	#[error("{0} is a secret file, of which NibKeep records no copies; save a copy instead")]
236	SecretFile(String),
237	/// An entity to read or save as a secret file that is none.
238	#[error("{0} is not a secret file")]
239	NotSecretFile(String),
240	/// Content in a secret field that was not sealed for the secrets, so that every replica
241	/// of the space could read it.
242	#[error("the content for {0} is not sealed for the secrets; add it as a secret file")]
243	NotSealedForSecrets(FieldName),
244	/// A file to check out whose content this device holds no copy of: neither in the store
245	/// nor where it was found.
246	#[error("this device holds no copy of the content of {0}; sync with a device that does")]
247	NoCopyHere(String),
248	/// A file to check out into a folder whose name is no name a file can have.
249	#[error("{0:?} is not a name a file can have; give the path of the copy")]
250	NotAFileName(String),
251	/// Something is where a checkout's copy would go.
252	#[error("{} exists already, and a checkout replaces nothing", .0.display())]
253	PlaceTaken(PathBuf),
254	/// No checkout on this device is the one named.
255	#[error("{0} is not checked out on this device")]
256	NotCheckedOut(String),
257	/// A file checked out in several places on this device, named without saying which.
258	#[error("{file} is checked out in several places on this device; name one: {}", paths.iter().map(|path| path.display().to_string()).collect::<Vec<_>>().join(", "))]
259	WhichCheckout {
260		/// The file, as it was given.
261		file: String,
262		/// Where its copies are.
263		paths: Vec<PathBuf>,
264	},
265	/// The edits of a checkout's copy, made to a version that is no longer the file's own.
266	#[error("{} was checked out from a version that is no longer the file's own", .0.display())]
267	Stale(PathBuf),
268	/// A checkout to end whose copy has edits that are not recorded.
269	#[error("{} has edits that are not recorded yet", .0.display())]
270	Unrecorded(PathBuf),
271	/// A file to add that is a checkout's copy, whose edits checking in records.
272	#[error("{} is a checkout", .0.display())]
273	CheckedOut(PathBuf),
274	/// A file to add that is in a tree's folder on this device, whose commits record it
275	/// (technical design: trees).
276	#[error("{} is in a tree, whose commits record it", .0.display())]
277	InTree(PathBuf),
278	/// A file to check out that is a tree's, whose own commands write its copies.
279	#[error("{0} is a file of a tree, whose copy the tree's commands write")]
280	TreeFile(String),
281	/// A tree with no folder on this device.
282	#[error("the tree {0} has no folder on this device")]
283	NoTreeHere(String),
284	/// A tree to start in a tree's folder on this device, or in a folder that holds one: a
285	/// tree is never inside another.
286	#[error("{} is in a tree's folder, or holds one, and a tree is never inside another", .0.display())]
287	TreeInTree(PathBuf),
288	/// A tree to start in a folder that holds a Git repository, which a tree records as a
289	/// reference to its remotes rather than file by file.
290	#[error(
291		"{} holds a Git repository, which a tree records by its remotes, not file by file; start the tree in the folder above it, or move .git away to keep its files in the tree",
292		.0.display()
293	)]
294	GitRepositoryHere(PathBuf),
295	/// A tree to start in the library's own folder, or in a folder that a system reads as
296	/// `.git` or `.nib`.
297	#[error("{} is in the library's own folder or in Git's, where no tree can be", .0.display())]
298	NotATreeFolder(PathBuf),
299	/// Paths of a tree that changed in the copy and in the tree since the copy was written,
300	/// as by another device.
301	#[error(
302		"the tree changed elsewhere at {} since this copy was written; update the copy first, or commit with force to record these on top",
303		.0.join(", ")
304	)]
305	TreeStale(Vec<String>),
306	/// Paths of a tree whose copies were edited from a version this replica does not know,
307	/// as after its index was lost.
308	#[error(
309		"{} were edited from a version this device does not know; commit with force to record them on top",
310		.0.join(", ")
311	)]
312	UnknownVersion(Vec<String>),
313	/// Paths of a tree whose changes are of secret files, or take in new ones, which a commit
314	/// seals only with the space's secrets unlocked
315	/// ([technical design: secrets in trees](../../../docs/Technical-design.md#151-secrets-in-trees)).
316	#[error(
317		"{} are secret files, which a commit records only with the secrets unlocked; unlock them, or leave these changes for a later commit",
318		.0.join(", ")
319	)]
320	SecretsNeeded(Vec<String>),
321	/// A secret file of a tree whose lines were asked for without asking to show secrets.
322	#[error("{0} is a secret file, whose lines are shown only when asked for")]
323	SecretLines(String),
324	/// A tree's folder on this device that is gone, as when it moved: naming its new place
325	/// records it.
326	#[error("the tree's folder {} is gone; if it moved, record its new place", .0.display())]
327	TreeFolderGone(PathBuf),
328	/// A commit that would delete every file of a tree, which a drive that is not there, or a
329	/// folder emptied by mistake, would look like.
330	#[error(
331		"every file of the tree is gone from {}; if its drive is there and they were deleted on purpose, commit with force",
332		.0.display()
333	)]
334	TreeEmptied(PathBuf),
335	/// What a tree records of a Git repository changed while it was cloned.
336	#[error("the tree's record of the Git repository at {0} changed meanwhile; update again")]
337	RepositoryChanged(String),
338	/// A tree records no Git repository at a path.
339	#[error("the tree records no Git repository at {0}")]
340	NoRepository(String),
341	/// What was to name a commit is no full commit as Git names one.
342	#[error("{0} is no full commit, 40 or 64 hexadecimal digits in lower case")]
343	NotACommit(String),
344	/// A folder of a tree to move that holds copies NibKeep wrote for other reasons, such as
345	/// checkouts, which are recorded by their paths.
346	#[error(
347		"{0} holds copies that NibKeep wrote for other reasons, such as checkouts; end them or move them out first"
348	)]
349	HoldsOtherCopies(String),
350	/// A folder to write a tree into that holds something already.
351	#[error("{} is not empty, and a tree is written only into an empty folder or a new one", .0.display())]
352	FolderNotEmpty(PathBuf),
353	/// A tree to write as it is on a device that has a copy of it already.
354	#[error("this device has the tree in {} already", .0.display())]
355	TreeHereAlready(PathBuf),
356	/// A copy of a tree's file to write over that has edits not committed.
357	#[error("{0} has edits that are not committed; commit them or put them aside first")]
358	EditedCopy(String),
359	/// An entity named as a tree that is none.
360	#[error("{0} is not a tree")]
361	NotATree(String),
362	/// A revision that the tree does not have.
363	#[error("the tree has no revision {0}")]
364	NoRevision(String),
365	/// A file of a tree whose version is no text, for what reads lines.
366	#[error("{0} is not text")]
367	NotText(String),
368	/// A path that a tree holds nothing at or under.
369	#[error("the tree holds nothing at {0}")]
370	NotInTree(String),
371	/// A path that a tree may not hold or write: one that leaves its folder, goes through a
372	/// link, names the library's folder or Git's, or that this system cannot write.
373	#[error("{0:?} is not a path that a tree can hold here")]
374	UnsafePath(String),
375	/// The library's own passphrase, given to protect its secrets, or the secrets' to
376	/// protect the library.
377	#[error("the library and its secrets need passphrases of their own")]
378	LibraryPassphrase,
379	/// A connection answered while the library is locked here, as an app's is: the other
380	/// replica is told to try again later.
381	#[error("the library is locked")]
382	Locked,
383	/// Secret values to read, purge or protect anew, while the secrets are locked.
384	#[error("the secrets are locked; unlock them first")]
385	SecretsLocked,
386	/// Secrets keys to make on a replica of a library whose secrets another replica keeps:
387	/// this one asks for them instead.
388	#[error("another of your devices keeps this library's secrets; ask it for them")]
389	SecretsElsewhere,
390	/// Secrets keys to give a library that has them already, or asks for them already.
391	#[error("the library has its secrets keys already")]
392	SecretsSetUp,
393	/// A secrets compartment to forget, whose key the keystore still has, or that a
394	/// passphrase protects: it can still be unlocked.
395	#[error("the secrets can still be unlocked on this device")]
396	SecretsKeyKept,
397	/// Secrets to ask for on a replica that asked already: another that holds them gives
398	/// them.
399	#[error("this device asked for the secrets, and was not given them yet")]
400	SecretsAsked,
401	/// Secrets to ask for where no replica holds any yet: the first secret sets them up.
402	#[error("no device has the secrets yet; the first secret saved sets them up")]
403	SecretsNowhere,
404	/// A copy of a replica made elsewhere holds none of the secrets keys that the records
405	/// gave its replica (technical design: copies of a replica).
406	#[error("the copy holds none of the secrets keys that its device was given")]
407	NoSecretsInCopy,
408	/// Secrets to give a replica that has not asked for them, with a secrets encryption key
409	/// of its own.
410	#[error("that device has not asked for the secrets; ask for them on it first")]
411	NotAsked,
412	/// Secrets to ask for or give on a replica whose record lists a key that it did not make,
413	/// as only another replica of the user's can: what it says of the secrets counts for
414	/// nothing until it is added anew.
415	#[error(
416		"another of your devices listed a key of its own on this device, so what this one says of the secrets counts for nothing; that device may be infected"
417	)]
418	ForeignKey,
419	/// An import that stopped after it had imported some of an export: importing it again
420	/// brings in the rest.
421	#[error(
422		"the import stopped after {entities} entities, which it kept: {error}; importing again brings in the rest"
423	)]
424	ImportStopped {
425		/// How many entities it had read, those left out included.
426		entities: usize,
427		/// Why it stopped.
428		error: Box<NodeError>,
429	},
430	/// An export that could not be written.
431	#[error("the export could not be written: {0}")]
432	Export(String),
433	/// A type cannot be defined as it is.
434	#[error(transparent)]
435	Definition(#[from] DefinitionError),
436	/// The library's keys could not be unlocked or saved.
437	#[error(transparent)]
438	Keys(#[from] KeysError),
439	/// A space could not be stored or read.
440	#[error(transparent)]
441	Store(StoreError),
442	/// The log refused an operation.
443	#[error(transparent)]
444	Log(#[from] LogError),
445	/// A name of a device or an app is longer than pairing takes.
446	#[error("a name can be at most {0} bytes long")]
447	LongName(usize),
448	/// Pairing failed after the code matched: the new replica's first sync failed, and both
449	/// sides undid what they made.
450	#[error("the new device's first sync failed ({0}); nothing was kept, so pair it again")]
451	PairingFailed(Box<NodeError>),
452	/// No replica showed the pairing code on the network in time.
453	#[error(
454		"no device on the network shows this code; check the code, and that both devices are on the same network"
455	)]
456	NoSponsor,
457	/// No replica paired before the pairing code expired.
458	#[error("no device paired before the code expired")]
459	PairingExpired,
460	/// An invitation failed after the joiner took the space: its first sync failed, and both
461	/// sides undid what they made, as far as they could.
462	#[error(
463		"the first sync of the space failed ({0}); the joiner holds nothing of it, so invite again"
464	)]
465	InvitationFailed(Box<NodeError>),
466	/// Nobody joined before the invitation's code expired.
467	#[error("nobody joined before the code expired")]
468	InvitationExpired,
469	/// The person who invites declined the one who asked.
470	#[error("the invitation was declined, and nobody was added")]
471	Declined,
472	/// The library that an invitation reached holds the space already.
473	#[error("this library holds the space {0} already")]
474	HoldsSpace(SpaceId),
475	/// Only an admin of the space may do what was asked.
476	#[error("only an admin of the space may {0}")]
477	AdminsOnly(&'static str),
478	/// The personal space has no members, and is never shared.
479	#[error("the personal space is never shared, and has no members")]
480	NeverShared,
481	/// What was asked needs a group, and the space is none.
482	#[error("the space {0} is no group")]
483	NotAGroup(SpaceId),
484	/// A group is a member of a space as an editor or a viewer, never as an admin.
485	#[error("a group joins a space as an editor or a viewer, never as an admin")]
486	GroupAsAdmin,
487	/// Only a member of a group adds it to a space.
488	#[error("only a member of the group may add it to a space")]
489	NotInGroup,
490	/// Only a direct member of a space claims its admin role.
491	#[error("only a direct member of this space claims its admin role")]
492	ClaimNeedsDirect,
493	/// Only an editor claims the admin role.
494	#[error("only an editor claims the admin role")]
495	ClaimNeedsEditor,
496	/// The member made no claim to the admin role that stands.
497	#[error("{0} made no claim to the admin role that stands")]
498	NoClaim(String),
499	/// No admin of the space made a record there for this user, a member through a group.
500	#[error("no admin of this space made you a direct member of it")]
501	NoDirectRecord,
502	/// The space did not take this device on the record that an admin made for its user.
503	#[error(
504		"this space did not take this device on the record made for you: ask an admin to make you a direct member again"
505	)]
506	DirectRecordRefused,
507	/// A group would hold itself, or a group that holds it.
508	#[error("a group cannot hold itself, or a group that holds it")]
509	GroupCycle,
510	/// A group holds no entities of its own, as everyone in the spaces it is in reads it.
511	#[error("a group holds no entities of its own: choose a space for them")]
512	GroupHoldsNoData,
513	/// A group has no request to join the space named.
514	#[error("the group has no request to join that space")]
515	NoGroupRequest,
516	/// The device that invites a former member back has not learned yet that they left or
517	/// were removed, and offers the member they were.
518	#[error(
519		"the device that invites you does not know yet that you left; let it sync with yours, then invite you again"
520	)]
521	InviterBehind,
522	/// An invitation gives a role; leaving and removing give the role none.
523	#[error("an invitation gives a role: viewer, editor or admin")]
524	NoRole,
525	/// This replica's member left the space or was removed from it, or, for an AI agent's
526	/// replica, the space allows AI agents no more: it keeps what it holds, read only.
527	#[error(
528		"you are no longer a member of this space: you keep what you had there, read only, and come back only if an admin invites you again"
529	)]
530	Removed,
531	/// A space never goes without an admin.
532	#[error("the space would have no admin left; make another member an admin first")]
533	LastAdmin,
534	/// A space where AI agents were chosen for allows none, and this replica's member is no
535	/// admin there to allow them.
536	#[error("{0} allows no AI agents; ask an admin of it to allow them")]
537	AiAgentsNotAllowed(String),
538	/// The space has no such member.
539	#[error("the space has no member {0}")]
540	NoMember(String),
541	/// What a move made of the entity is not here, or differs from what stays.
542	#[error(
543		"what moved of {0} is not on this device, or differs from what stays; nothing was purged"
544	)]
545	MovedDiffers(String),
546	/// No move asked that what it left behind of the entity be purged.
547	#[error("no move asked that what it left of {0} be purged")]
548	NoPurgeAsked(String),
549	/// Another program borrows the network to pair, or the program that holds the sync role
550	/// keeps its endpoint, as while a long sync runs.
551	#[error("another program uses the network, to add a device or to sync; try again later")]
552	NetworkBusy,
553	/// A sync panicked, which the program that holds the sync role went on after.
554	#[error("the sync failed unexpectedly")]
555	Panicked,
556	/// The library has no other replica to sync with.
557	#[error("this library has no other device to sync with")]
558	NoOtherReplicas,
559	/// A replica to sync with was not found on the network.
560	#[error("not found on the network")]
561	NotFound,
562	/// This replica was revoked from another device: it can change nothing, and no other
563	/// device syncs with it.
564	#[error("this device was revoked from another of yours; pair it again to use the library here")]
565	Revoked,
566	/// No counting change of the library lists this replica, as when the replica that
567	/// paired it was revoked without having passed the pairing on: whatever it changed would
568	/// be set aside.
569	#[error(
570		"this device is not among the library's devices any more; pair it again to use the library here"
571	)]
572	NotListed,
573	/// This replica is an AI agent's, which may not do what was asked: every replica would
574	/// set the change aside ([technical design](../../../docs/Technical-design.md#14-ai-agents)).
575	#[error("an AI agent may not {0}; the user can, on a device of theirs")]
576	AiAgentMayNot(&'static str),
577	/// An AI client asked to work on the user's replica, where the person did not allow AI
578	/// agents ([technical design](../../../docs/Technical-design.md#14-ai-agents)).
579	#[error(
580		"AI agents are not allowed on this library here; the user allows them with nib mcp enable"
581	)]
582	AiAgentsNotAllowedHere,
583	/// The key of the AI client named was revoked.
584	#[error("the AI agent {0} was revoked; the user allows AI agents again with nib mcp enable")]
585	AiAgentRevoked(String),
586	/// A name or ID names no device or replica of the user's, or several, or this device
587	/// where another is needed.
588	#[error("{0}")]
589	WhichDevice(String),
590	/// A name or ID names no backup of the user's, or several.
591	#[error("{0}")]
592	WhichBackup(String),
593	/// A name or ID names no recovery code of the user's that stands, or several
594	/// ([technical design](../../../docs/Technical-design.md#111-recovery-codes)).
595	#[error("{0}")]
596	WhichRecoveryCode(String),
597	/// A folder to recover a library from that holds neither a backup nor a library of a
598	/// person with a recovery code
599	/// ([technical design](../../../docs/Technical-design.md#111-recovery-codes)).
600	#[error(
601		"{} holds no backup or library with a recovery code: a library holds its code's file once the code is made, and a backup once it was updated after that",
602		.0.display()
603	)]
604	NoRecoverySource(PathBuf),
605	/// A recovery code that opens nothing of the folder to recover from: another library's,
606	/// or one made after the backup was last updated.
607	#[error(
608		"this recovery code opens nothing here: it may be another library's, or newer than the backup's last update, which an older code then opens"
609	)]
610	CodeOpensNothing,
611	/// A recovery file whose entry for the code the code did not sign, as a device that
612	/// turned against its person could write while it updated the backup: what it names is
613	/// not taken ([technical design](../../../docs/Technical-design.md#111-recovery-codes)).
614	#[error(
615		"the recovery file here was changed: its entry for this code is not what the code signed as it was made, as a device that turned against you could write; bring the library back from another backup"
616	)]
617	RecoveryFileChanged,
618	/// A recovery code that the library revoked, which brings no device back.
619	#[error("this recovery code was revoked, and brings no device back")]
620	RecoveryCodeRevoked,
621	/// The secrets asked for a recovery code that another device made, which alone gives it
622	/// them: a device that recovers with the code could not tell what another gave it from a
623	/// key of someone's own making
624	/// ([technical design](../../../docs/Technical-design.md#111-recovery-codes)).
625	#[error(
626		"this recovery code was made on another device, which alone can give it the secrets; make a new code here to have one that holds them"
627	)]
628	RecoveryCodeMadeElsewhere,
629	/// A backup that another program is updating, which a recovery reads once it is done.
630	#[error("another program is updating this backup; try again once it is done")]
631	BackupBusy,
632	/// A recovery of the library in the directory was cut short, as by a crash: its new
633	/// device is known to nobody, and running the recovery again starts it over.
634	#[error(
635		"recovering the library here was cut short; run nib recovery restore again to start over"
636	)]
637	RecoveryCutShort,
638	/// A name names no purge that waits on this device, or several
639	/// ([technical design](../../../docs/Technical-design.md#911-copies-that-wait)).
640	#[error("{0}")]
641	WhichPurge(String),
642	/// The member's role does not allow restoring what a purge destroyed: an admin may, and
643	/// an editor for the changes their member's replicas signed.
644	#[error(
645		"your role here does not allow stopping this purge: an admin may, and an editor for what their own devices wrote"
646	)]
647	CannotRestore,
648	/// A folder to make a backup in that holds something already, or lies in a library.
649	#[error("{0}")]
650	BackupFolder(String),
651	/// A folder found as a backup that is not that backup, or is damaged
652	/// ([technical design](../../../docs/Technical-design.md#910-backups)).
653	#[error("{} is not the backup {name}", path.display())]
654	NotTheBackup {
655		/// The folder.
656		path: PathBuf,
657		/// The backup's name.
658		name: String,
659	},
660	/// A file written to a backup that did not read back as written.
661	#[error("{} did not read back as it was written; the drive may be failing", .0.display())]
662	BackupWrite(PathBuf),
663
664	/// Syncing from login, for a library whose keys a passphrase protects: nobody is there
665	/// to type it at login ([technical design](../../../docs/Technical-design.md#106-syncing-from-login)).
666	#[error(
667		"syncing from login needs the library's keys in the keystore, as nobody is there to type a passphrase at login"
668	)]
669	NotInKeystore,
670	/// A path that the list of libraries synced from login cannot hold, as it is not
671	/// Unicode.
672	#[error("{} is not a Unicode path", .0.display())]
673	NotUnicode(PathBuf),
674
675	/// Pairing or syncing failed.
676	#[error(transparent)]
677	Sync(#[from] SyncError),
678	/// The network failed.
679	#[error(transparent)]
680	Network(#[from] NetError),
681	/// A file of the library is not well-formed, or missing.
682	#[error("the library is damaged: {0}")]
683	Damaged(String),
684	/// A file or directory of the library could not be read or written.
685	#[error("{}: {error}", path.display())]
686	File {
687		/// The file or directory.
688		path: PathBuf,
689		/// What went wrong.
690		error: io::Error,
691	},
692}
693
694impl NodeError {
695	/// Whether the library could not be opened, or a change made, because another program,
696	/// or another part of this one, has a space to itself for a moment, as while it makes its
697	/// state file anew after a purge, or waits to until the others close it: the library is
698	/// to be closed, and trying again later may work.
699	#[must_use]
700	pub fn in_use(&self) -> bool {
701		matches!(self, Self::Store(StoreError::InUse))
702	}
703
704	/// Whether a pairing or an invitation failed because the code that a device typed did not
705	/// match the one shown.
706	#[must_use]
707	pub const fn is_wrong_code(&self) -> bool {
708		matches!(self, Self::Sync(nibkeep_sync::SyncError::WrongCode))
709	}
710
711	/// The error for a file or directory that could not be read or written.
712	pub(crate) fn file(path: &Path) -> impl FnOnce(io::Error) -> Self {
713		let path = path.to_owned();
714		move |error| Self::File { path, error }
715	}
716}
717
718impl From<StoreError> for NodeError {
719	/// A space's file of a later version is the library's, which only an update reads.
720	fn from(error: StoreError) -> Self {
721		match error {
722			StoreError::Newer => Self::NewerFormats,
723			error => Self::Store(error),
724		}
725	}
726}
727
728impl From<std::convert::Infallible> for NodeError {
729	fn from(never: std::convert::Infallible) -> Self {
730		match never {}
731	}
732}