use super::company_formation::FORMATION_TAGS;
pub use super::company_formation::{registered_agent, FormationAttrs};
use super::company_profile::{profile_of, PROFILE_TAGS};
pub use super::company_profile::{address, listing, owner, stock, ProfileAttrs};
use super::company_governance::{governance_of, GOVERNANCE_TAGS};
use super::company_deals::{deals_of, DEALS_TAGS};
use super::element::{attributes, elements, fn_name, rust_name};
use super::classification::{classification_problem, PRODUCT_SCHEMES};
use super::business_finance::{finance_of, COMPANY_FINANCE_MANY, COMPANY_FINANCE_ONE, FINANCE_TAGS};
use super::people_ops::{team_of, PEOPLE_OPS_TAGS};
use super::facts::fact_marker;
use super::{Declared, Family};
pub use super::business_finance::{bank_account, budget, data_room, include, payroll, FinanceAttrs};
pub use super::people_ops::{band, benefit, checklist, role, team, PeopleOpsAttrs};
pub use super::company_governance::{assignment, compliance, evidence, ip_asset, obligation, policy, seat, vote, GovernanceAttrs};
pub use super::company_deals::{deal, deal_move, deal_stage, pipeline, DealAttrs};
pub use super::{fragment, json, El, Json, Value};
mod equity;
use equity::{rounds_of, EQUITY_TAGS};
pub use equity::{investment, round, EquityAttrs};
elements! {
company = "Company": children;
entity = "Entity": leaf;
registration = "Registration": leaf;
books = "Books": leaf;
cap_table = "CapTable": leaf;
people = "People": leaf;
calendar = "Calendar": leaf;
matters = "Matters": leaf;
subsidiary = "Subsidiary": leaf;
paper = "Paper": children;
signature = "Signature": leaf;
version = "Version": leaf;
brand = "Brand": leaf;
product = "Product": leaf;
relationships = "Relationships": leaf;
rfc = "Rfc": children;
option = "Option": children;
change = "Change": leaf;
case = "Case": children;
harm = "Harm": leaf;
relief = "Relief": leaf;
}
attributes! {
pub trait BusinessAttrs {
name; about; codes; tags; postal_address; files; dashboard; banner;
jurisdiction; form; tax_classification; formed; fiscal_year_end; ids; operates_in; conditions;
kind; id; issued; expires; file; journal; ocf; folder;
procedure; title; body; target; engine_json; summary;
op; parameter; value; duty_id; text; protects; harm; elements; criminal; uniqueness; issuers; modalities;
respondent; filed_for; source; article; agreement_id; term; act; act_date; evidence; media; reopens;
interest; description; amount; harm_ids; days;
ownership; since; method; status; parties; effective; renewal; notice_days;
party; signer; capacity; date; note;
maker; seller;
}
}
pub(crate) const FAMILY: Family = Family { module: "business", roots: &["Company", "Rfc", "Case"], declare };
#[derive(Clone, Copy, PartialEq)]
pub(super) enum Kind {
Text,
Texts,
Bool,
Record,
Data,
Amount,
}
pub(crate) use Kind::*;
pub(super) struct TagSpec {
pub(super) tag: &'static str,
pub(super) props: &'static [(&'static str, Kind)],
pub(super) required: &'static [&'static str],
pub(super) one: &'static [(&'static str, &'static str)],
pub(super) many: &'static [(&'static str, &'static str)],
}
pub(crate) const fn spec(tag: &'static str, props: &'static [(&'static str, Kind)], required: &'static [&'static str]) -> TagSpec {
TagSpec { tag, props, required, one: &[], many: &[] }
}
const BUSINESS_TAGS: &[TagSpec] = &[
TagSpec {
tag: "Company",
props: &[("name", Text), ("about", Text), ("codes", Texts), ("tags", Texts), ("postalAddress", Text), ("files", Texts), ("dashboard", Text), ("banner", Text), ("website", Text)],
required: &["name"],
one: &[("entity", "Entity"), ("books", "Books"), ("captable", "CapTable"), ("people", "People"), ("calendar", "Calendar"), ("matters", "Matters"), ("team", "Team"), ("compliance", "Compliance"), ("relationships", "Relationships"), ("pipeline", "Pipeline")],
many: &[("registrations", "Registration"), ("subsidiaries", "Subsidiary"), ("papers", "Paper"), ("rounds", "Round"), ("registeredAgents", "RegisteredAgent"), ("addresses", "Address"), ("listings", "Listing"), ("stock", "Stock"), ("owners", "Owner"), ("brands", "Brand"),
("seats", "Seat"), ("obligations", "Obligation"), ("evidence", "Evidence"), ("ipAssets", "IpAsset"), ("assignments", "Assignment"), ("policies", "Policy"), ("products", "Product"), ("deals", "Deal")],
},
spec(
"Entity",
&[
("jurisdiction", Text),
("form", Text),
("taxClassification", Text),
("name", Text),
("formed", Text),
("fiscalYearEnd", Text),
("ids", Record),
("operatesIn", Texts),
("conditions", Record),
("dissolved", Text),
],
&["jurisdiction"],
),
spec("Registration", &[("kind", Text), ("jurisdiction", Text), ("id", Text), ("issued", Text), ("expires", Text), ("file", Text)], &["kind"]),
spec("Books", &[("journal", Text)], &["journal"]),
spec("CapTable", &[("ocf", Text)], &["ocf"]),
spec("People", &[("folder", Text)], &["folder"]),
spec("Calendar", &[("folder", Text)], &["folder"]),
spec("Matters", &[("folder", Text)], &["folder"]),
spec("Subsidiary", &[("file", Text), ("ownership", Amount), ("since", Text), ("method", Text)], &["file", "ownership"]),
TagSpec {
tag: "Paper",
props: &[
("kind", Text),
("title", Text),
("file", Text),
("status", Text),
("parties", Texts),
("jurisdiction", Text),
("effective", Text),
("expires", Text),
("renewal", Text),
("noticeDays", Amount),
("rule", Text),
("period", Text),
("due", Text),
("submitted", Text),
("accepted", Text),
("confirmation", Text),
("body", Text),
("threshold", Text),
("quorum", Text),
],
required: &["kind", "file"],
one: &[],
many: &[("signatures", "Signature"), ("versions", "Version"), ("votes", "Vote")],
},
spec("Signature", &[("party", Text), ("signer", Text), ("capacity", Text), ("date", Text), ("method", Text), ("evidence", Text)], &["party"]),
spec("Version", &[("file", Text), ("date", Text), ("note", Text)], &["file"]),
spec("Brand", &[("file", Text)], &["file"]),
spec("Product", &[("id", Text), ("name", Text), ("about", Text), ("codes", Texts), ("tags", Texts), ("maker", Text), ("seller", Text), ("status", Text), ("file", Text)], &["name"]),
spec("Relationships", &[("file", Text)], &["file"]),
TagSpec {
tag: "Rfc",
props: &[("id", Text), ("procedure", Text), ("title", Text), ("body", Text), ("target", Text), ("engineJson", Text)],
required: &[],
one: &[],
many: &[("options", "Option")],
},
TagSpec { tag: "Option", props: &[("title", Text), ("summary", Text)], required: &[], one: &[], many: &[("changes", "Change")] },
spec(
"Change",
&[
("op", Text),
("parameter", Text),
("value", Text),
("dutyId", Text),
("title", Text),
("text", Text),
("protects", Texts),
("harm", Text),
("elements", Text),
("criminal", Bool),
("kind", Text),
("uniqueness", Text),
("issuers", Text),
("modalities", Texts),
],
&["op"],
),
TagSpec {
tag: "Case",
props: &[
("id", Text),
("respondent", Text),
("filedFor", Text),
("criminal", Bool),
("source", Text),
("dutyId", Text),
("article", Text),
("agreementId", Text),
("term", Text),
("act", Text),
("actDate", Text),
("evidence", Text),
("media", Data),
("reopens", Text),
],
required: &[],
one: &[],
many: &[("harms", "Harm"), ("relief", "Relief")],
},
spec("Harm", &[("id", Text), ("interest", Text), ("description", Text), ("amount", Amount)], &["id"]),
spec("Relief", &[("kind", Text), ("description", Text), ("harmIds", Texts), ("amount", Amount), ("days", Amount)], &["kind"]),
];
pub struct BusinessNode {
pub tag: &'static str,
pub props: Vec<(String, Json)>,
pub one: Vec<(&'static str, Option<BusinessNode>)>,
pub many: Vec<(&'static str, Vec<BusinessNode>)>,
pub source: Option<Json>,
}
impl BusinessNode {
pub(super) fn prop(&self, name: &str) -> Option<&Json> {
self.props.iter().find(|(k, _)| k == name).map(|(_, v)| v)
}
pub(super) fn one(&self, field: &str) -> Option<&BusinessNode> {
self.one.iter().find(|(f, _)| *f == field).and_then(|(_, n)| n.as_ref())
}
pub(super) fn many(&self, field: &str) -> &[BusinessNode] {
self.many.iter().find(|(f, _)| *f == field).map(|(_, l)| l.as_slice()).unwrap_or(&[])
}
}
fn value(tag: &str, prop: &str, kind: Kind, v: &Json) -> Result<Json, String> {
if let Some(fact) = fact_marker(v, &format!("{}(): {}", fn_name(tag), rust_name(prop)), |x| value(tag, prop, kind, x))? {
return Ok(fact);
}
let bad = |what: &str| format!("{}(): {} is {what}, not {}", fn_name(tag), rust_name(prop), v.text());
let ok = match (kind, v) {
(Text, Json::Str(_)) | (Bool, Json::Bool(_)) | (Record, Json::Obj(_)) | (Data, _) => true,
(Amount, Json::Str(_)) => true,
(Amount, Json::Num(n)) => n.is_finite(),
(Texts, Json::Arr(items)) => items.iter().all(|x| matches!(x, Json::Str(_))),
_ => false,
};
if ok {
return Ok(v.clone());
}
Err(bad(match kind {
Text => "text",
Amount => "text or a number",
Texts => "a list of text",
Bool => "true or false",
Record => "an object",
Data => unreachable!("any JSON is data"),
}))
}
pub fn read_business_node(el: &El) -> Result<BusinessNode, String> {
let Some(spec) = BUSINESS_TAGS.iter().chain(FINANCE_TAGS).chain(FORMATION_TAGS).chain(PROFILE_TAGS).chain(EQUITY_TAGS).chain(PEOPLE_OPS_TAGS).chain(GOVERNANCE_TAGS).chain(DEALS_TAGS).find(|s| s.tag == el.tag) else {
return Err(format!("{}() is not an element of a company, an RFC or a case", fn_name(el.tag)));
};
let f = fn_name(el.tag);
let mut props = Vec::new();
for (k, v) in &el.attrs {
let Some((_, kind)) = spec.props.iter().find(|(p, _)| p == k) else {
let takes: Vec<String> = spec.props.iter().map(|(p, _)| rust_name(p)).collect();
return Err(format!("{f}() has no attribute {} (it takes {})", rust_name(k), takes.join(", ")));
};
props.push((k.clone(), value(el.tag, k, *kind, v)?));
}
for k in spec.required {
let given = props.iter().find(|(p, _)| p == k).map(|(_, v)| v);
if given.is_none() || given.and_then(Json::as_str) == Some("") {
return Err(format!("{f}() needs {}", rust_name(k)));
}
}
let (finance_one, finance_many): (&[(&str, &str)], &[(&str, &str)]) = if spec.tag == "Company" { (COMPANY_FINANCE_ONE, COMPANY_FINANCE_MANY) } else { (&[], &[]) };
let spec_one: Vec<(&'static str, &'static str)> = spec.one.iter().chain(finance_one).copied().collect();
let spec_many: Vec<(&'static str, &'static str)> = spec.many.iter().chain(finance_many).copied().collect();
let mut one: Vec<(&'static str, Option<BusinessNode>)> = spec_one.iter().map(|(field, _)| (*field, None)).collect();
let mut many: Vec<(&'static str, Vec<BusinessNode>)> = spec_many.iter().map(|(field, _)| (*field, Vec::new())).collect();
for child in el.child_elements() {
if let Some(i) = spec_one.iter().position(|(_, t)| *t == child.tag) {
let node = read_business_node(child)?;
if one[i].1.is_some() {
return Err(format!("{f}() has two {}()", fn_name(child.tag)));
}
one[i].1 = Some(node);
} else if let Some(i) = spec_many.iter().position(|(_, t)| *t == child.tag) {
many[i].1.push(read_business_node(child)?);
} else {
let takes: Vec<String> = spec_one.iter().chain(&spec_many).map(|(_, t)| format!("{}()", fn_name(t))).collect();
let takes = if takes.is_empty() { "no children".to_string() } else { takes.join(", ") };
return Err(format!("{f}() does not take {}() (it takes {takes})", fn_name(child.tag)));
}
}
Ok(BusinessNode { tag: el.tag, props, one, many, source: el.source_json() })
}
fn sources_of(node: &BusinessNode, at: &str, out: &mut Vec<(String, Json)>) {
if let Some(s) = &node.source {
out.push((at.to_string(), s.clone()));
}
let join = |f: &str| if at.is_empty() { f.to_string() } else { format!("{at}/{f}") };
for (field, n) in &node.one {
if let Some(n) = n {
sources_of(n, &join(field), out);
}
}
for (field, list) in &node.many {
for (i, n) in list.iter().enumerate() {
sources_of(n, &join(&format!("{field}/{i}")), out);
}
}
}
fn or_null(v: Option<&Json>) -> Json {
v.cloned().unwrap_or(Json::Null)
}
fn classified_fields(node: &BusinessNode, schemes: Option<&[&str]>, at: &str) -> Result<Vec<(String, Json)>, String> {
if let Some(problem) = classification_problem(node.prop("codes"), node.prop("tags"), schemes) {
return Err(format!("{at} {problem}"));
}
Ok(["codes", "tags"].iter().filter_map(|k| node.prop(k).map(|v| (k.to_string(), v.clone()))).collect())
}
pub fn company_of(node: &BusinessNode) -> Result<Json, String> {
let classification = classified_fields(node, None, "company()")?;
let entity = match node.one("entity") {
None => Json::Null,
Some(e) => {
let mut out = Json::obj().with("jurisdiction", or_null(e.prop("jurisdiction"))).with("form", or_null(e.prop("form")));
if let Some(t) = e.prop("taxClassification") {
out.set("taxClassification", t.clone());
}
out = out
.with("name", or_null(e.prop("name")))
.with("formed", or_null(e.prop("formed")))
.with("fiscalYearEnd", or_null(e.prop("fiscalYearEnd")))
.with("ids", e.prop("ids").cloned().unwrap_or_else(Json::obj))
.with("operatesIn", e.prop("operatesIn").cloned().unwrap_or(Json::Arr(Vec::new())));
if let Some(c) = e.prop("conditions") {
out.set("conditions", c.clone());
}
if let Some(d) = e.prop("dissolved") {
out.set("dissolved", d.clone());
}
out
}
};
let reference = |field: &str, prop: &str| or_null(node.one(field).and_then(|n| n.prop(prop)));
let registrations: Vec<Json> = node.many("registrations").iter().map(|r| Json::Obj(r.props.clone())).collect();
let on = ["books", "captable", "people", "calendar", "matters"].iter().any(|f| node.one(f).is_some()) || !registrations.is_empty();
let standard = if on {
Json::obj()
.with("enabled", true)
.with("books", reference("books", "journal"))
.with("captable", reference("captable", "ocf"))
.with("people", reference("people", "folder"))
.with("calendar", reference("calendar", "folder"))
.with("registrations", registrations)
.with("matters", reference("matters", "folder"))
} else {
Json::obj().with("enabled", false)
};
let company = Json::obj()
.with("format", "commandagi-company")
.with("name", or_null(node.prop("name")))
.with("about", node.prop("about").cloned().unwrap_or_else(|| "".into()));
let company = match node.prop("postalAddress") {
Some(p) => company.with("postalAddress", p.clone()),
None => company,
};
let company = company
.with("files", node.prop("files").cloned().unwrap_or(Json::Arr(Vec::new())))
.with("dashboard", or_null(node.prop("dashboard")));
let company = match node.prop("banner") {
Some(b) => company.with("banner", b.clone()),
None => company,
};
let company = company.with("entity", entity).with("standard", standard);
let subsidiaries: Vec<Json> = node.many("subsidiaries").iter().map(|x| Json::Obj(x.props.clone())).collect();
let papers: Vec<Json> = node
.many("papers")
.iter()
.map(|x| {
let mut paper = Json::Obj(x.props.clone());
for field in ["signatures", "versions", "votes"] {
let list: Vec<Json> = x.many(field).iter().map(|n| Json::Obj(n.props.clone())).collect();
if !list.is_empty() {
paper.set(field, list);
}
}
paper
})
.collect();
let company = if subsidiaries.is_empty() { company } else { company.with("subsidiaries", subsidiaries) };
let company = if papers.is_empty() { company } else { company.with("papers", papers) };
let rounds = rounds_of(node);
let company = if rounds.is_empty() { company } else { company.with("rounds", rounds) };
let company = match finance_of(node) {
Some(finance) => company.with("finance", finance),
None => company,
};
let agents: Vec<Json> = node.many("registeredAgents").iter().map(|x| Json::Obj(x.props.clone())).collect();
let company = if agents.is_empty() { company } else { company.with("registeredAgents", agents) };
let company = profile_of(node, company);
let company = match node.one("team") {
Some(t) => company.with("team", team_of(t)),
None => company,
};
let brands: Vec<Json> = node.many("brands").iter().map(|x| Json::Obj(x.props.clone())).collect();
let company = if brands.is_empty() { company } else { company.with("brands", brands) };
let products: Vec<Json> = node
.many("products")
.iter()
.map(|x| {
let at = format!("product().name({})", or_null(x.prop("name")).text());
classified_fields(x, Some(PRODUCT_SCHEMES), &at).map(|_| Json::Obj(x.props.clone()))
})
.collect::<Result<_, _>>()?;
let company = if products.is_empty() { company } else { company.with("products", products) };
let company = match node.one("relationships").and_then(|n| n.prop("file")) {
Some(file) => company.with("relationships", file.clone()),
None => company,
};
let company = governance_of(node, company);
let mut company = deals_of(node, company);
for (k, v) in classification {
company.set(&k, v);
}
Ok(company)
}
fn with_id(node: &BusinessNode, draft: Json) -> Json {
let out = match node.prop("id") {
Some(id) => Json::obj().with("id", id.clone()),
None => Json::obj(),
};
out.with("draft", draft)
}
fn draft_of(node: &BusinessNode) -> Json {
Json::Obj(node.props.iter().filter(|(k, _)| k != "id").cloned().collect())
}
pub fn rfc_of(node: &BusinessNode) -> Json {
let change = |c: &BusinessNode| {
let mut out = Json::Obj(c.props.iter().filter(|(k, _)| k != "parameter").cloned().collect());
if let Some(p) = c.prop("parameter") {
out.set("key", p.clone());
}
out
};
let options: Vec<Json> = node
.many("options")
.iter()
.map(|o| Json::Obj(o.props.clone()).with("changes", o.many("changes").iter().map(change).collect::<Vec<_>>()))
.collect();
with_id(node, draft_of(node).with("options", options))
}
pub fn case_of(node: &BusinessNode) -> Json {
let records = |field: &str| node.many(field).iter().map(|n| Json::Obj(n.props.clone())).collect::<Vec<_>>();
with_id(node, draft_of(node).with("harms", records("harms")).with("relief", records("relief")))
}
fn declare(root: El) -> Result<Declared, String> {
let node = read_business_node(&root)?;
let (format, document) = match root.tag {
"Company" => ("company", company_of(&node)?),
"Rfc" => ("rfc", rfc_of(&node)),
_ => ("case", case_of(&node)),
};
let mut sources = Vec::new();
sources_of(&node, "", &mut sources);
Ok(Declared::Document { format: format.into(), document, sources: Json::Obj(sources) })
}