Skip to content

Commit

Permalink
Refactor: Convert DesignComposeDefinition to proto (#1847)
Browse files Browse the repository at this point in the history
<!-- start git-machete generated -->

# Based on PR #1845

## Chain of upstream PRs & tree of downstream PRs as of 2024-12-09

* PR #1845:
  `main` ← `wb/froeht/convert-encodedimagemap`

  * **PR #1847 (THIS ONE)**:
    `wb/froeht/convert-encodedimagemap` ← `wb/froeht/1172-convert-dcd`

      * PR #1849:
        `wb/froeht/1172-convert-dcd` ← `wb/froeht/cargo-fix`

<!-- end git-machete generated -->

Fixes #1172
  • Loading branch information
timothyfroehlich authored Dec 10, 2024
1 parent 2a9942f commit 6f7c8cb
Show file tree
Hide file tree
Showing 65 changed files with 60 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ package com.android.designcompose.common

// LINT.IfChange
// Current serialized doc version
const val FSAAS_DOC_VERSION = 23
const val FSAAS_DOC_VERSION = 24
// LINT.ThenChange(crates/dc_bundle/src/legacy_definition.rs)
38 changes: 38 additions & 0 deletions crates/dc_bundle/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crate::definition::element::VariableMap;
use crate::definition::view::View;
use crate::Error;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::sync::Arc;

Expand All @@ -28,6 +31,41 @@ pub mod view;

include!(concat!(env!("OUT_DIR"), "/designcompose.definition.rs"));

impl DesignComposeDefinition {
pub fn new(
views: HashMap<NodeQuery, View>,
images: EncodedImageMap,
component_sets: HashMap<String, String>,
variable_map: VariableMap,
) -> DesignComposeDefinition {
DesignComposeDefinition {
views: views.iter().map(|(k, v)| (k.encode(), v.to_owned())).collect(),
images: images.into(),
component_sets,
variable_map: Some(variable_map),
}
}
pub fn views(&self) -> Result<HashMap<NodeQuery, View>, Error> {
self.views
.iter()
.map(|(k, v)| NodeQuery::decode(k).map(|query| (query, v.clone())))
.collect::<Result<HashMap<NodeQuery, View>, Error>>()
}
}

impl fmt::Display for DesignComposeDefinition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// NOTE: Using `write!` here instead of typical `format!`
// to keep newlines.
write!(
f,
"Views: {}\nComponent Sets: {}",
self.views.keys().count(),
self.component_sets.keys().count()
)
}
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash, Serialize, Deserialize)]
pub enum NodeQuery {
/// Find the node by ID
Expand Down
51 changes: 1 addition & 50 deletions crates/dc_bundle/src/legacy_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,13 @@
// This module holds the original structures that made up the serialized design doc, and which will be replaced with the protobuf implementations of the Design Definition

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
// To help keep the legacy definition files clear we alias `crate::definition`, which is the base
// module for the generated protobuf files to `proto`, so that all of the protobuf-generated types
// inside `legacy_definition` must be prepended with `proto::`
use crate::definition::element::VariableMap;
use crate::definition::view::View;
use crate::definition::{EncodedImageMap, NodeQuery};
use crate::Error;

// LINT.IfChange
static CURRENT_VERSION: u32 = 23;
static CURRENT_VERSION: u32 = 24;
// Lint.ThenChange(common/src/main/java/com/android/designcompose/common/FsaasSession.kt)

// This is our serialized document type.
Expand Down Expand Up @@ -84,47 +79,3 @@ impl fmt::Display for DesignComposeDefinitionHeader {
)
}
}

// This is our serialized document type.
#[derive(Serialize, Deserialize, Debug)]
pub struct DesignComposeDefinition {
pub views: HashMap<String, View>,
pub images: HashMap<String, Vec<u8>>,
pub component_sets: HashMap<String, String>,
pub variable_map: VariableMap,
}

impl DesignComposeDefinition {
pub fn new(
views: HashMap<NodeQuery, View>,
images: EncodedImageMap,
component_sets: HashMap<String, String>,
variable_map: VariableMap,
) -> DesignComposeDefinition {
DesignComposeDefinition {
views: views.iter().map(|(k, v)| (k.encode(), v.to_owned())).collect(),
images: images.into(),
component_sets,
variable_map,
}
}
pub fn views(&self) -> Result<HashMap<NodeQuery, View>, Error> {
self.views
.iter()
.map(|(k, v)| NodeQuery::decode(k).map(|query| (query, v.clone())))
.collect::<Result<HashMap<NodeQuery, View>, Error>>()
}
}

impl fmt::Display for DesignComposeDefinition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// NOTE: Using `write!` here instead of typical `format!`
// to keep newlines.
write!(
f,
"Views: {}\nComponent Sets: {}",
self.views.keys().count(),
self.component_sets.keys().count()
)
}
}
8 changes: 4 additions & 4 deletions crates/figma_import/src/design_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

use dc_bundle::legacy_figma_live_update::FigmaDocInfo;
use serde::{Deserialize, Serialize};

use dc_bundle::definition::DesignComposeDefinition;
use dc_bundle::legacy_definition::{
DesignComposeDefinition, DesignComposeDefinitionHeader, DesignComposeDefinitionHeaderV0,
DesignComposeDefinitionHeader, DesignComposeDefinitionHeaderV0,
};
use dc_bundle::legacy_figma_live_update::FigmaDocInfo;
use serde::{Deserialize, Serialize};

// This is the struct we send over to the client. It contains the serialized document
// along with some extra data: document branches, project files, and errors
Expand Down
4 changes: 2 additions & 2 deletions crates/figma_import/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// limitations under the License.

use crate::{Document, ImageContextSession, ServerFigmaDoc};
use dc_bundle::definition::NodeQuery;
use dc_bundle::legacy_definition::{DesignComposeDefinition, DesignComposeDefinitionHeader};
use dc_bundle::definition::{DesignComposeDefinition, NodeQuery};
use dc_bundle::legacy_definition::DesignComposeDefinitionHeader;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion crates/figma_import/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod variable_utils;

// Exports for library users
pub use dc_bundle::definition::element::Rectangle;
pub use dc_bundle::legacy_definition::DesignComposeDefinition;
pub use dc_bundle::definition::DesignComposeDefinition;
pub use dc_bundle::legacy_definition::DesignComposeDefinitionHeader;
pub use design_definition::ServerFigmaDoc;
pub use design_definition::{load_design_def, load_design_def_header_v0, save_design_def};
Expand Down
4 changes: 2 additions & 2 deletions crates/figma_import/src/tools/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use std::io::{Error, ErrorKind, Write};
use crate::{Document, ProxyConfig};
/// Utility program to fetch a doc and serialize it to file
use clap::Parser;
use dc_bundle::definition::NodeQuery;
use dc_bundle::legacy_definition::{DesignComposeDefinition, DesignComposeDefinitionHeader};
use dc_bundle::definition::{DesignComposeDefinition, NodeQuery};
use dc_bundle::legacy_definition::DesignComposeDefinitionHeader;

#[derive(Debug)]
#[allow(dead_code)]
Expand Down
4 changes: 2 additions & 2 deletions crates/figma_import/src/tools/fetch_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use dc_bundle::definition::layout::LayoutSizing;
use dc_bundle::definition::view::view_data::ViewDataType;
use dc_bundle::definition::view::view_data::ViewDataType::{Container, Text};
use dc_bundle::definition::view::{view_data, View};
use dc_bundle::definition::NodeQuery;
use dc_bundle::legacy_definition::{DesignComposeDefinition, DesignComposeDefinitionHeader};
use dc_bundle::definition::{DesignComposeDefinition, NodeQuery};
use dc_bundle::legacy_definition::DesignComposeDefinitionHeader;
use layout::LayoutManager;
use std::collections::HashMap;
use std::io;
Expand Down
Binary file modified crates/figma_import/tests/layout-unit-tests.dcf
Binary file not shown.
3 changes: 1 addition & 2 deletions crates/figma_import/tests/layout_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ use dc_bundle::definition::layout::LayoutSizing;
use dc_bundle::definition::view::view_data::ViewDataType;
use dc_bundle::definition::view::view_data::ViewDataType::{Container, Text};
use dc_bundle::definition::view::{view_data, View};
use dc_bundle::definition::NodeQuery;
use dc_bundle::legacy_definition::DesignComposeDefinition;
use dc_bundle::definition::{DesignComposeDefinition, NodeQuery};
use figma_import::load_design_def;
use layout::LayoutManager;
use std::collections::HashMap;
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ internal fun DocServer.fetchDocuments(firstFetch: Boolean): Boolean {
DesignSettings.fileFetchStatus[id]?.lastUpdateFromFetch = now
}

VariableManager.init(doc.c.docId, doc.c.document.variable_map)
VariableManager.init(doc.c.docId, doc.c.document.variable_map.get())

// Get the list of subscribers to this document id
val subs: Array<LiveDocSubscription> =
Expand Down Expand Up @@ -512,7 +512,7 @@ internal fun DocServer.doc(
}
targetDoc
}
targetDoc?.let { VariableManager.init(it.c.docId, it.c.document.variable_map) }
targetDoc?.let { VariableManager.init(it.c.docId, it.c.document.variable_map.get()) }
docUpdateCallback?.invoke(docId, targetDoc?.c?.toSerializedBytes(Feedback))
setLiveDoc(targetDoc)

Expand All @@ -528,7 +528,7 @@ internal fun DocServer.doc(
// Don't return a doc with the wrong ID.
if (liveDoc != null && liveDoc.c.docId == docId) return liveDoc
if (preloadedDoc != null && preloadedDoc.c.docId == docId) {
VariableManager.init(preloadedDoc.c.docId, preloadedDoc.c.document.variable_map)
VariableManager.init(preloadedDoc.c.docId, preloadedDoc.c.document.variable_map.get())
docUpdateCallback?.invoke(docId, preloadedDoc.c.toSerializedBytes(Feedback))
endSection()
return preloadedDoc
Expand Down Expand Up @@ -557,7 +557,7 @@ internal fun DocServer.doc(
synchronized(DesignSettings.fileFetchStatus) {
DesignSettings.fileFetchStatus[docId]?.lastLoadFromDisk = Instant.now()
}
VariableManager.init(decodedDoc.c.docId, decodedDoc.c.document.variable_map)
VariableManager.init(decodedDoc.c.docId, decodedDoc.c.document.variable_map.get())
docUpdateCallback?.invoke(docId, decodedDoc.c.toSerializedBytes(Feedback))
endSection()
return decodedDoc
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 4 additions & 8 deletions proto/definition/design_compose_definition.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ option optimize_for = LITE_RUNTIME;
// designs in a DesignCompose project.
message DesignComposeDefinition {

string last_modified = 1;
// The string here is a NodeQuery, encoded to a string so that it can be used
// as the key to the map. See crates/dc_bundle/src/definition.rs for encoding/decoding
map<string, definition.view.View> views = 2;
map<string, definition.view.View> views = 1;
// Images contains a mapping from ImageKey to network bytes. It can
// create an ImageMap and is intended to be used when we want to use
// Figma-defined components but do not want to communicate with the Figma
// service.
map<string, bytes> images = 3;
string name = 4;
map<string, string> component_sets = 5;
string version = 6;
string id = 7;
definition.element.VariableMap variable_map = 8;
map<string, bytes> images = 2;
map<string, string> component_sets = 3;
definition.element.VariableMap variable_map = 4;
}
Binary file not shown.
Binary file not shown.

0 comments on commit 6f7c8cb

Please sign in to comment.