memoryd, proto, brokerd, bxctl: repeated error lines written once
Implemented-By: OpenCode session (model recorded in docs/implementer-log.md)
This commit is contained in:
+19
-40
@@ -165,6 +165,14 @@ pub fn entry_problem(text: &str, max: usize) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// `entry_problem` as a `Result`, so a check can use `?`.
|
||||
fn entry(text: &str, max: usize) -> Result<(), String> {
|
||||
match entry_problem(text, max) {
|
||||
Some(problem) => Err(problem),
|
||||
None => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
impl RecallArgs {
|
||||
/// The shape rules of `recall` (M5a spec, section 5).
|
||||
pub fn check(&self) -> Result<(), String> {
|
||||
@@ -183,61 +191,32 @@ impl MemoryArgs {
|
||||
/// every `consolidate` line has one action and the list is not empty.
|
||||
pub fn check(&self) -> Result<(), String> {
|
||||
match self {
|
||||
MemoryArgs::Add { text, .. } => {
|
||||
if let Some(problem) = entry_problem(text, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
}
|
||||
MemoryArgs::Add { text, .. } => entry(text, MAX_ENTRY),
|
||||
MemoryArgs::Replace { old, text, .. } => {
|
||||
if let Some(problem) = entry_problem(old, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
if let Some(problem) = entry_problem(text, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
}
|
||||
MemoryArgs::Remove { old, .. } => {
|
||||
if let Some(problem) = entry_problem(old, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
}
|
||||
MemoryArgs::Note { text } => {
|
||||
if let Some(problem) = entry_problem(text, MAX_NOTE) {
|
||||
return Err(problem);
|
||||
}
|
||||
entry(old, MAX_ENTRY)?;
|
||||
entry(text, MAX_ENTRY)
|
||||
}
|
||||
MemoryArgs::Remove { old, .. } => entry(old, MAX_ENTRY),
|
||||
MemoryArgs::Note { text } => entry(text, MAX_NOTE),
|
||||
MemoryArgs::Consolidate { entries, .. } => {
|
||||
if entries.is_empty() {
|
||||
return Err("consolidate needs every entry of the file".to_string());
|
||||
}
|
||||
for line in entries {
|
||||
let action = line.action().map_err(|problem| problem.to_string())?;
|
||||
match action {
|
||||
match line.action().map_err(str::to_string)? {
|
||||
EntryAction::Keep(old)
|
||||
| EntryAction::Move(old)
|
||||
| EntryAction::Drop(old) => {
|
||||
if let Some(problem) = entry_problem(&old, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
}
|
||||
| EntryAction::Drop(old) => entry(&old, MAX_ENTRY)?,
|
||||
EntryAction::Rewrite { old, text } => {
|
||||
if let Some(problem) = entry_problem(&old, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
if let Some(problem) = entry_problem(&text, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
}
|
||||
EntryAction::New(text) => {
|
||||
if let Some(problem) = entry_problem(&text, MAX_ENTRY) {
|
||||
return Err(problem);
|
||||
}
|
||||
entry(&old, MAX_ENTRY)?;
|
||||
entry(&text, MAX_ENTRY)?;
|
||||
}
|
||||
EntryAction::New(text) => entry(&text, MAX_ENTRY)?,
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user