Dependency-free HTTP representation negotiation toolkit for MoonBit.
moon add XiaoMao-MYY/acceptkitXiaoMao-MYY/acceptkitimport {
"XiaoMao-MYY/acceptkit" @acceptkit,
}fn choose() -> String {
let result = @acceptkit.negotiate_response(
@acceptkit.RequestPreferences(
accept="application/json, text/html;q=0.6",
accept_language="zh-CN, en;q=0.8",
accept_encoding="br, gzip;q=0.7",
accept_charset="utf-8",
),
["text/html", "application/json"],
["en-US", "zh-CN"],
["gzip", "br"],
["utf-8"],
)
match result {
Ok(choice) => @acceptkit.response_header_block(choice.summary)
Err(_) => "invalid negotiation header"
}
}moon check
moon build
moon test
moon run cmd/mainmoon check
moon build
moon test
moon run cmd/main
moon publish --dry-runpub(all) struct CachePolicySummary {
no_store : Bool
no_cache : Bool
is_public : Bool
is_private : Bool
must_revalidate : Bool
immutable : Bool
max_age : Int?
s_maxage : Int?
stale_while_revalidate : Int?
stale_if_error : Int?
unknown : Array[CacheDirective]
} derive(Eq, Debug)pub(all) struct CharsetCandidate {
offer : CharsetOffer
matched_range : CharsetRange
quality : Int
specificity : Int
reason : String
} derive(Eq, Debug)pub(all) struct CharsetDecision {
selected : String?
quality : Int
candidate : CharsetCandidate?
matches : Array[CharsetCandidate]
explanation : String
} derive(Eq, Debug)pub(all) struct EncodingCandidate {
offer : EncodingOffer
matched_range : EncodingRange
quality : Int
specificity : Int
reason : String
} derive(Eq, Debug)pub(all) struct EncodingDecision {
selected : String?
quality : Int
candidate : EncodingCandidate?
matches : Array[EncodingCandidate]
explanation : String
} derive(Eq, Debug)pub(all) struct LanguageCandidate {
offer : LanguageOffer
matched_range : LanguageRange
quality : Int
specificity : Int
reason : String
} derive(Eq, Debug)pub(all) struct LanguageDecision {
selected : String?
quality : Int
candidate : LanguageCandidate?
matches : Array[LanguageCandidate]
explanation : String
} derive(Eq, Debug)pub(all) struct MediaCandidate {
offer : MediaOffer
matched_range : MediaRange
quality : Int
specificity : Int
parameter_matches : Int
reason : String
} derive(Eq, Debug)pub(all) struct MediaDecision {
selected : String?
quality : Int
candidate : MediaCandidate?
matches : Array[MediaCandidate]
explanation : String
} derive(Eq, Debug)fn RequestPreferences::RequestPreferences(accept? : StringView, accept_language? : StringView, accept_encoding? : StringView, accept_charset? : StringView) -> RequestPreferencespub(all) struct ResponseNegotiation {
decision : VariantDecision
summary : ResponseSummary
headers : Array[Param]
header_block : String
} derive(Eq, Debug)fn ServerVariant::ServerVariant(id : StringView, media : StringView, language? : StringView, encoding? : StringView, charset? : StringView, server_weight? : Int, index? : Int) -> ServerVariantpub(all) struct VariantCandidate {
variant : ServerVariant
media : MediaCandidate?
language : LanguageCandidate?
encoding : EncodingCandidate?
charset : CharsetCandidate?
quality : Int
reason : String
} derive(Eq, Debug)pub(all) struct VariantDecision {
selected : ServerVariant?
quality : Int
matches : Array[VariantCandidate]
vary : Array[String]
explanation : String
} derive(Eq, Debug)fn charset_is_acceptable(accept_charset : StringView, charset : StringView) -> Result[Bool, ParseError]fn encoding_is_acceptable(accept_encoding : StringView, coding : StringView) -> Result[Bool, ParseError]fn etag_list_matches(header_value : StringView, current : EntityTag, strong? : Bool) -> Result[Bool, ParseError]fn evaluate_if_match(header_value : StringView, current : EntityTag) -> Result[ConditionalDecision, ParseError]fn evaluate_if_none_match(http_method : StringView, header_value : StringView, current : EntityTag) -> Result[ConditionalDecision, ParseError]fn explain_charset_candidates(accept_charset : StringView, offers : Array[String]) -> Result[Array[String], ParseError]fn explain_encoding_candidates(accept_encoding : StringView, offers : Array[String]) -> Result[Array[String], ParseError]fn explain_language_candidates(accept_language : StringView, offers : Array[String]) -> Result[Array[String], ParseError]fn explain_media_candidates(accept_header : StringView, offers : Array[String]) -> Result[Array[String], ParseError]fn negotiate_charset(accept_charset : StringView, offers : Array[String]) -> Result[CharsetDecision, ParseError]fn negotiate_encoding(accept_encoding : StringView, offers : Array[String]) -> Result[EncodingDecision, ParseError]fn negotiate_language(accept_language : StringView, offers : Array[String]) -> Result[LanguageDecision, ParseError]fn negotiate_media(accept_header : StringView, offers : Array[String]) -> Result[MediaDecision, ParseError]fn negotiate_response(preferences : RequestPreferences, media_offers : Array[String], language_offers : Array[String], encoding_offers : Array[String], charset_offers : Array[String]) -> Result[ResponseNegotiation, ParseError]fn negotiate_response_summary(preferences : RequestPreferences, media_offers : Array[String], language_offers : Array[String], encoding_offers : Array[String], charset_offers : Array[String]) -> Result[ResponseSummary, ParseError]fn negotiate_variant(preferences : RequestPreferences, variants : Array[ServerVariant]) -> Result[VariantDecision, ParseError]fn negotiation_matrix_report(preferences : RequestPreferences, variants : Array[ServerVariant]) -> Result[String, ParseError]fn summarize_cache_control(directives : Array[CacheDirective]) -> Result[CachePolicySummary, ParseError]Dependency-free HTTP representation negotiation toolkit for MoonBit.