05nelsonm / kmp-tor

Kotlin Multiplatform Library for embedding Tor into your applications
Apache License 2.0
33 stars 5 forks source link

Refactor `TorConfig` #515

Closed 05nelsonm closed 1 month ago

05nelsonm commented 2 months ago

I hate it, everything is in a single class, and builders are far too coupled with their Keyword.

There should be a separate class that is just for the Keyword. Also, it should use the same terminology as tor-man and call it Option Name and Option Value, or something similar.

e.g.

public abstract class TorOption private constructor(
    @JvmField
    public val default: String,
    @JvmField
    public val attributes: Set<Attribute>,
    @JvmField
    public val isCmdLineArg: Boolean,
    @JvmField
    public val isUnique: Boolean,
) {

    @get:JvmName("name")
    public val name: String get() = toString()

    public data object DisableNetwork: TorOption(
        default = false.byte.toString(),
        attributes = emptySet(),
        isCmdLineArg = true,
        isUnique = true,
    )

    // ...
}

Then, separate classes can be had for using that TorOption in a configuration. Keeping TorOption objects as clean as possible.

public class TorSetting private constructor(
    @JvmField
    public val items: Set<LineItem>,
    @JvmField
    public val extras: Map<String, Any>,
) {

    public class LineItem private constructor(
        @JvmField
        public val option: TorOption,
        @JvmField
        public val argument: String,
        @JvmField
        public val optionals: Set<String>,
    ) {

        public companion object {
            // TODO: static factory functions
            //  TorOption.toLineItem()
            //  TorOption.toLineItemOrNull()
        }
    }

    public companion object {
        // TODO: static factory functions
        //  LineItem.toSetting()
        //  LineItem.toSettingOrNull()
        //  Set<LineItem>.toSetting()
        //  Set<LineItem>.toSettingOrNull()
    }
}