-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move all plugin patternfly components into core
- Loading branch information
Showing
21 changed files
with
258 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1 @@ | ||
use kube::CustomResource; | ||
use schemars::JsonSchema; | ||
#[cfg(feature = "ui")] | ||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::task::CassetteTask; | ||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, CustomResource)] | ||
#[kube( | ||
group = "cassette.ulagbulag.io", | ||
version = "v1alpha1", | ||
kind = "CassetteComponent", | ||
root = "CassetteComponentCrd", | ||
shortname = "casc", | ||
namespaced, | ||
printcolumn = r#"{ | ||
"name": "created-at", | ||
"type": "date", | ||
"description": "created time", | ||
"jsonPath": ".metadata.creationTimestamp" | ||
}"#, | ||
printcolumn = r#"{ | ||
"name": "version", | ||
"type": "integer", | ||
"description": "component version", | ||
"jsonPath": ".metadata.generation" | ||
}"# | ||
)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct CassetteComponentSpec { | ||
#[serde(default)] | ||
pub tasks: Vec<CassetteTask>, | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
pub trait ComponentRenderer<Spec> { | ||
fn render( | ||
self, | ||
ctx: &mut crate::cassette::CassetteContext, | ||
spec: Spec, | ||
) -> crate::task::TaskResult<Option<Self>> | ||
where | ||
Self: Sized; | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
pub trait ComponentRendererExt<Spec> | ||
where | ||
Self: Default + Serialize + DeserializeOwned + ComponentRenderer<Spec>, | ||
Spec: DeserializeOwned, | ||
{ | ||
fn render_with( | ||
mut ctx: crate::cassette::CassetteContext, | ||
spec: &crate::task::TaskSpec, | ||
) -> crate::task::TaskResult<()> | ||
where | ||
Self: Sized, | ||
{ | ||
use serde_json::Value; | ||
|
||
fn replace_key( | ||
ctx: &crate::cassette::CassetteContext, | ||
spec: &crate::task::TaskSpec, | ||
value: &Value, | ||
) -> Result<Value, String> { | ||
match value { | ||
Value::Null => Ok(Value::Null), | ||
Value::Bool(data) => Ok(Value::Bool(*data)), | ||
Value::Number(data) => Ok(Value::Number(data.clone())), | ||
Value::String(data) => { | ||
if data.starts_with(":/") { | ||
ctx.get(&data[1..]).cloned() | ||
} else if data.starts_with("~/") { | ||
spec.get(&data[1..]).cloned() | ||
} else if data.starts_with("\\:/") || data.starts_with("\\~/") { | ||
Ok(Value::String(data[1..].into())) | ||
} else { | ||
Ok(Value::String(data.clone())) | ||
} | ||
} | ||
Value::Array(array) => array | ||
.iter() | ||
.map(|value| replace_key(ctx, spec, value)) | ||
.collect::<Result<_, _>>() | ||
.map(Value::Array), | ||
Value::Object(map) => map | ||
.iter() | ||
.map(|(key, value)| { | ||
replace_key(ctx, spec, value).map(|value| (key.clone(), value)) | ||
}) | ||
.collect::<Result<_, _>>() | ||
.map(Value::Object), | ||
} | ||
} | ||
|
||
let state = ctx.get_task_state()?.unwrap_or_default(); | ||
|
||
let spec = replace_key(&ctx, spec, &spec.0)?; | ||
let spec = ::serde_json::from_value(spec) | ||
.map_err(|error| format!("Failed to parse task spec: {error}"))?; | ||
|
||
let state = <Self as ComponentRenderer<Spec>>::render(state, &mut ctx, spec) | ||
.and_then(crate::task::TaskState::try_into_spec)?; | ||
Ok(ctx.set(state)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
impl<Spec, T> ComponentRendererExt<Spec> for T | ||
where | ||
Self: Default + Serialize + DeserializeOwned + ComponentRenderer<Spec>, | ||
Spec: DeserializeOwned, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use patternfly_yew::prelude::*; | ||
use yew::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Properties)] | ||
pub struct Props { | ||
pub msg: AttrValue, | ||
} | ||
|
||
#[function_component(Error)] | ||
pub fn error(props: &Props) -> Html { | ||
let Props { msg } = props; | ||
|
||
html! { | ||
<Content> | ||
<p>{ msg }</p> | ||
</Content> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
use patternfly_yew::prelude::*; | ||
use yew::prelude::*; | ||
|
||
#[function_component(Loading)] | ||
pub fn loading() -> Html { | ||
html! { | ||
<Flex > | ||
<FlexItem> | ||
<Spinner size={ SpinnerSize::Lg } /> | ||
</FlexItem> | ||
<FlexItem modifiers={[ FlexModifier::Align(Alignment::Center) ]}> | ||
<Content> | ||
<p>{ "Loading..." }</p> | ||
</Content> | ||
</FlexItem> | ||
</Flex> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#[cfg(feature = "ui")] | ||
pub mod error; | ||
#[cfg(feature = "ui")] | ||
pub mod loading; | ||
|
||
use kube::CustomResource; | ||
use schemars::JsonSchema; | ||
#[cfg(feature = "ui")] | ||
use serde::de::DeserializeOwned; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::task::CassetteTask; | ||
|
||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema, CustomResource)] | ||
#[kube( | ||
group = "cassette.ulagbulag.io", | ||
version = "v1alpha1", | ||
kind = "CassetteComponent", | ||
root = "CassetteComponentCrd", | ||
shortname = "casc", | ||
namespaced, | ||
printcolumn = r#"{ | ||
"name": "created-at", | ||
"type": "date", | ||
"description": "created time", | ||
"jsonPath": ".metadata.creationTimestamp" | ||
}"#, | ||
printcolumn = r#"{ | ||
"name": "version", | ||
"type": "integer", | ||
"description": "component version", | ||
"jsonPath": ".metadata.generation" | ||
}"# | ||
)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct CassetteComponentSpec { | ||
#[serde(default)] | ||
pub tasks: Vec<CassetteTask>, | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
pub trait ComponentRenderer<Spec> { | ||
fn render( | ||
self, | ||
ctx: &mut crate::cassette::CassetteContext, | ||
spec: Spec, | ||
) -> crate::task::TaskResult<Option<Self>> | ||
where | ||
Self: Sized; | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
pub trait ComponentRendererExt<Spec> | ||
where | ||
Self: Default + Serialize + DeserializeOwned + ComponentRenderer<Spec>, | ||
Spec: DeserializeOwned, | ||
{ | ||
fn render_with( | ||
mut ctx: crate::cassette::CassetteContext, | ||
spec: &crate::task::TaskSpec, | ||
) -> crate::task::TaskResult<()> | ||
where | ||
Self: Sized, | ||
{ | ||
use serde_json::Value; | ||
|
||
fn replace_key( | ||
ctx: &crate::cassette::CassetteContext, | ||
spec: &crate::task::TaskSpec, | ||
value: &Value, | ||
) -> Result<Value, String> { | ||
match value { | ||
Value::Null => Ok(Value::Null), | ||
Value::Bool(data) => Ok(Value::Bool(*data)), | ||
Value::Number(data) => Ok(Value::Number(data.clone())), | ||
Value::String(data) => { | ||
if data.starts_with(":/") { | ||
ctx.get(&data[1..]).cloned() | ||
} else if data.starts_with("~/") { | ||
spec.get(&data[1..]).cloned() | ||
} else if data.starts_with("\\:/") || data.starts_with("\\~/") { | ||
Ok(Value::String(data[1..].into())) | ||
} else { | ||
Ok(Value::String(data.clone())) | ||
} | ||
} | ||
Value::Array(array) => array | ||
.iter() | ||
.map(|value| replace_key(ctx, spec, value)) | ||
.collect::<Result<_, _>>() | ||
.map(Value::Array), | ||
Value::Object(map) => map | ||
.iter() | ||
.map(|(key, value)| { | ||
replace_key(ctx, spec, value).map(|value| (key.clone(), value)) | ||
}) | ||
.collect::<Result<_, _>>() | ||
.map(Value::Object), | ||
} | ||
} | ||
|
||
let state = ctx.get_task_state()?.unwrap_or_default(); | ||
|
||
let spec = replace_key(&ctx, spec, &spec.0)?; | ||
let spec = ::serde_json::from_value(spec) | ||
.map_err(|error| format!("Failed to parse task spec: {error}"))?; | ||
|
||
let state = <Self as ComponentRenderer<Spec>>::render(state, &mut ctx, spec) | ||
.and_then(crate::task::TaskState::try_into_spec)?; | ||
Ok(ctx.set(state)) | ||
} | ||
} | ||
|
||
#[cfg(feature = "ui")] | ||
impl<Spec, T> ComponentRendererExt<Spec> for T | ||
where | ||
Self: Default + Serialize + DeserializeOwned + ComponentRenderer<Spec>, | ||
Spec: DeserializeOwned, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
pub mod cassette; | ||
pub mod component; | ||
pub mod components; | ||
pub mod document; | ||
pub mod net; | ||
pub mod result; | ||
pub mod task; | ||
|
||
#[cfg(feature = "ui")] | ||
pub mod prelude { | ||
pub use crate::components::{error::Error, loading::Loading}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.