package com.zaojiao.app.data.local.settings

import androidx.datastore.core.CorruptionException
import androidx.datastore.core.Serializer
import com.zaojiao.app.data.local.SettingsPreferences
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import javax.inject.Inject

class SettingsPreferencesSerializer @Inject constructor() : Serializer<SettingsPreferences> {
    override val defaultValue: SettingsPreferences get() = SettingsPreferences.getDefaultInstance()
    override suspend fun readFrom(input: InputStream): SettingsPreferences {
        try {
            return SettingsPreferences.parseFrom(input)
        } catch (exception: IOException) {
            throw CorruptionException("OPEN SETTINGS PROTO FAILURE", exception)
        }
    }

    override suspend fun writeTo(t: SettingsPreferences, output: OutputStream) {
        t.writeTo(output)
    }
}