Skip to content

Commit

Permalink
Adding more Metadata and displaying them on the editor
Browse files Browse the repository at this point in the history
  • Loading branch information
notdanilo committed Nov 7, 2023
1 parent 3e2a7d5 commit de85cc6
Show file tree
Hide file tree
Showing 17 changed files with 284 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# IntelliJ idea auto-generated files
.idea
.gradle
.vscode

# Space for testing things
workbench
Expand Down
17 changes: 15 additions & 2 deletions ecosystem/python/parser/src/parser/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ligen::parser::ParserConfig;
use ligen::ir::{Metadata, Version};
use ligen::ir::{Metadata, Version, VersionRequirement, Author, Dependency, Language};

use crate::prelude::*;

Expand All @@ -16,7 +16,20 @@ impl Parser<python_pkginfo::Metadata> for MetadataParser {
type Output = Metadata;
fn parse(&self, input: python_pkginfo::Metadata, _config: &ParserConfig) -> Result<Self::Output> {
let version = Version::try_from(input.version.as_str())?;
Ok(Self::Output { version })
let requirement = VersionRequirement::try_from(input.requires_python.unwrap_or_default().as_str())?;
let language = Language { name: "Python".into(), requirement };
let homepage = input.home_page.unwrap_or_default();
let summary = input.summary.unwrap_or_default();
let description = input.description.unwrap_or_default();
let keywords = input.keywords.unwrap_or_default().split(',').map(String::from).collect();
let authors = vec![Author::new(input.author.unwrap_or_default(), input.author_email.unwrap_or_default())];
let license = input.license.unwrap_or_default();
let mut dependencies = Vec::new();
for requirement in input.requires_dist {
let requirement = Dependency::try_from(requirement.as_str())?;
dependencies.push(requirement);
}
Ok(Self::Output { version, authors, dependencies, keywords, description, language, homepage, summary, license })
}
}

Expand Down
15 changes: 15 additions & 0 deletions ligen/ir/src/library/metadata/author.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::prelude::*;

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Author {
pub name: String,
pub email: String,
}

impl Author {
pub fn new(name: impl Into<String>, email: impl Into<String>) -> Self {
let name = name.into();
let email = email.into();
Self { name, email }
}
}
18 changes: 18 additions & 0 deletions ligen/ir/src/library/metadata/dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::{prelude::*, VersionRequirement};

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Dependency {
pub identifier: String,
pub requirement: VersionRequirement,
}

impl TryFrom<&str> for Dependency {
type Error = Error;
fn try_from(value: &str) -> Result<Self> {
let mut parts = value.split(' ');
let identifier = parts.next().ok_or("Failed to get identifier.")?.to_string();
let requirement = parts.next().ok_or("Failed to get requirement.")?;
let requirement = VersionRequirement::try_from(requirement)?;
Ok(Self { identifier, requirement })
}
}
7 changes: 7 additions & 0 deletions ligen/ir/src/library/metadata/language.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::{prelude::*, VersionRequirement};

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Language {
pub name: String,
pub requirement: VersionRequirement,
}
15 changes: 15 additions & 0 deletions ligen/ir/src/library/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
use crate::prelude::*;

pub mod language;
pub mod dependency;
pub mod author;
pub mod version;

pub use language::*;
pub use dependency::*;
pub use author::*;
pub use version::*;

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Metadata {
pub version: Version,
pub language: Language,
pub summary: String,
pub description: String,
pub homepage: String,
pub authors: Vec<Author>,
pub dependencies: Vec<Dependency>,
pub keywords: Vec<String>,
pub license: String,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::prelude::*;

pub mod requirement;
pub use requirement::*;

#[derive(Shrinkwrap, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[shrinkwrap(mutable)]
pub struct Version(pub semver::Version);
Expand All @@ -15,7 +18,7 @@ impl TryFrom<&str> for Version {
fn try_from(value: &str) -> Result<Self> {
let version =
semver::Version::parse(value)
.map_err(|e| Error::Message(format!("Failed to parse version: {}", e)))?;
.map_err(|e| Error::Message(format!("Failed to parse version: {}, Reason: {}", value, e)))?;
let version = Self(version);
Ok(version)
}
Expand Down
23 changes: 23 additions & 0 deletions ligen/ir/src/library/metadata/version/requirement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::prelude::*;

#[derive(Shrinkwrap, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[shrinkwrap(mutable)]
pub struct VersionRequirement(pub semver::VersionReq);

impl TryFrom<&str> for VersionRequirement {
type Error = Error;
fn try_from(value: &str) -> Result<Self> {
let value = value.replace("==", "=");
let version =
semver::VersionReq::parse(&value)
.map_err(|e| Error::Message(format!("Failed to parse version requirement: {}, Reason: {}", value, e)))?;
let version = Self(version);
Ok(version)
}
}

impl Default for VersionRequirement {
fn default() -> Self {
Self(semver::VersionReq::parse("*").unwrap())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::{prelude::*, gui::ui::{editor::{widget::{Widget, WidgetFor}, settings::Settings}, Labeled, StringField}};

#[derive(Default)]
pub struct Author {}

impl Widget for Author {
type Input = ligen_ir::Author;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
Labeled::new(&input.name).show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.email);
});
}
}

impl WidgetFor for ligen_ir::Author {
type Widget = Author;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::{prelude::*, gui::ui::{editor::{widget::{Widget, WidgetFor}, settings::Settings}, Labeled, StringField}};

#[derive(Default)]
pub struct Dependency {}

impl Widget for Dependency {
type Input = ligen_ir::Dependency;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
Labeled::new(&input.identifier).show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.requirement);
});
}
}

impl WidgetFor for ligen_ir::Dependency {
type Widget = Dependency;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use ligen_gui_runtime::egui::CollapsingHeader;

use crate::{prelude::*, gui::ui::{editor::widget::Widget, StringField, Labeled}};

#[derive(Default)]
pub struct Language {}

impl Language {
pub fn new() -> Self {
Default::default()
}
}

impl Widget for Language {
type Input = ligen_ir::Language;
fn show(&mut self, settings: &crate::gui::ui::editor::settings::Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
CollapsingHeader::new("Language")
.default_open(false)
.show(ui, |ui| {
Labeled::new("Name").show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.name);
});
Labeled::new("Requirement").show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.requirement);
});
});
}
}
46 changes: 46 additions & 0 deletions tools/editor/src/gui/ui/layout/editor/ir/library/metadata/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
pub mod language;
pub use language::*;

pub mod author;
pub use author::*;

pub mod dependency;
pub use dependency::*;

use ligen_gui_runtime::egui::CollapsingHeader;

use crate::{prelude::*, gui::ui::{editor::{widget::Widget, settings::Settings}, StringField, SubWidgets}};
use crate::gui::ui::Labeled;

#[derive(Default)]
pub struct Metadata {}

impl Metadata {
pub fn new() -> Self {
Default::default()
}
}

impl Widget for Metadata {
type Input = ligen_ir::Metadata;
fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, input: &mut Self::Input) {
CollapsingHeader::new("Metadata")
.show(ui, |ui| {
Labeled::new("Version").show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.version);
});
Labeled::new("Homepage").show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.homepage);
});
Labeled::new("Summary").show(settings, ui, |ui| {
StringField::default().show(settings, ui, &mut input.summary);
});
Language::new().show(settings, ui, &mut input.language);
SubWidgets::new("Author").show(settings, ui, &mut input.authors);
SubWidgets::new_irregular("Dependency", "Dependencies").show(settings, ui, &mut input.dependencies);
CollapsingHeader::new("Description").show(ui, |ui| {
StringField::default().show(settings, ui, &mut input.description);
});
});
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod metadata;
pub use metadata::*;

use crate::gui::ui::editor::settings::Settings;
use crate::gui::ui::editor::widget::Widget;
pub use crate::prelude::*;
Expand Down Expand Up @@ -25,9 +28,9 @@ impl Widget for Library {
.id_source("library")
.show(ui, |ui| {
if settings.editor.editable_fields {
StringField::new()
.show(settings, ui, &mut library.identifier);
StringField::new().show(settings, ui, &mut library.identifier);
}
Metadata::new().show(settings, ui, &mut library.metadata);
Module::new().show(settings, ui, &mut library.root_module);
});
});
Expand Down
22 changes: 22 additions & 0 deletions tools/editor/src/gui/ui/utils/labeled/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::prelude::*;
use crate::gui::ui::editor::{widget::Widget, settings::Settings};


pub struct Labeled {
label: String
}

impl Labeled {
pub fn new(label: impl Into<String>) -> Self {
let label = label.into();
Self { label }
}

pub fn show(&mut self, settings: &Settings, ui: &mut egui::Ui, mut show: impl FnMut(&mut egui::Ui)) {
ui.horizontal(|ui| {
ui.label(&self.label);
show(ui);
});

}
}
4 changes: 3 additions & 1 deletion tools/editor/src/gui/ui/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ mod list;
mod text_printer;
mod sub_widgets;
mod symbols_count;
mod labeled;

pub use list::*;
pub use string_editable_field::*;
pub use enum_editable_field::*;
pub use optional_field::*;
pub use text_printer::*;
pub use sub_widgets::*;
pub use symbols_count::*;
pub use symbols_count::*;
pub use labeled::*;
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub use crate::prelude::*;

use std::path::PathBuf;
use ligen_ir::{Identifier, Path};
use ligen_ir::{Identifier, Path, Version, VersionRequirement};

pub trait StringEditable {
fn as_string(&self) -> String;
Expand All @@ -23,6 +23,47 @@ impl StringEditable for PathBuf {
}
}

impl StringEditable for String {
fn as_string(&self) -> String {
self.to_string()
}

fn update(&mut self, string: impl AsRef<str>) -> bool {
*self = string.as_ref().to_string();
true
}
}

impl StringEditable for Version {
fn as_string(&self) -> String {
self.to_string()
}

fn update(&mut self, string: impl AsRef<str>) -> bool {
if let Ok(version) = Version::try_from(string.as_ref()) {
*self = version;
true
} else {
false
}
}
}

impl StringEditable for VersionRequirement {
fn as_string(&self) -> String {
self.to_string()
}

fn update(&mut self, string: impl AsRef<str>) -> bool {
if let Ok(version) = VersionRequirement::try_from(string.as_ref()) {
*self = version;
true
} else {
false
}
}
}

impl StringEditable for Path {
fn as_string(&self) -> String {
self.to_string_with_separator("::")
Expand Down
6 changes: 6 additions & 0 deletions tools/editor/src/gui/ui/utils/sub_widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ impl SubWidgets {
pub fn new(name: impl AsRef<str>) -> Self {
let singular = name.as_ref().to_string();
let plural = singular.clone() + "s";
Self::new_irregular(singular, plural)
}

pub fn new_irregular(singular: impl AsRef<str>, plural: impl AsRef<str>) -> Self {
let singular = singular.as_ref().to_string();
let plural = plural.as_ref().to_string();
let add_button_name = format!("Add {}", singular.to_lowercase());
Self { plural, add_button_name }
}
Expand Down

0 comments on commit de85cc6

Please sign in to comment.