package com.zaojiao.app.data.local.user

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

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

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