Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement config feature #47

Draft
wants to merge 4 commits into
base: main-dev
Choose a base branch
from
Draft

Implement config feature #47

wants to merge 4 commits into from

Conversation

SpriteOvO
Copy link
Owner

@SpriteOvO SpriteOvO commented Nov 14, 2023

This is an early implementation of #25 in progress. It is currently very incomplete, just for previewing its API and approach.

Note to self: Remember to change the base branch to main or main-dev before merging.


We mainly introduced a new trait:

pub trait Configurable: Sized {
    type Params: DeserializeOwned + Default + Send;

    fn metadata() -> ComponentMetadata;
    fn build(params: Self::Params) -> Result<Self>;
}

then we implement it for built-in sinks and formatters like:

impl Configurable for FileSink {
    type Params = FileSinkParams;

    fn metadata() -> ComponentMetadata {
        ComponentMetadata { name: "FileSink" }
    }

    fn build(params: Self::Params) -> Result<Self> {
        let mut builder = FileSink::builder()
            .level_filter(params.0.common_builder_impl.level_filter)
            .path(params.0.path)
            .truncate(params.0.truncate);
        if let Some(formatter) = params.0.common_builder_impl.formatter {
            builder = builder.formatter(formatter);
        }
        builder.build()
    }
}
impl Configurable for PatternFormatter<RuntimePattern> {
    type Params = PatternFormatterRuntimePatternParams;

    fn metadata() -> ComponentMetadata {
        ComponentMetadata {
            name: "PatternFormatter",
        }
    }

    fn build(params: Self::Params) -> Result<Self> {
        Ok(Self::new(RuntimePattern::new(params.template)?))
    }
}

Of course, users can implement this trait for their own sinks and formatters.

With the power of erased-serde, we can cast the type Params to a dyn erased_serde::Deserializer to be used later. (Check config/registry.rs)

When we deserializing a sink or formatter, we will first read the name field, then look it up from the registry, if found, deserializing the rest of the fields using the erased stored Deserializer. (Check config/parse.rs)


This whole deserialization process will be very hidden to users! trait Configurable and .register_xxx() is the only thing they need to care about if they have their own sink / formatter.

@SpriteOvO SpriteOvO marked this pull request as draft November 14, 2023 18:26
@SpriteOvO SpriteOvO added this to the v0.4.0 milestone Dec 2, 2023
@SpriteOvO SpriteOvO force-pushed the runtime-pattern branch 2 times, most recently from a2b7cbe to 71b4fd2 Compare March 9, 2024 22:48
@SpriteOvO SpriteOvO force-pushed the runtime-pattern branch 4 times, most recently from 38cf870 to 62d2abf Compare March 20, 2024 21:04
@SpriteOvO SpriteOvO force-pushed the runtime-pattern branch 2 times, most recently from 03da466 to 0c4d130 Compare March 27, 2024 20:59
@SpriteOvO SpriteOvO changed the base branch from runtime-pattern to main-dev March 27, 2024 21:00
@SpriteOvO
Copy link
Owner Author

There's still a lot of work to be done on this, and I'd like to delay it to v0.5.0. This way we can release v0.4.0 earlier to provide many of the small fixes, since backporting existing fixes has been a bit difficult.

@SpriteOvO SpriteOvO modified the milestones: v0.4.0, v0.5.0 Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant