Skip to content

Conversation

@david-allison
Copy link
Member

Purpose / Description

https://github.com/ankitects/anki/blob/a6d5c949970627f2b4dcea8a02fea3a497e0440f/pylib/anki/decks.py

Approach

Go through each method

  • ensure implementation matches
  • implement some methods
  • add RustCleanup on others

How Has This Been Tested?

Unit tested

Checklist

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

Copy link
Member

@lukstbit lukstbit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a note, but everything else LGTM.

TODO()
}
@Suppress("unused")
fun nameIfExists(did: DeckId): String? = getLegacy(did)?.name
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should implement nameIfExists and name as they are in the backend(get() is the same) to not have to update them once again when get() has the proper implementation.

This is why I'm not fond of implementing these methods ahead of actually using them.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented get; fixed name and nameIfExists

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted the get() changes due to test failure. Would you like these resolved in another manner?

git reset 28298a3265d29700c15e7cbeddddc6fd88dd35ec

DecksTest > test_remove FAILED
    org.opentest4j.AssertionFailedError: expected: <[no deck]> but was: <Default>
        at app//org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
        at app//org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
        at app//org.junit.jupiter.api.AssertEquals.failNotEqual(AssertEquals.java:197)
        at app//org.junit.jupiter.api.AssertEquals.assertEquals(AssertEquals.java:182)
        at app//org.junit.jupiter.api.Assertions.assertEquals(Assertions.java:1156)
        at app//kotlin.test.junit5.JUnit5Asserter.assertEquals(JUnitSupport.kt:32)
        at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals(Assertions.kt:63)
        at app//kotlin.test.AssertionsKt.assertEquals(Unknown Source)
        at app//kotlin.test.AssertionsKt__AssertionsKt.assertEquals$default(Assertions.kt:62)
        at app//kotlin.test.AssertionsKt.assertEquals$default(Unknown Source)
        at app//com.ichi2.anki.libanki.DecksTest.test_remove(DecksTest.kt:48)
Index: libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt b/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt
--- a/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt	(revision 28298a3265d29700c15e7cbeddddc6fd88dd35ec)
+++ b/libanki/src/main/java/com/ichi2/anki/libanki/Decks.kt	(date 1760665574575)
@@ -38,6 +38,7 @@
 import anki.decks.copy
 import com.ichi2.anki.common.annotations.NeedsTest
 import com.ichi2.anki.common.utils.ext.jsonObjectIterable
+import com.ichi2.anki.libanki.Consts.DEFAULT_DECK_ID
 import com.ichi2.anki.libanki.backend.BackendUtils
 import com.ichi2.anki.libanki.backend.BackendUtils.fromJsonBytes
 import com.ichi2.anki.libanki.backend.BackendUtils.toJsonBytes
@@ -289,19 +290,29 @@
         return col.db.queryScalar("select count() from cards where did in $strIds or odid in $strIds")
     }
 
-    @RustCleanup("implement and make public")
     @LibAnkiAlias("get")
-    @Suppress("unused", "unused_parameter")
+    @Suppress("unused", "unused_parameter", "LiftReturnOrAssignment", "RedundantElseInIf")
     @CheckResult
-    private fun get(
-        did: DeckId,
+    fun get(
+        did: DeckId?,
         default: Boolean = true,
-    ): Deck? =
-        try {
-            Deck(BackendUtils.fromJsonBytes(col.backend.getDeckLegacy(did)))
-        } catch (ex: BackendNotFoundException) {
-            null
+    ): Deck? {
+        if (did == null) {
+            if (default) {
+                return getLegacy(DEFAULT_DECK_ID)
+            } else {
+                return null
+            }
         }
+        val deck = getLegacy(did)
+        if (deck != null) {
+            return deck
+        } else if (default) {
+            return getLegacy(DEFAULT_DECK_ID)
+        } else {
+            return null
+        }
+    }
 
     /** Get deck with NAME, ignoring case. */
     @LibAnkiAlias("by_name")
@@ -494,13 +505,13 @@
     /** Returns the [deck name][Deck.name], or 'no deck' if not found */
     @LibAnkiAlias("name")
     @CheckResult
-    fun name(did: DeckId): String = getLegacy(did)?.name ?: col.backend.tr.decksNoDeck()
+    fun name(did: DeckId): String = get(did)?.name ?: col.backend.tr.decksNoDeck()
 
     /** Returns the [deck name][Deck.name], or `null` if not found */
     @LibAnkiAlias("name_if_exists")
     @Suppress("unused")
     @CheckResult
-    fun nameIfExists(did: DeckId): String? = getLegacy(did)?.name
+    fun nameIfExists(did: DeckId): String? = get(did)?.name
 
     @RustCleanup("implement and make public")
     @Suppress("unused", "unused_parameter")

Copy link
Member

@lukstbit lukstbit Oct 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Decks.get() is ok
Decks.name() should include the boolean default parameter like on backend
Decks.nameIfExists() should call name() with false like on backend

Then we could make the correct call for that test to pass.

@lukstbit lukstbit added Needs Author Reply Waiting for a reply from the original author Needs Second Approval Has one approval, one more approval to merge and removed Needs Review labels Sep 9, 2025
@david-allison david-allison removed the Needs Author Reply Waiting for a reply from the original author label Sep 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Second Approval Has one approval, one more approval to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants