Skip to content

Commit fdb1525

Browse files
vitlindanicolasfara
authored andcommitted
feat: add Entity parsing
1 parent cf3b685 commit fdb1525

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/main/scala/dev/atedeg/Configuration.scala

+44
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,50 @@ import cats.implicits.*
1010

1111
import Extensions.*
1212

13+
sealed trait EntityType
14+
15+
object EntityType {
16+
17+
def fromString(s: String): Option[EntityType] = s match {
18+
case "class" => Some(Class)
19+
case "trait" => Some(Trait)
20+
case "enum" => Some(Enum)
21+
case "type" => Some(Type)
22+
case "case" => Some(Case)
23+
case "def" => Some(Def)
24+
case _ => None
25+
}
26+
}
27+
case object Class extends EntityType
28+
case object Trait extends EntityType
29+
case object Enum extends EntityType
30+
case object Type extends EntityType
31+
case object Case extends EntityType
32+
case object Def extends EntityType
33+
34+
final case class Entity(entityType: EntityType, link: String, name: String, packageName: String)
35+
36+
object Entity {
37+
private val entitiesFileName = "searchData.js"
38+
39+
private def allEntitiesFile(workingDir: File): File =
40+
workingDir / "target" / "site" / "scripts" / entitiesFileName
41+
42+
def readAll(workingDir: File): Either[Error, Set[Entity]] = Utils.parseFileWith(allEntitiesFile(workingDir))(parse)
43+
44+
private[atedeg] def parse(raw: String): Either[Error, Set[Entity]] =
45+
parseJsonString(raw).leftMap(CirceParsingFailure).flatMap(parseJson)
46+
47+
private def parseJson(json: Json): Either[Error, Set[Entity]] = {
48+
def build(maybeType: Option[EntityType], link: String, name: String, packageName: String): Option[Entity] =
49+
maybeType.map(Entity(_, link, name, packageName))
50+
51+
implicit val decodeEntityType: Decoder[Option[EntityType]] = Decoder.decodeString.map(EntityType.fromString)
52+
implicit val decodeEntity: Decoder[Option[Entity]] = Decoder.forProduct4("k", "l", "n", "d")(build)
53+
json.as[Set[Option[Entity]]].map(_.dropNone).leftMap(CirceDecodingFailure)
54+
}
55+
}
56+
1357
sealed trait IgnoredSelector
1458
final case class IgnoredClass(className: String) extends IgnoredSelector
1559
final case class IgnoredTrait(traitName: String) extends IgnoredSelector

0 commit comments

Comments
 (0)