text/style/module.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
bb204cb8898afc6e08e11ec0b2ed11583ac1997a
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Immutable text styles. A style is a frozen semantic value describing how
//! text should be presented; it carries no rendering, terminal, or encoding
//! behavior of its own. A style's state is opaque: no slot, method, or other
//! public operation exposes it.
//!
//! Styles answer semantic equality through `==`: two styles are equal when
//! both are styles constructed from the same foreground. `hash` is coherent
//! with that equality, so independently constructed equal styles work as the
//! same Map key. `===` remains ordinary object identity, and every
//! construction answers a fresh style.
//!
//! Only values constructed by this module are styles. `recognizes` never
//! invokes behavior of the candidate, so an object that merely has similar
//! slots, parent, or shape is not a style.

/// Answers a fresh frozen style whose foreground is `foreground`, which must
/// be exactly one of the Strings `"default"`, `"black"`, `"red"`, `"green"`,
/// `"yellow"`, `"blue"`, `"magenta"`, `"cyan"`, or `"white"`. Any other
/// argument signals an Error; no argument is converted, case-folded, or
/// defaulted.
this.construct: ((facility) => (foreground) => {
    Style: import("std:text/style")

    String.recognizes(foreground).ifFalse(() => {
        Error().signal()
    })
    valid: false
    for(name in ["default", "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"]) {
        if(name == foreground) {
            valid = true
        }
    }
    valid.ifFalse(() => {
        Error().signal()
    })

    // Hashes are folded modulo a fixed prime so the accumulator stays bounded.
    mix: (accumulated, value) => {
        (accumulated * 31 + value) % 2305843009213693951
    }

    behavior: {
        equals: (other) => {
            style: this
            Style.recognizes(other) && (facility.state(style) == facility.state(other))
        }

        hash: () => {
            style: this
            mix(17, facility.state(style).hash())
        }
    }

    // The foreground is kept as runtime-private family state, never as a slot.
    template: (Style) {
        ...behavior.alias("equals", "==")
    }
    facility.seal(template, foreground)
})(_styleFacility)

/// Answers `true` exactly when `value` is a style constructed by this module.
/// Answers `false` for any other value, including this module and objects
/// with the same slots or parent. The candidate is neither copied, converted,
/// nor sent any message.
this.recognizes: ((facility) => (value) => {
    facility.recognizes(value)
})(_styleFacility)

// The runtime-provisioned facility is captured privately above; it is not
// module surface.
context.removeSlot("_styleFacility")