Read and write view rules through the shared rule codec (#59)
This commit is contained in:
1 parent
bb6e027011
commit
e7b726ee82
4 files changed
+38
-8
No files matched your search
@@ -129,7 +129,8 @@ internal object SavedFilterCodec {
|
||||
return here.mapNotNull { it.removePrefix("$mode:").toLongOrNull() }.toSet() to elsewhere.toSet()
|
||||
}
|
||||
|
||||
private fun encodeRule(rule: Rule, mode: String): String = when (rule) {
|
||||
/** [rule] in the stored grammar, [mode]'s list ids qualified by it; views store their rules this way too. */
|
||||
fun encodeRule(rule: Rule, mode: String): String = when (rule) {
|
||||
is Rule.AllOf -> "and(${rule.children.joinToString(",") { encodeRule(it, mode) }})"
|
||||
is Rule.AnyOf -> "or(${rule.children.joinToString(",") { encodeRule(it, mode) }})"
|
||||
is Rule.Not -> "not(${encodeRule(rule.child, mode)})"
|
||||
@@ -147,6 +148,9 @@ internal object SavedFilterCodec {
|
||||
}
|
||||
}
|
||||
|
||||
/** The rule [text] holds, exactly as [encodeRule] wrote it, with [mode]'s list ids; throws on anything else. */
|
||||
fun decodeRule(text: String, mode: String): Rule = RuleParser(text, mode).parse()
|
||||
|
||||
/** Reads what [encodeRule] writes; throws on anything else. */
|
||||
private class RuleParser(private val text: String, private val mode: String) {
|
||||
private var at = 0
|
||||
|
||||
@@ -6,7 +6,6 @@ import de.jeanlucmakiola.agendula.domain.BlockSource
|
||||
import de.jeanlucmakiola.agendula.domain.BlockType
|
||||
import de.jeanlucmakiola.agendula.domain.BlockWidth
|
||||
import de.jeanlucmakiola.agendula.domain.Rule
|
||||
import de.jeanlucmakiola.agendula.domain.SavedFilter
|
||||
import de.jeanlucmakiola.agendula.domain.SmartList
|
||||
import de.jeanlucmakiola.agendula.domain.TaskSortOrder
|
||||
import de.jeanlucmakiola.agendula.domain.TileColor
|
||||
@@ -212,13 +211,9 @@ internal object ViewCodec {
|
||||
private fun pairs(text: String): Map<String, String> = text.split(';').filter { '=' in it }
|
||||
.associate { it.substringBefore('=') to it.substringAfter('=') }
|
||||
|
||||
// The rule grammar is the saved filters': written and read through their codec, as the rule of a
|
||||
// line. Reading one applies that codec's top-level list pruning, the same one withoutLists applies.
|
||||
private fun encodeRule(rule: Rule, mode: String): String =
|
||||
SavedFilterCodec.upsert(null, SavedFilter(0, "", rule), mode).split('|', limit = 4)[3]
|
||||
private fun encodeRule(rule: Rule, mode: String): String = SavedFilterCodec.encodeRule(rule, mode)
|
||||
|
||||
private fun decodeRule(text: String, mode: String): Rule =
|
||||
SavedFilterCodec.decode("v2|0||$text", mode).single().rule
|
||||
private fun decodeRule(text: String, mode: String): Rule = SavedFilterCodec.decodeRule(text, mode)
|
||||
|
||||
private fun flag(value: Boolean): String = if (value) "1" else "0"
|
||||
|
||||
|
||||
@@ -14,6 +14,24 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
class SavedFilterCodecTest {
|
||||
|
||||
@Test
|
||||
fun `the stored line stays byte for byte, and its rule reads back through decodeRule`() {
|
||||
val rule = Rule.AllOf(
|
||||
listOf(
|
||||
Rule.AnyOf(listOf(Rule.InList(setOf(4L, 2L), setOf("EXTERNAL:7")), Rule.HasTag("a b,c"))),
|
||||
Rule.Not(Rule.Due(DueSpec.Window(DueWindow.NO_DATE))),
|
||||
Rule.MinPriority(Priority.HIGH),
|
||||
Rule.Due(DueSpec.ThisWeek),
|
||||
Rule.Due(DueSpec.NextDays(7)),
|
||||
),
|
||||
)
|
||||
val text = "and(or(list(OWN%3A2,OWN%3A4,EXTERNAL%3A7),tag(a+b%2Cc)),not(due(NO_DATE)),prio(HIGH),range(week),range(next,7))"
|
||||
assertThat(SavedFilterCodec.upsert(null, SavedFilter(3, "Work | x", rule), "OWN")).isEqualTo("v2|3|Work+%7C+x|$text")
|
||||
assertThat(SavedFilterCodec.encodeRule(rule, "OWN")).isEqualTo(text)
|
||||
assertThat(SavedFilterCodec.decodeRule(text, "OWN")).isEqualTo(rule)
|
||||
assertThat(SavedFilterCodec.decodeRule("list()", "OWN")).isEqualTo(Rule.InList(emptySet()))
|
||||
}
|
||||
|
||||
private val criteria = FilterCriteria(
|
||||
due = setOf(DueWindow.TODAY, DueWindow.OVERDUE),
|
||||
listIds = setOf(4L, 9L),
|
||||
|
||||
@@ -97,6 +97,19 @@ class ViewCodecTest {
|
||||
assertThat(odd.blocks.single().source).isEqualTo(BlockSource.ListElsewhere("OWN:abc"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a rule reads back exactly as written, with no saved-filter pruning`() {
|
||||
val scopes = listOf(
|
||||
Rule.InList(emptySet()),
|
||||
Rule.AllOf(listOf(Rule.InList(emptySet()), Rule.HasTag("x"))),
|
||||
Rule.AllOf(listOf(Rule.InList(emptySet(), setOf("EXTERNAL:7")))),
|
||||
)
|
||||
scopes.forEachIndexed { i, scope ->
|
||||
val view = View(i + 1L, "x", ViewIcon.HOME, scope, listOf(Block(1, BlockType.TASK_LIST, BlockSource.Rules(scope))))
|
||||
assertThat(ViewCodec.decode(ViewCodec.encode(listOf(view), "OWN"), "OWN")).containsExactly(view)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `nothing stored, or another version, reads as no views`() {
|
||||
assertThat(ViewCodec.decode(null, "OWN")).isEmpty()
|
||||
|
||||
Reference in new issue
Block a user