package com.zaojiao.app.data.local.baby

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

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

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