Skip to content

Commit

Permalink
Provide the same functions of Permissions to MessageFlags (#203)
Browse files Browse the repository at this point in the history
* Provide the same functions of Permissions to MessageFlags

* Apply suggestions

* Change toString to indicate the type as well

Co-authored-by: Bart Arys <[email protected]>

Co-authored-by: Bart Arys <[email protected]>
  • Loading branch information
HopeBaron and BartArys authored Mar 10, 2021
1 parent 62ab68e commit cae8b41
Showing 1 changed file with 46 additions and 11 deletions.
57 changes: 46 additions & 11 deletions common/src/main/kotlin/entity/DiscordMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -284,32 +284,57 @@ enum class MessageFlag(val code: Int) {
}

@Serializable(with = MessageFlags.Serializer::class)
data class MessageFlags internal constructor(val code: Int) {
class MessageFlags internal constructor(val code: Int) {

val flags = MessageFlag.values().filter { code and it.code != 0 }

operator fun contains(flag: MessageFlag) = flag in flags
operator fun contains(flag: MessageFlag) = flag.code and this.code == flag.code

operator fun plus(flags: MessageFlags): MessageFlags = when {
code and flags.code == flags.code -> this
else -> MessageFlags(this.code or flags.code)
}
operator fun contains(flags: MessageFlags) = flags.code and this.code == flags.code

operator fun plus(flags: MessageFlags): MessageFlags = MessageFlags(this.code or flags.code)

operator fun plus(flags: MessageFlag): MessageFlags = MessageFlags(this.code or flags.code)

operator fun minus(flags: MessageFlags): MessageFlags = MessageFlags(this.code xor flags.code)

operator fun minus(flags: MessageFlag): MessageFlags = MessageFlags(this.code xor flags.code)

operator fun minus(flag: MessageFlag): MessageFlags = when {
code and flag.code == flag.code -> MessageFlags(code xor flag.code)
else -> this
}

inline fun copy(block: Builder.() -> Unit): MessageFlags {
val builder = Builder(code)
builder.apply(block)
return builder.flags()
}

override fun toString(): String = "MessageFlags(flags=${flags.toString()})"

companion object {
inline operator fun invoke(builder: Builder.() -> Unit): MessageFlags {
return Builder().apply(builder).flags()
}

operator fun invoke(value: Int) = MessageFlags(value)

operator fun invoke(vararg flags: MessageFlag) = MessageFlags {
flags.forEach { +it }
}

operator fun invoke(vararg flags: MessageFlags) = MessageFlags {
flags.forEach { +it }
}

operator fun invoke(flags: Iterable<MessageFlag>) = MessageFlags {
flags.forEach { +it }
}


@JvmName("invokeWithFlags")
operator fun invoke(flags: Iterable<MessageFlags>) = MessageFlags {
flags.forEach { +it }
}


}

internal object Serializer : KSerializer<MessageFlags> {
Expand Down Expand Up @@ -337,6 +362,16 @@ data class MessageFlags internal constructor(val code: Int) {
}
}

operator fun MessageFlags.unaryPlus() {
this@Builder.code = this@Builder.code or code
}

operator fun MessageFlags.unaryMinus() {
if (this@Builder.code and code == code) {
this@Builder.code = this@Builder.code xor code
}
}

fun flags() = MessageFlags(code)
}

Expand Down Expand Up @@ -747,4 +782,4 @@ data class AllowedMentions(
val roles: List<String>,
@SerialName("replied_user")
val repliedUser: OptionalBoolean = OptionalBoolean.Missing
)
)

0 comments on commit cae8b41

Please sign in to comment.