11//! This module contains [`ComponentDiff`].
2-
32use serde:: { Deserialize , Serialize } ;
43use serde_json:: json;
54
5+ use crate :: code_view:: { CodeLanguage , CodeView } ;
6+ use crate :: component:: properties:: ComponentProperties ;
67use crate :: component:: ComponentResult ;
7- use crate :: {
8- CodeLanguage , CodeView , Component , ComponentError , ComponentId , ComponentView ,
9- ComponentViewProperties , DalContext , StandardModel ,
10- } ;
8+ use crate :: { Component , ComponentId , DalContext } ;
119
10+ //
1211const NEWLINE : & str = "\n " ;
1312
14- // NOTE(nick): while the destination is the browser, we may want to consider platform-specific
15- // newline characters.
16- // #[cfg(target_os != "windows")]
17- // const NEWLINE: &str = "\n";
18- // #[cfg(target_os = "windows")]
19- // const NEWLINE: &str = "\r\n";
20-
2113/// Contains the "diffs" for a given [`Component`](crate::Component). Generated by
2214/// [`Self::new()`].
2315#[ derive( Deserialize , Serialize , Debug ) ]
@@ -34,66 +26,55 @@ pub struct ComponentDiff {
3426 pub diffs : Vec < CodeView > ,
3527}
3628
37- impl ComponentDiff {
38- pub async fn new ( ctx : & DalContext , component_id : ComponentId ) -> ComponentResult < Self > {
39- // We take a clone of the original ctx for comparisons against the head visibility.
40- // Importantly, this `head_ctx` will be dropped at the end of this function and will not
41- // live any longer (that is, it's garbage collected at a reasonable time)
42- let head_ctx = ctx. clone_with_head ( ) ;
43-
44- if ctx. visibility ( ) . deleted_at . is_some ( ) {
45- return Err ( ComponentError :: InvalidContextForDiff ) ;
29+ impl Component {
30+ pub async fn get_diff (
31+ ctx : & DalContext ,
32+ component_id : ComponentId ,
33+ ) -> ComponentResult < ComponentDiff > {
34+ let component = Self :: get_by_id ( ctx, component_id) . await ?;
35+ let curr_json: String ;
36+ let materialized_view = component. materialized_view ( ctx) . await ?;
37+ if let Some ( view) = materialized_view {
38+ let mut current_component_view = ComponentProperties :: try_from ( view) ?;
39+ current_component_view. drop_private ( ) ;
40+ curr_json = serde_json:: to_string_pretty ( & current_component_view) ?;
41+ } else {
42+ curr_json = serde_json:: to_string_pretty ( & json ! ( null) ) ?;
4643 }
4744
48- let curr_component_view = ComponentView :: new ( ctx, component_id) . await ?;
49- if curr_component_view. properties . is_null ( ) {
50- return Ok ( Self {
45+ let head_ctx = ctx. clone_with_head ( ) . await ?;
46+ if ctx. change_set_id ( ) == head_ctx. get_workspace_default_change_set_id ( ) . await ? {
47+ // We are on HEAD and need to react as so!
48+ return Ok ( ComponentDiff {
5149 component_id,
52- current : CodeView :: new ( CodeLanguage :: Json , Some ( "{}" . to_owned ( ) ) , None , None ) ,
53- diffs : Vec :: new ( ) ,
50+ current : CodeView :: assemble ( CodeLanguage :: Json , Some ( curr_json ) , None , None ) ,
51+ diffs : vec ! [ ] ,
5452 } ) ;
5553 }
5654
57- let mut curr_component_view = ComponentViewProperties :: try_from ( curr_component_view) ?;
58- curr_component_view. drop_private ( ) ;
59-
60- let curr_json = serde_json:: to_string_pretty ( & curr_component_view) ?;
61-
62- if ctx. visibility ( ) . is_head ( ) {
63- return Ok ( Self {
64- component_id,
65- current : CodeView :: new ( CodeLanguage :: Json , Some ( curr_json) , None , None ) ,
66- diffs : Vec :: new ( ) ,
67- } ) ;
68- }
69-
70- // Find the "diffs" given the head dal context only if the component exists on head.
71- let mut is_new_component = false ;
72- let prev_json: String ;
73- if Component :: get_by_id ( & head_ctx, & component_id)
74- . await ?
75- . is_some ( )
76- {
77- let prev_component_view = ComponentView :: new ( & head_ctx, component_id) . await ?;
78- if prev_component_view. properties . is_null ( ) {
79- return Ok ( Self {
80- component_id,
81- current : CodeView :: new ( CodeLanguage :: Json , Some ( curr_json) , None , None ) ,
82- diffs : Vec :: new ( ) ,
83- } ) ;
55+ let head_json: String ;
56+ let mut is_new_comp = false ;
57+ let head_component = Self :: get_by_id ( & head_ctx, component_id) . await ;
58+ match head_component {
59+ Ok ( comp) => {
60+ let materialized_view = comp. materialized_view ( & head_ctx) . await ?;
61+ if let Some ( view) = materialized_view {
62+ let mut head_component_view = ComponentProperties :: try_from ( view) ?;
63+ head_component_view. drop_private ( ) ;
64+ head_component_view. drop_private ( ) ;
65+ head_json = serde_json:: to_string_pretty ( & head_component_view) ?;
66+ } else {
67+ head_json = serde_json:: to_string_pretty ( & json ! ( null) ) ?;
68+ }
8469 }
85-
86- let mut prev_component_view = ComponentViewProperties :: try_from ( prev_component_view) ?;
87- prev_component_view. drop_private ( ) ;
88-
89- prev_json = serde_json:: to_string_pretty ( & prev_component_view) ?;
90- } else {
91- is_new_component = true ;
92- prev_json = serde_json:: to_string_pretty ( & json ! ( null) ) ?;
93- } ;
70+ Err ( _) => {
71+ is_new_comp = true ;
72+ head_json = serde_json:: to_string_pretty ( & json ! ( null) ) ?;
73+ }
74+ }
9475
9576 let mut lines = Vec :: new ( ) ;
96- for diff_object in diff:: lines ( & prev_json , & curr_json) {
77+ for diff_object in diff:: lines ( & head_json , & curr_json) {
9778 let line = match diff_object {
9879 diff:: Result :: Left ( left) => format ! ( "-{left}" ) ,
9980 diff:: Result :: Both ( unchanged, _) => format ! ( " {unchanged}" ) ,
@@ -103,18 +84,14 @@ impl ComponentDiff {
10384 lines. push ( line) ;
10485 }
10586 }
106- let diff = CodeView :: new ( CodeLanguage :: Diff , Some ( lines. join ( NEWLINE ) ) , None , None ) ;
87+ let diff = CodeView :: assemble ( CodeLanguage :: Diff , Some ( lines. join ( NEWLINE ) ) , None , None ) ;
10788 let diffs: Vec < CodeView > = vec ! [ diff] ;
10889
109- Ok ( Self {
90+ Ok ( ComponentDiff {
11091 component_id,
111- current : CodeView :: new (
92+ current : CodeView :: assemble (
11293 CodeLanguage :: Json ,
113- if is_new_component {
114- Some ( curr_json)
115- } else {
116- None
117- } ,
94+ if is_new_comp { Some ( curr_json) } else { None } ,
11895 None ,
11996 None ,
12097 ) ,
0 commit comments