use crate::XbrlError;
use std::{
borrow::Borrow,
fmt::{self, Display},
ops::Deref,
str::FromStr,
};
#[derive(Debug, PartialEq, Eq)]
pub struct SchemaRef {
pub href: String,
}
#[derive(Debug, PartialEq, Eq)]
pub struct RoleRef {
pub role_uri: String,
pub href: String,
}
#[derive(Debug, PartialEq, Eq)]
pub struct ArcroleRef {
pub arcrole_uri: String,
pub href: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NamespacePrefix(String);
impl NamespacePrefix {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for NamespacePrefix {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for NamespacePrefix {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for NamespacePrefix {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for NamespacePrefix {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for NamespacePrefix {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for NamespacePrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct NamespaceUri(String);
impl NamespaceUri {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for NamespaceUri {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for NamespaceUri {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for NamespaceUri {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for NamespaceUri {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for NamespaceUri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SchemaRefUrl(String);
impl SchemaRefUrl {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for SchemaRefUrl {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for SchemaRefUrl {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for SchemaRefUrl {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for SchemaRefUrl {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for SchemaRefUrl {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for SchemaRefUrl {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct RoleUri(String);
impl RoleUri {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for RoleUri {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for RoleUri {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for RoleUri {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for RoleUri {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for RoleUri {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for RoleUri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ArcroleUri(String);
impl ArcroleUri {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for ArcroleUri {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for ArcroleUri {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for ArcroleUri {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for ArcroleUri {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Display for ArcroleUri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ConceptId(String);
impl ConceptId {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl From<String> for ConceptId {
fn from(value: String) -> Self {
Self(value)
}
}
impl From<&str> for ConceptId {
fn from(value: &str) -> Self {
Self(value.to_owned())
}
}
impl Deref for ConceptId {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_str()
}
}
impl AsRef<str> for ConceptId {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl Borrow<str> for ConceptId {
fn borrow(&self) -> &str {
self.as_str()
}
}
impl fmt::Display for ConceptId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct QName {
pub prefix: Option<NamespacePrefix>,
pub local_name: String,
}
impl Display for QName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(prefix) = &self.prefix {
write!(f, "{}:{}", prefix, self.local_name)
} else {
f.write_str(&self.local_name)
}
}
}
impl FromStr for QName {
type Err = XbrlError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(parse_qname(s))
}
}
pub fn parse_qname(value: &str) -> QName {
if let Some(idx) = value.find(':') {
QName {
prefix: Some(NamespacePrefix::from(&value[..idx])),
local_name: value[idx + 1..].to_string(),
}
} else {
QName {
prefix: None,
local_name: value.to_string(),
}
}
}
pub fn parse_u32(value: &str) -> Result<u32, XbrlError> {
value.parse::<u32>().map_err(|_| XbrlError::ParseError {
expected: "integer",
value: value.to_string(),
})
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ExpandedName {
pub namespace_uri: NamespaceUri,
pub local_name: String,
}
impl ExpandedName {
pub fn new(namespace_uri: NamespaceUri, local_name: String) -> Self {
Self {
namespace_uri,
local_name,
}
}
}
impl Display for ExpandedName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{{}}}{}", self.namespace_uri, self.local_name)
}
}